繁体   English   中英

Python3 语音到文本

[英]Python3 SPEECH to TEXT

我是一个 12 岁的孩子,是编程的初学者。 如果有人可以提供帮助,那就太好了。 下面我有我的语音识别应用程序的代码,我在 MAC OS CATALINA 上,同样的错误不断出现,它打印出“说点什么”,然后一旦我说了什么什么都没有发生并且它保持冻结,一旦我停止运行的代码出现此错误。

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
    print("SAY SOMETHING")
    audio = r.listen(source)
    print("THANK YOU")

try:
    print("TEXT:  "+r.recognize_google(audio))
except:
    print("SORRY I DONT KNOW WHAT YOU MEAN")

这是我停止代码时遇到的错误,一旦它在 SAY SOMETHING 暂停很长时间。

Traceback (most recent call last):
  File "/Users/anishnagariya/PycharmProjects/HelloWorld/Tester.py", line 8, in <module>
    print("THANK YOU")
  File "/Users/anishnagariya/PycharmProjects/AI/HelloWorld/lib/python3.7/site-packages/speech_recognition/__init__.py", line 620, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/Users/anishnagariya/PycharmProjects/AI/HelloWorld/lib/python3.7/site-packages/speech_recognition/__init__.py", line 161, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "/Users/anishnagariya/PycharmProjects/AI/HelloWorld/lib/python3.7/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
KeyboardInterrupt

Process finished with exit code 1

我建议阅读您正在使用的语音识别模块的文档:

https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst

具体检查这部分:

https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst#recognizer_instanceenergy_threshold--300---type-float

能量阈值描述如下:

表示声音的能级阈值。 低于此阈值的值被视为静音,高于此阈值的值被视为语音。 可以改变。

您可以像这样设置阈值: r.energy_threshold = 4000

或者您可以这样做,这将根据当前来自环境的声音动态调整阈值。 r.dynamic_energy_threshold = True

还要确保您的麦克风正常工作

编辑:

https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst#recognizer_instancelistensource-audiosource-timeout-unionfloat-none--none-phrase_time_limit-unionfloat-none--none-snowboy_configuration-uniontuplestr- iterablestr-none--none---audiodata

为侦听呼叫设置超时和/或暂停持续时间。

import speech_recognition as sr

r = sr.Recognizer()

while True:
    with sr.Microphone() as source:
        print("SAY SOMETHING")
        try:
            audio = r.listen(source, timeout=3)
            print("THANK YOU")
            break
        except sr.WaitTimeoutError:
            print("timed out")
try:
    print("TEXT:  "+r.recognize_google(audio))
except:
    print("SORRY I DONT KNOW WHAT YOU MEAN")

根据您的麦克风质量,您可能需要设置一些阈值:

import speech_recognition as sr

r = sr.Recognizer()
r.energy_threshold = 1000
r.pause_threshold = 0.5

with sr.Microphone() as source:
    print("SAY SOMETHING")
    audio = r.listen(source)
    print("THANK YOU")

    try:
        print("TEXT:  " + r.recognize_google(audio))

    except sr.UnknownValueError:
        print("SORRY I DONT KNOW WHAT YOU MEAN")

使用一个sr.UnknownValueError except 子句sr.UnknownValueError也是speech_recognition库中未知语音的标准错误,这sr.UnknownValueError是一个好习惯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM