Skip to content

Commit b4ce438

Browse files
committed
add challenge 5: Check your English speech using Baidu AI cloud ASR service
1 parent eb6a1e1 commit b4ce438

6 files changed

+287
-0
lines changed
+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# Check your English speech using Baidu AI cloud ASR service
2+
3+
## Requirements
4+
5+
1. Run the code in console using command line.
6+
2. It'll let you input a English word or sentence, then it'll let you speak it.
7+
3. It'll record your voice using mic and check if you speak correctly. If not it'll ask you speak it again until you speak it correctly.
8+
4. It''ll will use Baidu AI Cloud ASR service: https://ai.baidu.com/tech/speech/asr
9+
10+
## What will we practice in this project?
11+
12+
- for loop
13+
- input text
14+
- Text lower case
15+
- if conditions
16+
- functions
17+
- exception handle
18+
- SpeechRecognition package (need to install by `pip install SpeechRecognition`)
19+
- pyaudio package (need to install by `pip install pyaudio`)
20+
- baidu-aip package(need to install by `pip install baidu-aip`)
21+
22+
## A reference code
23+
24+
```python
25+
import speech_recognition as sr
26+
from aip import AipSpeech
27+
28+
29+
# Please signup baidu ASR service:https://ai.baidu.com/tech/speech/asr
30+
VOICE_APP_ID = 'YOUR_ASR_APP_ID'
31+
VOICE_API_KEY = 'YOUR_ASR_APP_KEY'
32+
VOICE_SECRET_KEY = 'YOUR_ASR_SECRET_KEY'
33+
voice_client = AipSpeech(VOICE_APP_ID, VOICE_API_KEY, VOICE_SECRET_KEY)
34+
35+
36+
# baidu asr service
37+
def asr(audio_data):
38+
wav_data = audio_data.get_wav_data(
39+
convert_rate=16000,
40+
convert_width=2
41+
)
42+
res = voice_client.asr(wav_data, 'wav', 16000, {
43+
'dev_pid': 1737,
44+
})
45+
if res['err_no'] == 0:
46+
return ''.join(res['result'])
47+
else:
48+
return ''
49+
50+
51+
def recognize_speech_from_mic(recognizer, microphone):
52+
'''
53+
Transcribe speech from recorded from `microphone`.
54+
:param recognizer:
55+
:param microphone:
56+
:return: `None` if speech could not be transcribed, otherwise a string containing the transcribed text
57+
'''
58+
print('Please read the English sentence')
59+
# adjust the recognizer sensitivity to ambient noise and record audio
60+
# from the microphone
61+
with microphone as source:
62+
recognizer.adjust_for_ambient_noise(source)
63+
audio = recognizer.listen(source)
64+
65+
# try recognizing the speech in the recording
66+
try:
67+
text = asr(audio)
68+
except Exception as e:
69+
print(e)
70+
text = None
71+
72+
return text
73+
74+
75+
if __name__ == '__main__':
76+
# input a English word or sentence
77+
text = input('Please input a English word or sentence: ').strip()
78+
79+
# create recognizer and mic instances
80+
recognizer = sr.Recognizer()
81+
microphone = sr.Microphone()
82+
83+
# get your speech text
84+
speech_text = recognize_speech_from_mic(recognizer, microphone)
85+
86+
while speech_text != None and text.lower() != speech_text.lower():
87+
print('{} ×'.format(speech_text))
88+
# get your speech text
89+
speech_text = recognize_speech_from_mic(recognizer, microphone)
90+
91+
if speech_text:
92+
print('{} {}'.format(speech_text, ''))
93+
else:
94+
print('Please try the speech recognization service later or change another one.')
95+
96+
```
97+
98+
## Run the demo
99+
100+
- use `pip install requirements.txt` to install packages: `pyaudio`,`baidu-aip` and `SpeechRecognition`
101+
- run it in console
102+
103+
```shell
104+
python 5.py
105+
```
106+
107+
![](images/challenge_5_en.png)
108+
109+
----
110+
111+
112+
# 检测英语口语(使用百度云语音识别)
113+
114+
## 项目需求
115+
116+
1. 在命令行窗口运行;
117+
2. 程序运行时,会让你输入一句英语,然后你需要对着麦克风读出这句英语;
118+
3. 程序会判断你读的对不对,如果不对会让你重读,直到读对为止;
119+
4. 使用百度云语音识别:https://ai.baidu.com/tech/speech/asr。
120+
121+
## Python编程知识点
122+
123+
- while循环
124+
- 用户输入字符串
125+
- 字符串小写
126+
- 条件判断
127+
- 自定义函数
128+
- 异常处理
129+
- SpeechRecognition 模块 (安装: `pip install SpeechRecognition`)
130+
- pyaudio 模块 (安装: `pip install pyaudio`)
131+
- baidu-aip 模块(安装: `pip install baidu-aip`)
132+
133+
## 参考代码
134+
135+
```python
136+
import speech_recognition as sr
137+
from aip import AipSpeech
138+
139+
# 请自己注册百度云语音识别:https://ai.baidu.com/tech/speech/asr
140+
VOICE_APP_ID = 'YOUR_ASR_APP_ID'
141+
VOICE_API_KEY = 'YOUR_ASR_APP_KEY'
142+
VOICE_SECRET_KEY = 'YOUR_ASR_SECRET_KEY'
143+
voice_client = AipSpeech(VOICE_APP_ID, VOICE_API_KEY, VOICE_SECRET_KEY)
144+
145+
146+
# 百度云语音识别
147+
def asr(audio_data):
148+
wav_data = audio_data.get_wav_data(
149+
convert_rate=16000,
150+
convert_width=2
151+
)
152+
res = voice_client.asr(wav_data, 'wav', 16000, {
153+
'dev_pid': 1737,
154+
})
155+
if res['err_no'] == 0:
156+
return ''.join(res['result'])
157+
else:
158+
return ''
159+
160+
161+
def recognize_speech_from_mic(recognizer, microphone):
162+
'''
163+
麦克风录音并转文字 `microphone`.
164+
:param recognizer: 语音识别器
165+
:param microphone: 麦克风
166+
:return: `None` 如果识别失败返回None,否则返回语音文字
167+
'''
168+
print('开始朗读')
169+
# 录音并去除噪音
170+
with microphone as source:
171+
recognizer.adjust_for_ambient_noise(source)
172+
audio = recognizer.listen(source)
173+
174+
# 使用百度云语音识别
175+
try:
176+
text = asr(audio)
177+
except Exception as e:
178+
print(e)
179+
text = None
180+
181+
return text
182+
183+
184+
if __name__ == '__main__':
185+
# 输入
186+
text = input('请输入一句英语: ').strip()
187+
188+
# 创建语音识别器和麦克风
189+
recognizer = sr.Recognizer()
190+
microphone = sr.Microphone()
191+
192+
# 录音并获取文字
193+
speech_text = recognize_speech_from_mic(recognizer, microphone)
194+
195+
while speech_text != None and text.lower() != speech_text.lower():
196+
print('{} ×'.format(speech_text))
197+
speech_text = recognize_speech_from_mic(recognizer, microphone)
198+
199+
if speech_text:
200+
print('{} {}'.format(speech_text, ''))
201+
else:
202+
print('语音识别服务暂不可用,请稍后再试。')
203+
204+
```
205+
## 运行测试
206+
207+
- 使用 `pip install requirements.txt` 安装模块: `pyaudio``SpeechRecognition``baidu-aip`
208+
- 运行
209+
210+
```shell
211+
python 5.py
212+
```
213+
214+
![](images/challenge_5_cn.png)
215+

