-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoiceRecognizerApi.py
69 lines (56 loc) · 2.69 KB
/
VoiceRecognizerApi.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
import operator
import os
import tempfile
import requests
import subprocess
from bs4 import BeautifulSoup
import Config
class VoiceRecognizer:
def ask(data, length, uid, lang="auto"):
url = "https://asr.yandex.net/asr_xml?uuid={}&key={}&topic={}&lang={}&disableAntimat={}"
url = url.format(uid, Config.yandex_api, "queries", lang, "true")
headers = {"Content-Type": "audio/x-pcm;bit=16;rate=16000", "Content-Length": str(length)}
resp = requests.post(url, data=data, headers=headers)
dom = BeautifulSoup(resp.text, "html5lib")
result = dict(
(var.string, float(var["confidence"])) for var in dom.html.body.recognitionresults.findAll("variant"))
res1 = max(result.items(), key=operator.itemgetter(1))
url = "https://asr.yandex.net/asr_xml?uuid={}&key={}&topic={}&lang={}&disableAntimat={}"
url = url.format(uid, Config.yandex_api, "queries", 'en', "true")
headers = {"Content-Type": "audio/x-pcm;bit=16;rate=16000", "Content-Length": str(length)}
resp = requests.post(url, data=data, headers=headers)
dom = BeautifulSoup(resp.text, "html5lib")
result = dict(
(var.string, float(var["confidence"])) for var in dom.html.body.recognitionresults.findAll("variant"))
res2 = max(result.items(), key=operator.itemgetter(1))
url = "https://asr.yandex.net/asr_xml?uuid={}&key={}&topic={}&lang={}&disableAntimat={}"
url = url.format(uid, Config.yandex_api, "queries", 'ru', "true")
headers = {"Content-Type": "audio/x-pcm;bit=16;rate=16000", "Content-Length": str(length)}
resp = requests.post(url, data=data, headers=headers)
dom = BeautifulSoup(resp.text, "html5lib")
result = dict(
(var.string, float(var["confidence"])) for var in dom.html.body.recognitionresults.findAll("variant"))
res3 = max(result.items(), key=operator.itemgetter(1))
# print(res1, res2, res3)
return max(res1, res2, res3)[0]
def ogg2pcm(ogg):
with tempfile.TemporaryFile() as temp_out:
name_in = None
if ogg:
temp_in = tempfile.NamedTemporaryFile(delete=False)
temp_in.write(ogg)
name_in = temp_in.name
temp_in.close()
command = [
"ffmpeg",
"-i", name_in,
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '16000',
'-'
]
process = subprocess.Popen(command, stdout=temp_out, stderr=subprocess.DEVNULL)
process.wait()
os.remove(name_in)
temp_out.seek(0)
return temp_out.read()