|
| 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 | + |
| 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 | + |
| 181 | + |
0 commit comments