簡體   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