语音识别不限制文件

发布于 2025-01-23 18:23:38 字数 1545 浏览 7 评论 0原文

我知道这可能是一件容易的事,但似乎没有用。我正在尝试创建一个简单的“语音到文本”脚本,并且我正在使用库“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-01-30 18:23:38

OS.ListDir仅列出该目录中的文件的相对路径,因此代码不会在您执行脚本的目录中找到文件。如果您加入“ dir”和“ rec”,一切都应该有效:

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)

# Modified here !!
rec = os.path.join(diR, 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')

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":

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)

# Modified here !!
rec = os.path.join(diR, 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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文