- Python : 3.8.13
- OS : Windows 21H1
- Conda : 4.12.0
- PyCharm : 2022.1 (Community Edition)
代码及报错
import speech_recognition as sr
audio_file = 'test.wav'
r = sr.Recognizer()
# 打开语音文件
with sr.AudioFile(audio_file) as source:
audio = r.record(source)
print('文本内容: ', r.recognize_sphinx(audio, language='zh_CN')) # 汉语
D:\Develop\Anaconda3\envs\pyqtenv\python.exe C:/Users/admin/Desktop/pythonProject/main.py
Traceback (most recent call last):
File "D:\Develop\Anaconda3\envs\pyqtenv\lib\site-packages\speech_recognition\__init__.py", line 729, in recognize_sphinx
from pocketsphinx import pocketsphinx, Jsgf, FsgModel
ModuleNotFoundError: No module named 'pocketsphinx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/admin/Desktop/pythonProject/main.py", line 10, in <module>
print('文本内容: ', r.recognize_sphinx(audio, language='zh_CN')) # 汉语
File "D:\Develop\Anaconda3\envs\pyqtenv\lib\site-packages\speech_recognition\__init__.py", line 732, in recognize_sphinx
raise RequestError("missing PocketSphinx module: ensure that PocketSphinx is set up correctly.")
speech_recognition.RequestError: missing PocketSphinx module: ensure that PocketSphinx is set up correctly.
Process finished with exit code 1
解决方法
- 报错的核心内容
speech_recognition.RequestError: missing PocketSphinx module: ensure that PocketSphinx is set up correctly.
- pip安装pocketsphinx
C:\Users\admin>conda activate pyqtenv
(pyqtenv) C:\Users\admin>pip install pocketsphinx
Collecting pocketsphinx
Using cached pocketsphinx-0.1.15.tar.gz (29.1 MB)
Building wheels for collected packages: pocketsphinx
Building wheel for pocketsphinx (setup.py) ... done
Created wheel for pocketsphinx: filename=pocketsphinx-0.1.15-cp38-cp38-win_amd64.whl size=29109817 sha256=64f81b8e45fc834dcb9c540fd12d6e413a0dc0e485e42ad8d7cad60078750055
Stored in directory: c:\users\admin\appdata\local\pip\cache\wheels\30\98\7e\529701aa2fd808ce345ae23f1fdd8f4bb0aa42ffac8ba058ff
Successfully built pocketsphinx
Installing collected packages: pocketsphinx
Successfully installed pocketsphinx-0.1.15
(pyqtenv) C:\Users\admin>
- pip安装中、再次运行代码时,会遇到各种报错,以下方法仅供参考
- Conda error command swig.exe failed None
- Python error Microsoft Visual C++ 14.0 or greater is required
- Python speech_recognition missing PocketSphinx language data directory zh-cn
学习推荐
Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。