-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmic.py
executable file
·159 lines (145 loc) · 5.33 KB
/
mic.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2017 CPqD. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
@author: valterf
Example with microphone input
"""
from cpqdasr import SpeechRecognizer, LanguageModelList
from cpqdasr import RecognitionException
from cpqdasr import MicAudioSource
from cpqdasr import RecognitionListener
from sys import argv
from sys import stdout, stderr
import os
import getopt
class PrinterListener(RecognitionListener):
def __init__(self, max_char=80, result_parse = False):
self.max_char = max_char
self.result_parse = result_parse
self.last_final = 0
self.print_str = ""
self.result_str = ""
# def on_partial_recognition(self, partial):
# pass
# if self.print_str:
# self.print_str += ' '
# self.print_str += partial.text
# self.print_str = self.print_str[:self.max_char]
# print(self.print_str, end='\r')
# stdout.flush()
def on_recognition_result(self, result):
self.result_str = "{}".format(result)
self.print_str = self.print_str[: self.last_final]
if "alternatives" in result and result["alternatives"]:
alt = result["alternatives"][0]
if "text" in alt:
if self.print_str:
self.print_str += " "
self.print_str += alt["text"]
if len(self.print_str) > self.max_char:
i = self.max_char
while self.print_str[i] != " ":
i -= 1
clear_spaces = " " * (self.max_char - i)
if not self.result_parse:
print(self.print_str[:i] + clear_spaces, end="\n")
self.print_str = self.print_str[i:]
if self.print_str[0] == " ":
self.print_str = self.print_str[1:]
if not self.result_parse:
print(self.print_str, end="\r")
self.last_final = len(self.print_str)
stdout.flush()
def usage():
print(
"Usage: {} -j -w <ws_url> -l <lang_uri> [ -u <user> -p <password> ]\n"
" -j: parse json result".format(argv[0])
)
print(
" eg: {} -w ws://127.0.0.1:8025/asr-server/asr "
"-l builtin:grammar/samples/phone".format(argv[0])
)
print(
" eg2: {} -w wss://contact/cpqd/and/request/a/key/ "
"-l builtin:slm/general myusername mypassword".format(argv[0])
)
exit()
if __name__ == "__main__":
user = ""
password = ""
pars = {}
result_parse = False
try:
opts, args = getopt.getopt(argv[1:], "hjw:l:u:p:v:")
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == "-w":
url = arg
#print("URL= {}".format(url))
elif opt == "-l":
lang_uri_or_path = arg
#print("Language= {}".format(lang_uri_or_path))
elif opt == "-u":
user = arg
print("User {}".format(user))
elif opt == "-v":
v = arg.split("=")
pars[v[0]] = v[1]
elif opt == "-p":
password = arg
print("User {}".format(password))
elif opt == "-j":
result_parse = True
elif opt == "-h":
usage(0)
if os.path.isfile(lang_uri_or_path):
lm = LanguageModelList(
LanguageModelList.grammar_from_path(os.path.basename(lang_uri_or_path), lang_uri_or_path)
)
else:
lm = LanguageModelList(LanguageModelList.from_uri(lang_uri_or_path))
credentials = (user, password)
listener=PrinterListener(result_parse = result_parse)
asr = SpeechRecognizer(
url,
credentials=credentials,
listener=listener,
)
with MicAudioSource() as mic:
try:
asr.recognize(mic, lm, wav=False, config=pars)
result = asr.wait_recognition_result()
if result:
if result[0].result_code == "RECOGNIZED":
if (result_parse):
out = listener.result_str.replace("'", "\"")
out = out.replace("True", "\"True\"")
out = out.replace("False", "\"False\"")
cmd = "echo '{}' | jq --raw-output".format(out)
os.system(cmd)
else:
print(result[0].alternatives[0]["text"])
else:
print(result[0].result_code)
except KeyboardInterrupt:
print("Caught interrupt. Closing ASR instance...", file=stderr)
try:
asr.cancel_recognition()
except RecognitionException:
pass # Ignores exceptions on canceling
asr.close()