Tencent AI 测试

简述

昨天花了一点时间测试了下腾讯AI开放平台的看图说话接口,感觉接口文档描述比较清晰准确,测试过程中除了时区设置有点问题外,还是比较顺利的。

下面记录下测试过程。

测试过程

登录图片识别首页了解功能介绍,并测试演示demo

首页

阅读接口文档,了解鉴权机制
接口文档
鉴权机制
接口错误吗描述

申请开发帐号,创建应用


编写测试代码

//tencentai.go
package tencentai


import (
    "crypto/md5"
    "fmt"
    "io"
    "math/rand"
    "net/http"
    "net/url"
    "sort"
    "strings"
    "sync"
    "time"
)


type AiClient struct {
    Client *http.Client
    Appid  string
    Appkey string
    Mutext sync.Mutex
}


//生成随机字符串
func GetRandomString(length int) string {
    str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    databytes := []byte(str)
    byteslen := len(databytes)
    result := []byte{}
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i := 0; i < length; i++ {
        result = append(result, databytes[r.Intn(byteslen)])
    }
    return string(result)
}


func NewAiClient(appid, appkey string) *AiClient {
    client := &AiClient{Appid: appid, Appkey: appkey}
    client.Client = &http.Client{}


    return client
}


func (this *AiClient) getMd5(str string) string {
    w := md5.New()
    io.WriteString(w, str)
    md5str := fmt.Sprintf("%x", w.Sum(nil))
    return strings.ToUpper(string(md5str))
}


func (this *AiClient) GetBody(options map[string]string) io.Reader {
    urlstr := string("")


    keys := make([]string, 0, len(options))
    for k := range options {
        keys = append(keys, k)
    }


    sort.Strings(keys)


    for _, key := range keys {
        if key == "sign" {
            continue
        }


        urlstr += key
        urlstr += "="
        urlstr += url.QueryEscape(options[key])
        urlstr += "&"


    }


    urlstr += "app_key="
    urlstr += url.QueryEscape(this.Appkey)
    sign := this.getMd5(urlstr)
    urlstr += "&sign="
    urlstr += url.QueryEscape(sign)
    return strings.NewReader(urlstr)


}


func (this *AiClient) Post(url string, body io.Reader) (*http.Response, error) {
    reqest, err := http.NewRequest("POST", url, body)
    if err != nil {
        return nil, err
    }
    reqest.Header.Set("Content-Type", "application/x-www-form-urlencoded")


    return this.Client.Do(reqest)


}






//vision.go
package tencentai


import (
    "bytes"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "io"
    "os"
    "time"
)


const (
    VISION_IMGTOTEXT_URI = "https://api.ai.qq.com/fcgi-bin/vision/vision_imgtotext"
)


func (this *AiClient) VisionImgFileToText(img string) (error, VisionImgToTextResp) {
    var vtt VisionImgToTextResp


    file, err := os.Open(img)
    if err != nil {
        return err, vtt
    }


    defer file.Close()
    return this.VisionImgReaderToText(file)


}


type VisionImgToTextRespData struct {
    Text string `json:"text, omitempty"`
}


type VisionImgToTextResp struct {
    Ret  int                     `json:"ret, omitempty"`
    Msg  string                  `json:"msg, omitempty"`
    Data VisionImgToTextRespData `json:"data, omitempty"`
}


func (this *AiClient) VisionImgReaderToText(img io.Reader) (error, VisionImgToTextResp) {
    var vtt VisionImgToTextResp
    buffer := &bytes.Buffer{}
    _, err := io.Copy(buffer, img)
    if err != nil {
        return err, vtt
    }


    data := make(map[string]string)
    data["app_id"] = this.Appid
    data["time_stamp"] = fmt.Sprintf("%d", time.Now().Unix())
    data["nonce_str"] = GetRandomString(30)
    data["session_id"] = "100"
    data["image"] = string(base64.StdEncoding.EncodeToString(buffer.Bytes()))


    reqbody := this.GetBody(data)
    //fmt.Println("body is:", reqbody, "image len:", len(data["image"]), " time:", data["time_stamp"], ",now:", time.Now())
    resp, err := this.Post(VISION_IMGTOTEXT_URI, reqbody)
    if err != nil {
        return err, vtt
    }


    json.NewDecoder(resp.Body).Decode(&vtt)
    return nil, vtt
}

单元测试

// vision_test.go 
package tencentai


import (
    "fmt"
    "testing"
)


func TestVisionImgFileToText(t *testing.T) {


    client := NewAiClient("your-appid", "your-appkey")


    err, vtt := client.VisionImgFileToText("./fm_big.jpg")
    if err != nil {
        t.Fatalf("Expect success but return:", err)
    } else {
        if vtt.Ret != 0 {
            t.Fatalf("Expect ret is 0 but%v msg:%s", vtt.Ret, vtt.Msg)
        } else {
            fmt.Println("text:", vtt.Data.Text)
        }
    }
}


测试结果

[root@localhost tencentai]# 
[root@localhost tencentai]# go test
text: 海 面 上 一 艘 帆 船 的 照 片
PASS
......

测试过程中遇到的问题

用来测试的虚拟机初始安装时,选择的时区有问题

[root@localhost ~]# date 
Wed Dec  6 09:42:34 EST 2017

所以在最初测试时返回了请求超时的错误

--- FAIL: TestVisionImgFileToText (0.78s)
	vision_test.go:17: Expect ret is 0 but 16390 msg:req expired
FAIL
exit status 1

修改机器时区设置

[root@localhost ~]# tzselect 
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
 1) Afghanistan		  18) Israel		    35) Palestine
 2) Armenia		  19) Japan		    36) Philippines
 3) Azerbaijan		  20) Jordan		    37) Qatar
 4) Bahrain		  21) Kazakhstan	    38) Russia
 5) Bangladesh		  22) Korea (North)	    39) Saudi Arabia
 6) Bhutan		  23) Korea (South)	    40) Singapore
 7) Brunei		  24) Kuwait		    41) Sri Lanka
 8) Cambodia		  25) Kyrgyzstan	    42) Syria
 9) China		  26) Laos		    43) Taiwan
10) Cyprus		  27) Lebanon		    44) Tajikistan
11) East Timor		  28) Macau		    45) Thailand
12) Georgia		  29) Malaysia		    46) Turkmenistan
13) Hong Kong		  30) Mongolia		    47) United Arab Emirates
14) India		  31) Myanmar (Burma)	    48) Uzbekistan
15) Indonesia		  32) Nepal		    49) Vietnam
16) Iran		  33) Oman		    50) Yemen
17) Iraq		  34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1


The following information has been given:


	China
	Beijing Time


Therefore TZ='Asia/Shanghai' will be used.
Local time is now:	Wed Dec  6 22:43:05 CST 2017.
Universal Time is now:	Wed Dec  6 14:43:05 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1


You can make this change permanent for yourself by appending the line
	TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.


Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

修改profile中的时区设置,并重新引入

[root@localhost ~]# vim /etc/profile
......
TZ='Asia/Shanghai'; export TZ


[root@localhost ~]# . /etc/profile

再次查看时间

[root@localhost ~]# date
Wed Dec  6 11:09:59 CST 2017

参考资料

接口文档

手把手教你实现人脸识别支付系统

声明:转载本博文章须在文章明显处注明作者及附上原文链接,便于读者找到原文的更新版。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值