语音识别不限制文件
我知道这可能是一件容易的事,但似乎没有用。我正在尝试创建一个简单的“语音到文本”脚本,并且我正在使用库“ SpeechRevention”。这是代码:
import speech_recognition as sr
import os
r = sr.Recognizer()
diR = input('Path with the file: ')
rec = ''
for e in os.listdir(diR):
if '.wav' in e or '.mp4' in e:
rec = e
print('the file is: ',rec)
# try:
with sr.AudioFile(rec) as source:
audio_text = r.listen(source)
try:
# using google speech recognition
text = r.recognize_google(audio_text, language ="it-IT")
print('Converting audio transcripts into text ...')
print(text)
except:
print("A problem occured ... Try again")
# except FileNotFoundError:
# print('no file found')
但是,当我运行te软件时(没有第一次尝试,除外),它给了我这个错误:
Path with the file: C:/vscode/PYTHON/Sbobinare
the file is: speech.wav
Traceback (most recent call last):
File "c:\vscode\PYTHON\Sbobinare\main.py", line 12, in <module>
with sr.AudioFile(rec) as source:
File "C:\Python310\lib\site-packages\speech_recognition\__init__.py", line 203, in __enter__
self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
File "C:\Python310\lib\wave.py", line 509, in open
return Wave_read(f)
File "C:\Python310\lib\wave.py", line 159, in __init__
f = builtins.open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'speech.wav'
我的情况下的目录是:
Sbobinare
|__ main.py
|__ speech.wav
我什至尝试将.wav文件放在不同的目录中,但是它会不断给我这个错误。有人有任何想法吗?
I Know that it is probably an easy task, but something seems not to work. I'm trying to create a simple "speech to text" script and I'm using the library "SpeechRecognition". This is the code:
import speech_recognition as sr
import os
r = sr.Recognizer()
diR = input('Path with the file: ')
rec = ''
for e in os.listdir(diR):
if '.wav' in e or '.mp4' in e:
rec = e
print('the file is: ',rec)
# try:
with sr.AudioFile(rec) as source:
audio_text = r.listen(source)
try:
# using google speech recognition
text = r.recognize_google(audio_text, language ="it-IT")
print('Converting audio transcripts into text ...')
print(text)
except:
print("A problem occured ... Try again")
# except FileNotFoundError:
# print('no file found')
However when I run te software (without the first try,except) it gives me this error:
Path with the file: C:/vscode/PYTHON/Sbobinare
the file is: speech.wav
Traceback (most recent call last):
File "c:\vscode\PYTHON\Sbobinare\main.py", line 12, in <module>
with sr.AudioFile(rec) as source:
File "C:\Python310\lib\site-packages\speech_recognition\__init__.py", line 203, in __enter__
self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
File "C:\Python310\lib\wave.py", line 509, in open
return Wave_read(f)
File "C:\Python310\lib\wave.py", line 159, in __init__
f = builtins.open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'speech.wav'
The directory in my case is:
Sbobinare
|__ main.py
|__ speech.wav
I even tryied putting the .wav file in different directory but it keeps giving me this error. Anyone has any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OS.ListDir仅列出该目录中的文件的相对路径,因此代码不会在您执行脚本的目录中找到文件。如果您加入“ dir”和“ rec”,一切都应该有效:
os.listdir just lists the files' relative path in that directory, so the code won't find the file in the directory you are executing the script from. Everything should work if you just join "diR" and "rec":