-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_example.go
189 lines (167 loc) · 5.27 KB
/
client_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"time"
"github.com/nl8590687/asrt-sdk-go/common"
"github.com/nl8590687/asrt-sdk-go/sdk"
)
func main() {
httpDemo()
grpcDemo()
}
func httpDemo() {
// 初始化
host := "127.0.0.1"
port := "20001"
protocol := "http"
sr := sdk.GetSpeechRecognizer(host, port, protocol)
// ======================================================
// 识别文件
filename := "testData/data1.wav"
resultFile, err := sr.RecogniteFile(filename)
if err != nil {
fmt.Println(err)
}
for index, res := range resultFile {
fmt.Println("Wav文件语音识别结果 ", index, ":", res.Result)
}
byteData := sdk.LoadFile(filename)
wave, err := sdk.DecodeWav(byteData)
if err != nil {
fmt.Println(err)
}
// ======================================================
// 识别一段Wave音频序列
result, err := sr.Recognite(wave.GetRawSamples(), wave.FrameRate, wave.Channels, wave.SampleWidth)
if err != nil {
fmt.Println(err)
}
fmt.Println("语音识别结果:", result.Result)
// ======================================================
// 调用声学模型识别一段Wave音频序列
result, err = sr.RecogniteSpeech(wave.GetRawSamples(), wave.FrameRate, wave.Channels, wave.SampleWidth)
if err != nil {
fmt.Println(err)
}
fmt.Println("语音识别声学模型结果:", result.Result)
// ======================================================
// 调用语言模型1
pinyinResult := []string{}
for i := 0; i < len(result.Result.([]interface{})); i += 1 {
pinyinResult = append(pinyinResult, result.Result.([]interface{})[i].(string))
}
result, err = sr.RecogniteLanguage(pinyinResult)
if err != nil {
fmt.Println(err)
}
fmt.Println("语言模型结果:", result.Result)
// ======================================================
// 调用语言模型2
sequencePinyin := []string{"ni3", "hao3", "a1"}
result, err = sr.RecogniteLanguage(sequencePinyin)
if err != nil {
fmt.Println(err)
}
fmt.Println("语言模型结果:", result.Result)
}
func grpcDemo() {
// 初始化
host := "127.0.0.1"
port := "20002"
protocol := "grpc"
sr := sdk.GetSpeechRecognizer(host, port, protocol)
fmt.Println("sr:", sr)
// ======================================================
// 识别文件
filename := "testData/data1.wav"
resultFile, err := sr.RecogniteFile(filename)
if err != nil {
fmt.Println(err)
}
for index, res := range resultFile {
fmt.Println("Wav文件语音识别结果 ", index, ":", res.Result)
}
byteData := sdk.LoadFile(filename)
wave, err := sdk.DecodeWav(byteData)
if err != nil {
fmt.Println(err)
}
// ======================================================
// 识别一段Wave音频序列
result, err := sr.Recognite(wave.GetRawSamples(), wave.FrameRate, wave.Channels, wave.SampleWidth)
if err != nil {
fmt.Println(err)
}
fmt.Println("语音识别结果:", result.Result)
// ======================================================
// 识别一段长Wave音频序列
longSample := wave.GetRawSamples()
longSample = append(longSample, wave.GetRawSamples()...)
resultLong, err := sr.RecogniteLong(longSample, wave.FrameRate, wave.Channels, wave.SampleWidth)
if err != nil {
fmt.Println(err)
}
for index, res := range resultLong {
fmt.Println("长文件语音识别结果 ", index, ":", res.Result)
}
// ======================================================
// 调用声学模型识别一段Wave音频序列
result, err = sr.RecogniteSpeech(wave.GetRawSamples(), wave.FrameRate, wave.Channels, wave.SampleWidth)
if err != nil {
fmt.Println(err)
}
fmt.Println("语音识别声学模型结果:", result.Result)
// ======================================================
// 调用语言模型1
pinyinResult := []string{}
for i := 0; i < len(result.Result.([]string)); i += 1 {
pinyinResult = append(pinyinResult, result.Result.([]string)[i])
}
result, err = sr.RecogniteLanguage(pinyinResult)
if err != nil {
fmt.Println(err)
}
fmt.Println("语言模型结果:", result.Result)
// ======================================================
// 调用语言模型2
sequencePinyin := []string{"ni3", "hao3", "a1"}
result, err = sr.RecogniteLanguage(sequencePinyin)
if err != nil {
fmt.Println(err)
}
fmt.Println("语言模型结果:", result.Result)
// ======================================================
// 调用ASRT grpc接口流式识别
wavChannel := make(chan *common.Wav, 5)
recognitionResult := make(chan *common.AsrtAPIResponse, 5)
sendFunction := func() {
var index int
for index = 0; index < 10; index += 1 {
time.Sleep(2 * time.Second)
wavChannel <- wave
}
close(wavChannel)
}
go sendFunction()
var asrResult string
var tmpAsrResult string
recvFunction := func() {
for value := range recognitionResult {
fmt.Println("流式解码结果:", value.StatusCode, value.Result, value.StatucMesaage)
if value.StatusCode == common.APIStatusCodeOK {
tmpAsrResult = ""
asrResult += value.Result.(string)
} else if value.StatusCode == common.APIStatusCodePartOK {
tmpAsrResult = value.Result.(string)
}
fmt.Println("语音识别文本:", asrResult+tmpAsrResult)
}
}
go recvFunction()
err = sr.(*sdk.GRPCSpeechRecognizer).RecogniteStream(wavChannel, recognitionResult)
fmt.Println("流式识别完毕")
if err != nil {
fmt.Println(err)
}
close(recognitionResult)
}