|
|
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 ""
|
|
|
}
|