Must-have skills for Golang developers: Easily connect to Baidu AI interface to achieve speech recognition
Introduction: With the rapid development of artificial intelligence, speech recognition technology is gradually penetrating into In our lives, it has become one of the important ways of our daily communication and interaction. As a Golang developer, knowing how to connect to Baidu AI interface for speech recognition will add a lot of convenience to our application development. This article will lead readers to understand how to use Golang to easily connect to Baidu AI interface to achieve speech recognition, and attaches code examples.
go get -u github.com/go-resty/resty/v2 go get -u github.com/json-iterator/go
speech_recognition. go
, write the following code in the file: package main import ( "fmt" "io/ioutil" "net/http" "os" "strings" "github.com/go-resty/resty/v2" "github.com/json-iterator/go" ) const ( TokenURL = "https://aip.baidubce.com/oauth/2.0/token" APIURL = "http://vop.baidu.com/server_api" APIKey = "your_api_key" // 替换成你的API Key SecretKey = "your_secret_key" // 替换成你的Secret Key AudioFile = "audio.wav" // 替换成你的音频文件路径 DevUserID = "user01" // 替换成你的用户标识 ) type TokenResponse struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` } type RecognitionResult struct { ErrNo int `json:"err_no"` ErrMsg string `json:"err_msg"` Result []string `json:"result"` } func main() { accessToken := getAccessToken() audioData, err := ioutil.ReadFile(AudioFile) if err != nil { fmt.Printf("读取音频文件失败:%s ", err.Error()) os.Exit(1) } boundary := "12345678901234567890" body := fmt.Sprintf("--%s Content-Disposition: form-data; name="dev_pid" 1537 --%s Content-Disposition: form-data; name="format" wav --%s Content-Disposition: form-data; name="channel" 1 --%s Content-Disposition: form-data; name="token" %s --%s Content-Disposition: form-data; name="cuid" %s --%s Content-Disposition: form-data; name="len" %d --%s Content-Disposition: form-data; name="speech"; filename="%s" Content-Type: application/octet-stream %s --%s--", boundary, boundary, boundary, boundary, accessToken, boundary, DevUserID, boundary, len(audioData), AudioFile, audioData, boundary) resp, err := resty.New().R(). SetHeader("Content-Type", "multipart/form-data; boundary="+boundary). SetBody(body). Post(APIURL) if err != nil { fmt.Printf("请求百度AI接口失败:%s ", err.Error()) os.Exit(1) } result := RecognitionResult{} if err := jsoniter.Unmarshal(resp.Body(), &result); err != nil { fmt.Printf("解析返回结果失败:%s ", err.Error()) os.Exit(1) } if result.ErrNo != 0 { fmt.Printf("识别失败:%s ", result.ErrMsg) } else { text := strings.Join(result.Result, "") fmt.Printf("识别结果:%s ", text) } } func getAccessToken() string { resp, err := resty.New().R(). SetQueryParams(map[string]string{ "grant_type": "client_credentials", "client_id": APIKey, "client_secret": SecretKey, }). Get(TokenURL) if err != nil { fmt.Printf("获取百度AI接口Token失败:%s ", err.Error()) os.Exit(1) } token := TokenResponse{} if err := jsoniter.Unmarshal(resp.Body(), &token); err != nil { fmt.Printf("解析Token失败:%s ", err.Error()) os.Exit(1) } return token.AccessToken }
go build speech_recognition.go ./speech_recognition
Summary: This article introduces how to use Golang to easily connect to Baidu AI interface to achieve speech recognition, and provides corresponding code examples. By mastering this skill, Golang developers can use Baidu AI interface to develop speech recognition applications more flexibly and conveniently. I hope this article can provide some help and inspiration to Golang developers in implementing speech recognition functions.
The above is the detailed content of Essential skills for Golang developers: Easily connect to Baidu AI interface to achieve speech recognition. For more information, please follow other related articles on the PHP Chinese website!