循环崩溃在speech_recognition上

发布于 2025-01-21 17:14:53 字数 2796 浏览 3 评论 0原文

Python Newbie试图教自己如何使用Python运行Secempon_recognition,而我并没有好运。

下面的代码运行一次,并正确将WAV文件转换为文本,但随后在我的S3存储桶中运行其余2个WAV文件之前崩溃了。文件绝对存在:

OSR_US_000_0010_8K.WAV
OSR_US_000_0011_8K.WAV
OSR_US_000_0012_8K.WAV

我可以对其进行一些帮助。

提前致谢。

import boto3

import speech_recognition as sr

r = sr.Recognizer()

session = boto3.client('s3',

                    aws_access_key_id= XXXX,

                    aws_secret_access_key=XXXX,

                    region_name='XXXX')

my_bucket = s3.Bucket(mys3bucket)

for my_bucket_object in my_bucket.objects.all():

    with sr.AudioFile(my_bucket_object.key) as source:

        print(my_bucket_object.key)

        audio_data = r.record(source)

        text = r.recognize_google(audio_data)

        print(text)


OSR_us_000_0010_8k.wav<br>
Birch canoe slid on the smooth plank glue the sea to a dark blue background it is easy to tell the depth of a well these day the chicken leg of a variegated rice is often served in roundels the juice of lemons mix find the boxes on the side the pump truck the ha grimstead topcon and garbage for hours of Citi workspace a large-sized and stockings in the hearts of cell

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-385959f26678> in <module>
     14 
     15 for my_bucket_object in my_bucket.objects.all():
---> 16     with sr.AudioFile(my_bucket_object.key) as source:
     17         print(my_bucket_object.key)
     18         audio_data = r.record(source)

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/site-packages/speech_recognition/__init__.py in __enter__(self)
    201         try:
    202             # attempt to read the file as WAV
--> 203             self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
    204             self.little_endian = True  # RIFF WAV is a little-endian format (most ``audioop`` operations assume that the frames are stored in little-endian form)
    205         except (wave.Error, EOFError):

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in open(f, mode)
    508             mode = 'rb'
    509     if mode in ('r', 'rb'):
--> 510         return Wave_read(f)
    511     elif mode in ('w', 'wb'):
    512         return Wave_write(f)

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in __init__(self, f)
    158         self._i_opened_the_file = None
    159         if isinstance(f, str):
--> 160             f = builtins.open(f, 'rb')
    161             self._i_opened_the_file = f
    162         # else, assume it is an open file object already

FileNotFoundError: [Errno 2] No such file or directory: 'OSR_us_000_0011_8k.wav'

Python Newbie trying to teach myself how to use Python to run speech_recognition, and Im not having great luck.

The code below runs once and correctly converts a wav file to text, but then it crashes before running the remaining 2 wav files in my S3 bucket. The files are absolutely there:

OSR_us_000_0010_8k.wav
OSR_us_000_0011_8k.wav
OSR_us_000_0012_8k.wav

I could use some help fixing it.

Thanks in Advance.

import boto3

import speech_recognition as sr

r = sr.Recognizer()

session = boto3.client('s3',

                    aws_access_key_id= XXXX,

                    aws_secret_access_key=XXXX,

                    region_name='XXXX')

my_bucket = s3.Bucket(mys3bucket)

for my_bucket_object in my_bucket.objects.all():

    with sr.AudioFile(my_bucket_object.key) as source:

        print(my_bucket_object.key)

        audio_data = r.record(source)

        text = r.recognize_google(audio_data)

        print(text)


OSR_us_000_0010_8k.wav<br>
Birch canoe slid on the smooth plank glue the sea to a dark blue background it is easy to tell the depth of a well these day the chicken leg of a variegated rice is often served in roundels the juice of lemons mix find the boxes on the side the pump truck the ha grimstead topcon and garbage for hours of Citi workspace a large-sized and stockings in the hearts of cell

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-385959f26678> in <module>
     14 
     15 for my_bucket_object in my_bucket.objects.all():
---> 16     with sr.AudioFile(my_bucket_object.key) as source:
     17         print(my_bucket_object.key)
     18         audio_data = r.record(source)

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/site-packages/speech_recognition/__init__.py in __enter__(self)
    201         try:
    202             # attempt to read the file as WAV
--> 203             self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
    204             self.little_endian = True  # RIFF WAV is a little-endian format (most ``audioop`` operations assume that the frames are stored in little-endian form)
    205         except (wave.Error, EOFError):

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in open(f, mode)
    508             mode = 'rb'
    509     if mode in ('r', 'rb'):
--> 510         return Wave_read(f)
    511     elif mode in ('w', 'wb'):
    512         return Wave_write(f)

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in __init__(self, f)
    158         self._i_opened_the_file = None
    159         if isinstance(f, str):
--> 160             f = builtins.open(f, 'rb')
    161             self._i_opened_the_file = f
    162         # else, assume it is an open file object already

FileNotFoundError: [Errno 2] No such file or directory: 'OSR_us_000_0011_8k.wav'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文