You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SpeechRecognition package (need to install by pip install SpeechRecognition)
pyaudio package (need to install by pip install pyaudio)
baidu-aip package(need to install by pip install baidu-aip)
A reference code
importspeech_recognitionassrfromaipimportAipSpeech# Please signup baidu ASR service: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)
# baidu asr servicedefasr(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,
})
ifres['err_no'] ==0:
return''.join(res['result'])
else:
return''defrecognize_speech_from_mic(recognizer, microphone):
''' Transcribe speech from recorded from `microphone`. :param recognizer: :param microphone: :return: `None` if speech could not be transcribed, otherwise a string containing the transcribed text '''print('Please read the English sentence')
# adjust the recognizer sensitivity to ambient noise and record audio# from the microphonewithmicrophoneassource:
recognizer.adjust_for_ambient_noise(source)
audio=recognizer.listen(source)
# try recognizing the speech in the recordingtry:
text=asr(audio)
exceptExceptionase:
print(e)
text=Nonereturntextif__name__=='__main__':
# input a English word or sentencetext=input('Please input a English word or sentence: ').strip()
# create recognizer and mic instancesrecognizer=sr.Recognizer()
microphone=sr.Microphone()
# get your speech textspeech_text=recognize_speech_from_mic(recognizer, microphone)
whilespeech_text!=Noneandtext.lower() !=speech_text.lower():
print('{} ×'.format(speech_text))
# get your speech textspeech_text=recognize_speech_from_mic(recognizer, microphone)
ifspeech_text:
print('{} {}'.format(speech_text, '✓'))
else:
print('Please try the speech recognization service later or change another one.')
Run the demo
use pip install requirements.txt to install packages: pyaudio,baidu-aip and SpeechRecognition