我在SpeechRecognition Python语言选择方面遇到问题

时间:2019-04-02 06:20:11

标签: python

使用SpeechRecognition获得Python错误

  

“识别器”对象没有属性“ recognize_google”

我尝试了很多事情,除了我使用r.recognize(audio)代替r.recognize_google

之外,其他方法均无效

r.recognize(audio)仅使用英语工作,不需要google key。.奇怪。

这是我的代码

import speech_recognition as sr
import socket
import requests
import json

WAV_FILE = '/home/nao/recordings/microphones/mywavefile.wav'
mykey = "AIzaSyDbB#@34N1VaRW27HxA68rZW59UZ2qoPD8" # <-- modified key for my protection.

r = sr.Recognizer()
with sr.WavFile(WAV_FILE) as source:
audio = r.record(source) # read the entire WAV
try:
    sresult = r.recognize_google(audio, key=mykey, language="fr-CA")
    # sresult = r.recognize(audio) work in english
    print("I think you said:" + sresult)
except Exception as e:
    print("An error occure: " + str(e))

通过

如果我只放

sresult = r.recognize(audio)

它是英语的,r.recognize_google似乎根本不工作,会返回此错误

  

AttributeError:“模块”对象没有属性“ google_Recognizer”

我拥有最新版本的SpeechRecognition 3.8.1

我在/usr/lib/python2.7/site-packages中的

文件夹,它是安装软件包的位置 语音识别 SpeechRecognition-3.8.1-py2.7.egg-info

我使用的文件名为ai.py,与语音识别无关 请帮助我...

我想使用我的Google密钥和语言。

请帮助我。

3 个答案:

答案 0 :(得分:0)

我找不到fr-CA中是否存在语言speechrecognition。您可以执行此代码吗?

import speech_recognition as sr
print(sr.__version__)

r = sr.Recognizer()
filename = '/home/nao/recordings/microphones/mywavefile.wav'
sourcedata = sr.AudioFile(filename)
with sourcedata as source:
    audio = r.record(source)
try:
    print("Google Speech Recognition thinks you said " +
          r.recognize_google(audio, language='fr-CA'))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results \
           from Google Speech Recognition service; {0}".format(e))

在谷歌搜索后,我发现此错误位于open issue中。受另一个issue的启发,我更新了代码。因此,您可以尝试捕获此错误,如上面的代码所示。

UnknownValueError实际上意味着Speech_recognition无法识别音频源中的任何内容。如果您从麦克风录音中收到此错误,建议您使用pyAudio调至sample_ratechunk_size之类的调参。

但是如果从文件录制后出现错误-我找不到解决方法

答案 1 :(得分:0)

以下作品。使用r.listen提供音​​频:

    r = sr.Recognizer()
    with sr.Microphone() as source:                # use the default microphone as the audio source
         audio = r.listen(source)
    try:
         words = r.recognize_google(audio)    # recognize speech using Google Speech Recognition

您一开始不必担心键或语言规范。尝试使用英语和法语同时存在的单词制作音频文件。如果仍然无法使用,请尝试使用麦克风。

答案 2 :(得分:0)

试试这个吧:

import speech_recognition as sr
from os import walk

r = sr.Recognizer()
#optional
#r.energy_threshold = 300

def startConvertion(path = 'file.wav', lang = 'fr-CA'): 
    with sr.AudioFile(path) as source:
        #print('Fetching File')
        audio_file = r.record(source)
        print(r.recognize_google(audio_file, language=lang))
startConvertion()