当前位置: 首页 > 后端技术 > Python

检测英语口语(使用百度云语音识别)

时间:2023-03-26 12:48:20 Python

项目需要在命令行窗口运行;程序运行时,会要求你输入一句英文,然后你需要对着麦克风读出这句英文;程序会判断你的阅读是否正确,如果不正确,会让你重新阅读,直到正确为止;使用百度云语音识别:https://ai.baidu.com/tech/spe...Python编程知识点while循环用户输入字符串string小写条件判断自定义函数异常处理SpeechRecognition模块(安装:pipinstallSpeechRecognition)pyaudio模块(安装:pipinstallpyaudio)baidu-aip模块(安装:pipinstallbaidu-aip)参考代码importspeech_recognitionassrfromaipimportAipSpeech#请自行注册百度云语音识别:https://ai.baidu.com/tech/speech/asrVOICE_APP_ID='YOUR_ASR_APP_ID'VOICE_API_KEY='YOUR_ASR_APP_KEY'VOICE_SECRET_KEY='YOUR_ASR_SECRET_KEY'voice_client=AipSpeech(VOICE_APP_ID,VOICE_API_KEY,VOICE_SECRET_KEY)#百度云语音识别defasr(audio_data):wav_data=audio_data.get_wav_data(convert_rate=16000,convert_width=2)res=voice_client.asr(wav_data,'wav',16000,{'dev_pid':1737,})如果res['err_no']==0:return''.join(res['result'])else:返回''defrecognize_speech_from_mic(recognizer,microphone):'''麦克风录音和转录`microphone`.:paramrecognizer:Speechrecognizer:parammicrophone:Microphone:return:`None`如果识别失败,返回None,否则返回语音文本'''print('Startreading')#以麦克风为源记录并去除噪音source:recognizer.adjust_for_ambient_noise(source)audio=recognizer.listen(source)#使用百度云语音识别try:text=asr(audio)exceptExceptionase:print(e)text=Nonereturntextif__name__=='__main__':#inputtext=input('PleaseenterasentenceinEnglish:').strip()#创建语音识别器和麦克风recognizer=sr.Recognizer()microphone=sr.Microphone()#录制并获取文本speech_text=recognize_speech_from_mic(recognizer,麦克风)而speech_text!=None和text.lower()!=speech_text.lower():print('{}×'.format(speech_text))speech_text=recognize_speech_from_mic(recognizer,microphone)ifspeech_text:print('{}{}'.format(speech_text,'?'))else:print('语音识别服务暂时不可用,pleasetryagainlater.')RunthetestUsepipinstallrequirements.txt安装模块:pyaudio,SpeechR生态认知,baidu-aip运行python5.py