如何解决并行运行
我正在尝试使用语音识别模块制作聊天机器人,并且将以下几行代码放在一起:
def recognize_speech_from_mic(recognizer,microphone):
"""Transcribe speech from recorded from `microphone`.
Returns a dictionary with three keys:
"success": a boolean indicating whether or not the API request was
successful
"error": `None` if no error occured,otherwise a string containing
an error message if the API could not be reached or
speech was unrecognizable
"transcription": `None` if speech could not be transcribed,otherwise a string containing the transcribed text
"""
# check that recognizer and microphone arguments are appropriate type
if not isinstance(recognizer,sr.Recognizer):
raise TypeError("`recognizer` must be `Recognizer` instance")
if not isinstance(microphone,sr.Microphone):
raise TypeError("`microphone` must be `Microphone` instance")
# adjust the recognizer sensitivity to ambient noise and record audio
# from the microphone
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
recognizer.pause_threshold = 1
audio = recognizer.listen(source)
# set up the response object
response = {
"success": True,"error": None,"transcription": None
}
# try recognizing the speech in the recording
# if a RequestError or UnknownValueError exception is caught,# update the response object accordingly
try:
response["transcription"] = recognizer.recognize_google(audio,language='pt-PT')
except sr.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
# speech was unintelligible
response["error"] = "Unable to recognize speech"
return response
clip_1.preview() #1
result = recognize_speech_from_mic(recognizer,microphone) #2
clip_7.preview() #3
print(result) #4
我的问题是,我希望第2行(结果= ognize_speech_from_mic)与上一条(clip_1.preview())同时运行。该剪辑是在PyGame窗口上运行的mp4剪辑。
提前谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。