code/5/5.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import speech_recognition as sr
2+
from aip import AipSpeech
3+
4+
# Please signup baidu ASR service:https://ai.baidu.com/tech/speech/asr
5+
VOICE_APP_ID = 'YOUR_ASR_APP_ID'
6+
VOICE_API_KEY = 'YOUR_ASR_APP_KEY'
7+
VOICE_SECRET_KEY = 'YOUR_ASR_SECRET_KEY'
8+
voice_client = AipSpeech(VOICE_APP_ID, VOICE_API_KEY, VOICE_SECRET_KEY)
9+
10+
11+
# baidu asr service
12+
def asr(audio_data):
13+
wav_data = audio_data.get_wav_data(
14+
convert_rate=16000,
15+
convert_width=2
16+
)
17+
res = voice_client.asr(wav_data, 'wav', 16000, {
18+
'dev_pid': 1737,
19+
})
20+
if res['err_no'] == 0:
21+
return ''.join(res['result'])
22+
else:
23+
return ''
24+
25+
26+
def recognize_speech_from_mic(recognizer, microphone):
27+
'''
28+
Transcribe speech from recorded from `microphone`.
29+
:param recognizer:
30+
:param microphone:
31+
:return: `None` if speech could not be transcribed, otherwise a string containing the transcribed text
32+
'''
33+
print('Please read the English sentence')
34+
# adjust the recognizer sensitivity to ambient noise and record audio
35+
# from the microphone
36+
with microphone as source:
37+
recognizer.adjust_for_ambient_noise(source)
38+
audio = recognizer.listen(source)
39+
40+
# try recognizing the speech in the recording
41+
try:
42+
text = asr(audio)
43+
except Exception as e:
44+
print(e)
45+
text = None
46+
47+
return text
48+
49+
50+
if __name__ == '__main__':
51+
# input a English word or sentence
52+
text = input('Please input a English word or sentence: ').strip()
53+
54+
# create recognizer and mic instances
55+
recognizer = sr.Recognizer()
56+
microphone = sr.Microphone()
57+
58+
# get your speech text
59+
speech_text = recognize_speech_from_mic(recognizer, microphone)
60+
61+
while speech_text != None and text.lower() != speech_text.lower():
62+
print('{} ×'.format(speech_text))
63+
# get your speech text
64+
speech_text = recognize_speech_from_mic(recognizer, microphone)
65+
66+
if speech_text:
67+
print('{} {}'.format(speech_text, '✓'))
68+
else:
69+
print('Please try the speech recognization service later or change another one.')

code/5/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
baidu-aip
2+
SpeechRecognition
3+
pyaudio
File renamed without changes.

images/challenge_5_cn.png

1.16 MB
Loading

images/challenge_5_en.png

1.2 MB
Loading

0 commit comments

Comments
 (0)