Go 請求兔子識別接口實現(xiàn)流程示例詳解
前言
兔年當(dāng)然要大展宏兔。因為剛好在處理物體識別開發(fā),所以就簡單做下總結(jié)自己使用 Go 實現(xiàn)對兔子圖片的識別的操作。
前期工作
這里主要使用的是曠視Face 的接口來完成這部分的工作。所以只要能獲取接口地址或者SDK的權(quán)限就可以完成工作。簡單的步驟:注冊賬號==> 控制臺中創(chuàng)建應(yīng)用==> 獲取對應(yīng)的 Api Key 和 Secret。詳細(xì)的接口說明可以查看曠視提供的接口文檔console.faceplusplus.com.cn/documents/5…

請求封裝
接口請求方法是 post 請求,請求格式是 multipart/form-data。所以可以對請求進行簡單的封裝。這里先簡單抽出圖片地址方式進行封裝,其他的key 和secret 可以使用配置在方式。
// 二進制文件形式封裝
func RequestFormImgPath(imgPath string) (result []byte, err error) {
url := "https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("api_key", _ConfigKey)
_ = writer.WriteField("api_secret", _ConfigSecret)
var file *os.File
if file, err = os.Open(imgPath); err != nil {
return nil, err
}
defer file.Close()
part3, err := writer.CreateFormFile("image_file", filepath.Base(imgPath))
_, err = io.Copy(part3, file)
if err != nil {
return nil, err
}
if err = writer.Close(); err != nil {
return nil, err
}
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Content-Type", writer.FormDataContentType())
var res *http.Response
if res, err = client.Do(req); err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
這種可使用的場景是,當(dāng)你的圖片存儲在OSS 上或者是你本地的課訪問的圖片地址。當(dāng)然可以進一步封裝請求,可以增加
兩個標(biāo)識請求類型和圖片字符串格式值,然后根據(jù)請求type 讀取不同的值進行請求。
func RequestFormImgPath(imgType int,imgStr string,imgPath string) (result []byte, err error) {
if imgType =1 {
imgage_url =imgStr
}
if imgType =2 {
image_base64 =imgStr
}
if imgType =3 {
image_file =imgPath
}
}
然后就可以根據(jù)返回值進行解析結(jié)果:objects[0].value == "Rabbit" 說明就是我們想要的兔子。
{
"time_used": 1398,
"scenes": [
{
"confidence": 42.366,
"value": "Coast"
}
],
"image_id": "tJw8Sy1jtTJ+9/DBZNSvvQ==",
"objects": [
{
"confidence": 85.478,
"value": "Rabbit"
}
],
"request_id": "1675855779,71f61465-240d-4174-b82b-f98f4b22b082"
}
因為 Go語言沒有類的定義,所以如何反序列化請求接口也很是很重要的問題,因為這就要求你的 response 的結(jié)構(gòu)體要定義好的。以上的 json 格式是當(dāng)你請求成功時的返回結(jié)果,但是當(dāng)請求失敗的時候返回結(jié)果是不一樣的。
所以在設(shè)計的時候可以將錯誤返回的字段也定義在響應(yīng)結(jié)構(gòu)體上,然后根據(jù) error_message 字段是否非空來判斷是否成功與否。
type RabbitResponse struct {
TimeUsed int `json:"time_used"`
ErrorMessage string `json:"error_message"` // 接口錯誤時才有值
Scenes []ScenesDto `json:"scenes"`
ImageId string `json:"image_id"`
Objects []ObjectsDto `json:"objects"`
RequestId string `json:"request_id"`
}
type ScenesDto struct {
Confidence float64 `json:"confidence"`
Value string `json:"value"`
}
type ObjectsDto struct {
Confidence float64 `json:"confidence"`
Value string `json:"value"`
}
這樣就簡單的完成了兔子圖片的識別接口封裝。
以上就是Go 請求兔子識別接口實現(xiàn)流程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Go 請求兔子識別接口的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語言通過chan進行數(shù)據(jù)傳遞的方法詳解
這篇文章主要為大家詳細(xì)介紹了Go語言如何通過chan進行數(shù)據(jù)傳遞的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06
詳解Golang中創(chuàng)建error的方式總結(jié)與應(yīng)用場景
Golang中創(chuàng)建error的方式包括errors.New、fmt.Errorf、自定義實現(xiàn)了error接口的類型等,本文主要為大家介紹了這些方式的具體應(yīng)用場景,需要的可以參考一下2023-07-07
Go語言獲取系統(tǒng)性能數(shù)據(jù)gopsutil庫的操作
這篇文章主要介紹了Go語言獲取系統(tǒng)性能數(shù)據(jù)gopsutil庫的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Go微服務(wù)網(wǎng)關(guān)的實現(xiàn)
本文主要介紹了Go微服務(wù)網(wǎng)關(guān)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

