提示:
SDK说明
录音文件识别的GO Demo使用了阿里云GO SDK的CommonRequest用来提交录音文件识别请求和识别结果查询,采用的是RPC风格的POP API调用。阿里云SDK的详细介绍请阅读阿里云GO SDK的 使用手册, GO SDK CommonRequest的使用方法请阅读 使用CommonRequest进行调用 。
SDK 安装
阿里云Go SDK支持Go 1.7及以上版本,您可以通过以下方式安装Go SDK:
使用Glide(推荐)
glide get github.com/aliyun/alibaba-cloud-sdk-go
使用govendor
执行以下命令,安装Go SDK:
go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk
调用步骤
- 创建并初始化阿里云鉴权client,鉴权使用了阿里云账号的AccessKey ID和AccessKey Secret(获取方法请阅读开通服务一节)。
- 创建录音文件识别请求,并设置请求参数。
- 提交录音文件识别请求,处理服务端返回的响应,获取任务ID。
- 创建识别结果查询请求,设置查询参数为任务ID。
- 轮询识别结果。
Demo 示例
录音文件说明: Demo中使用的录音文件为PCM编码格式16000Hz采样率,管控台设置的模型为通用模型;如果使用其他录音文件,请填入对应的编码格式和采样率,并在管控台设置对应的模型,模型设置请阅读管理项目一节。
nls-sample-16k.wav
阿里云鉴权:使用过程中,所有的调用均通过阿里云账号来完成鉴权操作。通过传入阿里云账号的AccessKey ID和AccessKey Secret(获取方法请阅读 开通服务 一节)。
示例
package main
import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"fmt"
"encoding/json"
"time"
)
func main() {
// 地域ID,常量内容,请勿改变
const REGION_ID string = "ap-southeast-1"
const ENDPOINT_NAME string = "ap-southeast-1"
const PRODUCT string = "nls-filetrans"
const DOMAIN string = "filetrans.ap-southeast-1.aliyuncs.com"
const API_VERSION string = "2019-08-23"
const POST_REQUEST_ACTION string = "SubmitTask"
const GET_REQUEST_ACTION string = "GetTaskResult"
// 请求参数key
const KEY_APP_KEY string = "appkey"
const KEY_FILE_LINK string = "file_link"
const KEY_VERSION string = "version"
const KEY_ENABLE_WORDS string = "enable_words"
// 响应参数key
const KEY_TASK string = "Task"
const KEY_TASK_ID string = "TaskId"
const KEY_STATUS_TEXT string = "StatusText"
const KEY_RESULT string = "Result"
// 状态值
const STATUS_SUCCESS string = "SUCCESS"
const STATUS_RUNNING string = "RUNNING"
const STATUS_QUEUEING string = "QUEUEING"
var accessKeyId string = "您的accessKeyId"
var accessKeySecret string = "您的accessKeySecret"
var appKey string = "您的appkey"
var fileLink string = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
client, err := sdk.NewClientWithAccessKey(REGION_ID, accessKeyId, accessKeySecret)
if err != nil {
panic(err)
}
postRequest := requests.NewCommonRequest()
postRequest.Domain = DOMAIN
postRequest.Version = API_VERSION
postRequest.Product = PRODUCT
postRequest.ApiName = POST_REQUEST_ACTION
postRequest.Method = "POST"
mapTask := make(map[string]string)
mapTask[KEY_APP_KEY] = appKey
mapTask[KEY_FILE_LINK] = fileLink
// 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置
mapTask[KEY_VERSION] = "4.0"
// 设置是否输出词信息,默认为false,开启时需要设置version为4.0
mapTask[KEY_ENABLE_WORDS] = "false"
task, err := json.Marshal(mapTask)
if err != nil {
panic(err)
}
postRequest.FormParams[KEY_TASK] = string(task)
postResponse, err := client.ProcessCommonRequest(postRequest)
if err != nil {
panic(err)
}
postResponseContent := postResponse.GetHttpContentString()
fmt.Println(postResponseContent)
if (postResponse.GetHttpStatus() != 200) {
fmt.Println("录音文件识别请求失败,Http错误码: ", postResponse.GetHttpStatus())
return
}
var postMapResult map[string]interface{}
err = json.Unmarshal([]byte(postResponseContent), &postMapResult)
if err != nil {
panic(err)
}
var taskId string = ""
var statusText string = ""
statusText = postMapResult[KEY_STATUS_TEXT].(string)
if statusText == STATUS_SUCCESS {
fmt.Println("录音文件识别请求成功响应!")
taskId = postMapResult[KEY_TASK_ID].(string)
} else {
fmt.Println("录音文件识别请求失败!")
return
}
getRequest := requests.NewCommonRequest()
getRequest.Domain = DOMAIN
getRequest.Version = API_VERSION
getRequest.Product = PRODUCT
getRequest.ApiName = GET_REQUEST_ACTION
getRequest.Method = "GET"
getRequest.QueryParams[KEY_TASK_ID] = taskId
statusText = ""
for true {
getResponse, err := client.ProcessCommonRequest(getRequest)
if err != nil {
panic(err)
}
getResponseContent := getResponse.GetHttpContentString()
fmt.Println("识别查询结果:", getResponseContent)
if (getResponse.GetHttpStatus() != 200) {
fmt.Println("识别结果查询请求失败,Http错误码:", getResponse.GetHttpStatus())
break
}
var getMapResult map[string]interface{}
err = json.Unmarshal([]byte(getResponseContent), &getMapResult)
if err != nil {
panic(err)
}
statusText = getMapResult[KEY_STATUS_TEXT].(string)
if statusText == STATUS_RUNNING || statusText == STATUS_QUEUEING {
time.Sleep(10 * time.Second)
} else {
break
}
}
if statusText == STATUS_SUCCESS {
fmt.Println("录音文件识别成功!")
} else {
fmt.Println("录音文件识别失败!")
}
}