Skip to content

Commit f6fc6db

Browse files
committed
chanllenge 4: Check your English speech
1 parent 9390672 commit f6fc6db

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed

002_English_Chinese_Translation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ python 2.py
115115
```
116116

117117
![image-20210505171540819](images/challenge2_cn.png)
118+

004_Check_your_English_speech.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Check your English speech
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+
9+
## What will we practice in this project?
10+
11+
- for loop
12+
- input text
13+
- Text lower case
14+
- if conditions
15+
- functions
16+
- exception handle
17+
- SpeechRecognition package (need to install by `pip install SpeechRecognition`) it support these speech recognitions:
18+
- `recognize_bing()`: [Microsoft Bing Speech](https://azure.microsoft.com/en-us/services/cognitive-services/speech/)
19+
- `recognize_google()`: [Google Web Speech API](https://w3c.github.io/speech-api/speechapi.html)
20+
- `recognize_google_cloud()`: [Google Cloud Speech](https://cloud.google.com/speech/) - requires installation of the google-cloud-speech package
21+
- `recognize_houndify()`: [Houndify](https://www.houndify.com/) by SoundHound
22+
- `recognize_ibm()`: [IBM Speech to Text](https://www.ibm.com/watson/services/speech-to-text/)
23+
- `recognize_sphinx()`: [CMU Sphinx](https://cmusphinx.github.io/) - requires installing PocketSphinx
24+
- `recognize_wit()`: [Wit.ai](https://wit.ai/)
25+
- pyaudio package (need to install by `pip install pyaudio`)
26+
27+
## A reference code
28+
29+
```python
30+
import speech_recognition as sr
31+
32+
33+
def recognize_speech_from_mic(recognizer, microphone):
34+
'''
35+
Transcribe speech from recorded from `microphone`.
36+
:param recognizer:
37+
:param microphone:
38+
:return: `None` if speech could not be transcribed, otherwise a string containing the transcribed text
39+
'''
40+
print('Please read the English sentence')
41+
# adjust the recognizer sensitivity to ambient noise and record audio
42+
# from the microphone
43+
with microphone as source:
44+
recognizer.adjust_for_ambient_noise(source)
45+
audio = recognizer.listen(source)
46+
47+
# try recognizing the speech in the recording
48+
try:
49+
text = recognizer.recognize_google(audio)
50+
except Exception as e:
51+
print(e)
52+
text = None
53+
54+
return text
55+
56+
57+
if __name__ == '__main__':
58+
# input a English word or sentence
59+
text = input('Please input a English word or sentence: ').strip()
60+
61+
# create recognizer and mic instances
62+
recognizer = sr.Recognizer()
63+
microphone = sr.Microphone()
64+
65+
# get your speech text
66+
speech_text = recognize_speech_from_mic(recognizer, microphone)
67+
68+
while speech_text != None and text.lower() != speech_text.lower():
69+
print(speech_text)
70+
# get your speech text
71+
speech_text = recognize_speech_from_mic(recognizer, microphone)
72+
73+
if speech_text:
74+
print('{} {}'.format(speech_text, ''))
75+
else:
76+
print('Please try the speech recognization service later or change another one.')
77+
78+
```
79+
80+
## Run the demo
81+
82+
- use `pip install requirements.txt` to install packages: `pyaudio` and `SpeechRecognition`
83+
- run it in console
84+
85+
```shell
86+
python 4.py
87+
```
88+
89+
![](images/challenge_4_en.png)
90+
91+
----
92+
93+
94+
# 检测英语口语
95+
96+
## 项目需求
97+
98+
1. 在命令行窗口运行;
99+
2. 程序运行时,会让你输入一句英语,然后你需要对着麦克风读出这句英语;
100+
3. 程序会判断你读的对不对,如果不对会让你重读,直到读对为止。
101+
102+
## Python编程知识点
103+
104+
- while循环
105+
- 用户输入字符串
106+
- 字符串小写
107+
- 条件判断
108+
- 自定义函数
109+
- 异常处理
110+
- SpeechRecognition 模块 (安装: `pip install SpeechRecognition`) it support these speech recognitions:
111+
- `recognize_bing()`: [Microsoft Bing Speech](https://azure.microsoft.com/en-us/services/cognitive-services/speech/)
112+
- `recognize_google()`: [Google Web Speech API](https://w3c.github.io/speech-api/speechapi.html)
113+
- `recognize_google_cloud()`: [Google Cloud Speech](https://cloud.google.com/speech/) - 需要安装`google-cloud-speech`模块
114+
- `recognize_houndify()`: [Houndify](https://www.houndify.com/) by SoundHound
115+
- `recognize_ibm()`: [IBM Speech to Text](https://www.ibm.com/watson/services/speech-to-text/)
116+
- `recognize_sphinx()`: [CMU Sphinx](https://cmusphinx.github.io/) - 需要安装`PocketSphinx`模块
117+
- `recognize_wit()`: [Wit.ai](https://wit.ai/)
118+
- pyaudio 模块 (安装: `pip install pyaudio`)
119+
120+
## 参考代码
121+
122+
```python
123+
import speech_recognition as sr
124+
125+
126+
def recognize_speech_from_mic(recognizer, microphone):
127+
'''
128+
麦克风录音并转文字 `microphone`.
129+
:param recognizer: 语音识别器
130+
:param microphone: 麦克风
131+
:return: `None` 如果识别失败返回None,否则返回语音文字
132+
'''
133+
print('开始朗读')
134+
# 录音并去除噪音
135+
with microphone as source:
136+
recognizer.adjust_for_ambient_noise(source)
137+
audio = recognizer.listen(source)
138+
139+
# 调用语音识别,亲测微软bing国内可用,国外建议使用google
140+
try:
141+
text = recognizer.recognize_bing(audio)
142+
except Exception as e:
143+
print(e)
144+
text = None
145+
146+
return text
147+
148+
149+
if __name__ == '__main__':
150+
# 输入
151+
text = input('请输入一句英语: ').strip()
152+
153+
# 创建语音识别器和麦克风
154+
recognizer = sr.Recognizer()
155+
microphone = sr.Microphone()
156+
157+
# 录音并获取文字
158+
speech_text = recognize_speech_from_mic(recognizer, microphone)
159+
160+
while speech_text != None and text.lower() != speech_text.lower():
161+
print(speech_text)
162+
speech_text = recognize_speech_from_mic(recognizer, microphone)
163+
164+
if speech_text:
165+
print('{} {}'.format(speech_text, ''))
166+
else:
167+
print('语音识别服务暂不可用,请稍后再试。')
168+
169+
```
170+
171+
## 运行测试
172+
173+
- 使用 `pip install requirements.txt` 安装模块: `pyaudio` and `SpeechRecognition`
174+
- 运行
175+
176+
```shell
177+
python 4.py
178+
```
179+
180+
![image-20210508095608400](images/challenge_4_cn.png)
181+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# 100+ Python Projects Challenge
22

33
## 001 1 minute math
4+
45
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/001_1_minute_math.md
56

67
## 002 Chinese English Translation
8+
79
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/002_English_Chinese_Translation.md
810

911
## 003 Add text watermark on pictures
12+
1013
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/003_Add_text_watermark_on_pictures.md
14+
15+
## 004 Check your English speech
16+
17+
https://github.com/zhiwehu/100_plus_Python_Projects_Challenge/blob/main/004_Check_your_English_speech.md

code/4/4.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import speech_recognition as sr
2+
3+
4+
def recognize_speech_from_mic(recognizer, microphone):
5+
'''
6+
Transcribe speech from recorded from `microphone`.
7+
:param recognizer:
8+
:param microphone:
9+
:return: `None` if speech could not be transcribed, otherwise a string containing the transcribed text
10+
'''
11+
print('Please read the English sentence')
12+
# adjust the recognizer sensitivity to ambient noise and record audio
13+
# from the microphone
14+
with microphone as source:
15+
recognizer.adjust_for_ambient_noise(source)
16+
audio = recognizer.listen(source)
17+
18+
# try recognizing the speech in the recording
19+
try:
20+
text = recognizer.recognize_google(audio)
21+
except Exception as e:
22+
print(e)
23+
text = None
24+
25+
return text
26+
27+
28+
if __name__ == '__main__':
29+
# input a English word or sentence
30+
text = input('Please input a English word or sentence: ').strip()
31+
32+
# create recognizer and mic instances
33+
recognizer = sr.Recognizer()
34+
microphone = sr.Microphone()
35+
36+
# get your speech text
37+
speech_text = recognize_speech_from_mic(recognizer, microphone)
38+
39+
while speech_text != None and text.lower() != speech_text.lower():
40+
print(speech_text)
41+
# get your speech text
42+
speech_text = recognize_speech_from_mic(recognizer, microphone)
43+
44+
if speech_text:
45+
print('{} {}'.format(speech_text, '✓'))
46+
else:
47+
print('Please try the speech recognization service later or change another one.')

code/4/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SpeechRecognition
2+
pyaudio

images/challenge_4_cn.png

508 KB
Loading

images/challenge_4_en.png

465 KB
Loading

0 commit comments

Comments
 (0)