简体   繁体   English

Python 3.4的语音识别功能简单易行吗?

[英]Speech recognition for Python 3.4 thats easy to work?

I wish to get a simple speech recognition that works. 我希望获得一个有效的简单语音识别。 I have been looking at this on speech_recognition, When I execute the code the following error occurs 我一直在看Speech_recognition上的代码,执行代码时发生以下错误

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:                
    audio = r.listen(source)                  

try:
    print("You said " + r.recognize(audio))    
except LookupError:                            




print("You said " + r.recognize(audio))    # recognize speech using Google       Speech Recognition
AttributeError: 'Recognizer' object has no attribute 'recognize'

    print("Could not understand audio")

This was copied from their examples on their web page 从他们的示例复制到他们的网页上

I was facing the same problem. 我面临着同样的问题。 The problem was that I had not set the minimum threshold level. 问题是我没有设置最低阈值水平。 So I added this code. 所以我添加了这段代码。

import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
#set threhold level
with m as source: r.adjust_for_ambient_noise(source)
print("Set minimum energy threshold to {}".format(r.energy_threshold))
# obtain audio from the microphone

with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

print(r.recognize_google(audio))

Now its working perfect!!! 现在它的工作完美!!!

I got it working. 我知道了

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

print(r.recognize_google(audio))

What version of SpeechRecognition library do you use? 您使用什么版本的SpeechRecognition库?

You can check it with: 您可以使用以下方法进行检查:

import speech_recognition as sr
print sr.__version__

If you are using the latest version ( SpeechRecognition 3.1.3 ), you should use a recognize_google() method instead of recognize() , to use Google API. 如果您使用的是最新版本( SpeechRecognition 3.1.3 ),则应使用ognize_google recognize_google()方法而不是recognize()来使用Google API。

As for the latest documentation, you can look it up here , and also some useful examples . 至于最新文档,您可以在这里查找,以及一些有用的示例

I have taken a code that actually executes but fails to print out what I say 我采用了实际上可以执行的代码,但无法打印出我所说的内容

NOTE: this example requires PyAudio because it uses the Microphone class 注意:此示例需要PyAudio,因为它使用Microphone类

import speech_recognition as sr
import pyaudio

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# recognize speech using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
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))

# recognize speech using Wit.ai
WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
try:
    print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
except sr.UnknownValueError:
    print("Wit.ai could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Wit.ai service; {0}".format(e))

# recognize speech using IBM Speech to Text
IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
try:
    print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
except sr.UnknownValueError:
    print("IBM Speech to Text could not understand audio")
except sr.RequestError as e:
    print("Could not request results from IBM Speech to Text service; {0}".format(e))

# recognize speech using AT&T Speech to Text
ATT_APP_KEY = "INSERT AT&T SPEECH TO TEXT APP KEY HERE" # AT&T Speech to Text app keys are 32-character lowercase alphanumeric strings
ATT_APP_SECRET = "INSERT AT&T SPEECH TO TEXT APP SECRET HERE" # AT&T Speech to Text app secrets are 32-character lowercase alphanumeric strings
try:
    print("AT&T Speech to Text thinks you said " + r.recognize_att(audio, app_key=ATT_APP_KEY, app_secret=ATT_APP_SECRET))
except sr.UnknownValueError:
    print("AT&T Speech to Text could not understand audio")
except sr.RequestError as e:
    print("Could not request results from AT&T Speech to Text service; {0}".format(e))

I get the following read out 我得到以下内容

Say something! 说些什么! Google Speech Recognition thinks you said hello Could not request results from Wit.ai service; Google Speech Recognition认为您打招呼无法从Wit.ai服务请求结果; recognition request failed: Bad Request Could not request results from IBM Speech to Text service; 识别请求失败:错误请求无法从IBM语音转文本服务请求结果; recognition request failed: Unauthorized Could not request results from AT&T Speech to Text service; 识别请求失败:未经授权,无法从AT&T语音转文本服务请求结果; credential request failed: Unauthorized 凭据请求失败:未经授权

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

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