Skip to content

Commit

Permalink
[voice] fix google voice exception issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wanggang1987 committed Mar 7, 2023
1 parent d38fc61 commit 1711a5c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pip3 install itchat-uos==1.5.0.dev0
pip3 install --upgrade openai

如果使用google的语音识别,需要安装speech_recognition和依赖的ffmpeg
pip3 install speech_recognition
pip3 install SpeechRecognition
--在MacOS中安装ffmpeg,brew install ffmpeg
--在Windows中安装ffmpeg,下载ffmpeg.exe
--在Linux中安装ffmpeg,apt-get install ffmpeg
Expand Down
7 changes: 6 additions & 1 deletion voice/google/google_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ def voiceToText(self, voice_file):
subprocess.call('ffmpeg -i ' + voice_file + ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
with speech_recognition.AudioFile(new_file) as source:
audio = self.recognizer.record(source)
return self.recognizer.recognize_google(audio, language='zh-CN')
try:
return self.recognizer.recognize_google(audio, language='zh-CN')
except speech_recognition.UnknownValueError:
return "抱歉,我听不懂。"
except speech_recognition.RequestError as e:
return "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)

0 comments on commit 1711a5c

Please sign in to comment.