You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.3 KiB

package ai_model_cli
import (
"bytes"
"encoding/base64"
"encoding/json"
"goskeleton/app/global/my_errors"
"goskeleton/app/global/variable"
"goskeleton/app/utils/baidubce"
"net/http"
"github.com/gin-gonic/gin"
)
type BaiduBCEVOP struct {
Format string `json:"format"`
Rate int `json:"rate"`
Channel int `json:"channel"`
Cuid string `json:"cuid"`
DevPid int `json:"dev_pid"`
Speech string `json:"speech"`
Len int `json:"len"`
Token string `json:"token"`
}
/**
* ocr(),
* @return
*/
func RequestVOP(context *gin.Context) (r bool, c interface{}) {
path := "https://vop.baidu.com/pro_api"
// 获得语音数据,需要 base64 和urlencode处理
// TODO:linlnf
content := context.PostForm("voc")
// 再进行 Base64 解码
decodedVoc, _ := base64.StdEncoding.DecodeString(content)
// 组装 body 数据参数在该网址https://cloud.baidu.com/doc/SPEECH/s/4lbxdz34z
jsonData, _ := json.Marshal(BaiduBCEVOP{
Format: "pcm",
Rate: 16000,
Channel: 1,
Cuid: "kRxqyFGTgJOtBU4b5LnhWEvX6g8EU4Z7",
DevPid: 80001,
Speech: content,
Len: len(decodedVoc),
Token: baidubce.GetAccessToken(),
})
client := &http.Client{}
// 请求数据
req, err := http.NewRequest("POST", path, bytes.NewBuffer(jsonData))
if err != nil {
variable.ZapLog.Error(my_errors.ErrorBaiduCEUseVOPFail + err.Error())
return false, nil
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
variable.ZapLog.Error(my_errors.ErrorBaiduCEUseVOPFail + err.Error())
return false, nil
}
defer res.Body.Close()
//从res中提取识别出的信息
mbody, err := baidubce.DecodeResBody(res, "vop")
if err != nil {
variable.ZapLog.Error(my_errors.ErrorBaiduCEUseVOPFail + err.Error())
return false, nil
}
return true, gin.H{
"words": decodeBody2Str(mbody),
}
}
/*
*
* @return
*/
func decodeBody2Str(mbody map[string]interface{}) (str string) {
words, ok := mbody["result"].([]interface{})
if ok {
for _, word := range words {
str += word.(string) + "\n"
}
return str
}
return ""
}