def recognize(self, path, lang="zh-CN"):
if isinstance(path, str):
file = open(path, 'rb')
else:
return ["ERROR!", "File must by a path string."]
if lang not in self.lang_list:
return ["ERROR!", "Invalid language."]
audio = pydub.AudioSegment.from_file(file)
audio = audio.set_frame_rate(16000)
audio.export("%s.wav" % path, format="wav")
header = {
"Authorization": "Bearer %s" % self.access_token,
"Content-Type": "audio/wav; samplerate=16000"
}
d = {
"version": "3.0",
"requestid": str(uuid.uuid1()),
"appID": "D4D52672-91D7-4C74-8AD8-42B1D98141A5",
"format": "json",
"locale": lang,
"device.os": "Telegram",
"scenarios": "ulm",
"instanceid": uuid.uuid3(uuid.NAMESPACE_DNS, 'com.1a23.eh_telegram_master'),
"maxnbest": 5
}
with open("%s.wav" % path, 'rb') as f:
r = requests.post("https://speech.platform.bing.com/recognize", params=d, data=f.read(), headers=header)
os.remove("%s.wav" % path)
try:
rjson = r.json()
except:
return ["ERROR!", r.text]
if r.status_code == 200:
return [i['name'] for i in rjson['results']]
else:
return ["ERROR!", r.text]
评论列表
文章目录