- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我尝试运行我的 python 文件时,我在 python3 和 python on raspbian 上都安装了语音识别,我收到以下错误
(hhsmartmirror) pi@raspberrypi:~/AI-Smart-Mirror $ python bot.py Traceback (most recent call last): File "bot.py", line 13, in from speech import Speech File "/home/pi/AI-Smart-Mirror/speech.py", line 3, in import speech_recognition as sr ImportError: No module named speech_recognition
代码文件:
#bot.py
#speechrecognition, pyaudio, brew install portaudio
import sys
sys.path.append("./")
import requests
import datetime
import dateutil.parser
import json
import traceback
from nlg import NLG
from speech import Speech
from knowledge import Knowledge
from vision import Vision
my_name = "Aaron"
launch_phrase = "ok mirror"
use_launch_phrase = True
weather_api_token = "Bearer d1e49b263abd08d422822b72b4c3eec4"
wit_ai_token = "Bearer CL47LPKJWMNDNWPVMY4RPTZR3JDCZ2GZ"
debugger_enabled = True
camera = 0
class Bot(object):
def __init__(self):
self.nlg = NLG(user_name=my_name)
self.speech = Speech(launch_phrase=launch_phrase, debugger_enabled=debugger_enabled)
self.knowledge = Knowledge(weather_api_token)
self.vision = Vision(camera=camera)
def start(self):
"""
Main loop. Waits for the launch phrase, then decides an action.
:return:
"""
while True:
requests.get("http://localhost:8080/clear")
if self.vision.recognize_face():
print "Found face"
if use_launch_phrase:
recognizer, audio = self.speech.listen_for_audio()
if self.speech.is_call_to_action(recognizer, audio):
self.__acknowledge_action()
self.decide_action()
else:
self.decide_action()
def decide_action(self):
"""
Recursively decides an action based on the intent.
:return:
"""
recognizer, audio = self.speech.listen_for_audio()
# received audio data, now we'll recognize it using Google Speech Recognition
speech = self.speech.google_speech_recognition(recognizer, audio)
if speech is not None:
try:
r = requests.get('https://api.wit.ai/message?v=20160918&q=%s' % speech,
headers={"Authorization": wit_ai_token})
print r.text
json_resp = json.loads(r.text)
entities = None
intent = None
if 'entities' in json_resp and 'Intent' in json_resp['entities']:
entities = json_resp['entities']
intent = json_resp['entities']['Intent'][0]["value"]
print intent
if intent == 'greeting':
self.__text_action(self.nlg.greet())
elif intent == 'snow white':
self.__text_action(self.nlg.snow_white())
elif intent == 'weather':
self.__weather_action(entities)
elif intent == 'news':
self.__news_action()
elif intent == 'maps':
self.__maps_action(entities)
elif intent == 'holidays':
self.__holidays_action()
elif intent == 'appearance':
self.__appearance_action()
elif intent == 'user status':
self.__user_status_action(entities)
elif intent == 'user name':
self.__user_name_action()
elif intent == 'personal status':
self.__personal_status_action()
elif intent == 'joke':
self.__joke_action()
elif intent == 'insult':
self.__insult_action()
return
elif intent == 'appreciation':
self.__appreciation_action()
return
else: # No recognized intent
self.__text_action("I'm sorry, I don't know about that yet.")
return
except Exception as e:
print "Failed wit!"
print(e)
traceback.print_exc()
self.__text_action("I'm sorry, I couldn't understand what you meant by that")
return
self.decide_action()
def __joke_action(self):
joke = self.nlg.joke()
if joke is not None:
self.__text_action(joke)
else:
self.__text_action("I couldn't find any jokes")
def __user_status_action(self, nlu_entities=None):
attribute = None
if (nlu_entities is not None) and ("Status_Type" in nlu_entities):
attribute = nlu_entities['Status_Type'][0]['value']
self.__text_action(self.nlg.user_status(attribute=attribute))
def __user_name_action(self):
if self.nlg.user_name is None:
self.__text_action("I don't know your name. You can configure it in bot.py")
self.__text_action(self.nlg.user_name)
def __appearance_action(self):
requests.get("http://localhost:8080/face")
def __appreciation_action(self):
self.__text_action(self.nlg.appreciation())
def __acknowledge_action(self):
self.__text_action(self.nlg.acknowledge())
def __insult_action(self):
self.__text_action(self.nlg.insult())
def __personal_status_action(self):
self.__text_action(self.nlg.personal_status())
def __text_action(self, text=None):
if text is not None:
requests.get("http://localhost:8080/statement?text=%s" % text)
self.speech.synthesize_text(text)
def __news_action(self):
headlines = self.knowledge.get_news()
if headlines:
requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines}))
self.speech.synthesize_text(self.nlg.news("past"))
interest = self.nlg.article_interest(headlines)
if interest is not None:
self.speech.synthesize_text(interest)
else:
self.__text_action("I had some trouble finding news for you")
def __weather_action(self, nlu_entities=None):
current_dtime = datetime.datetime.now()
skip_weather = False # used if we decide that current weather is not important
weather_obj = self.knowledge.find_weather()
temperature = weather_obj['temperature']
icon = weather_obj['icon']
wind_speed = weather_obj['windSpeed']
weather_speech = self.nlg.weather(temperature, current_dtime, "present")
forecast_speech = None
if nlu_entities is not None:
if 'datetime' in nlu_entities:
if 'grain' in nlu_entities['datetime'][0] and nlu_entities['datetime'][0]['grain'] == 'day':
dtime_str = nlu_entities['datetime'][0]['value'] # 2016-09-26T00:00:00.000-07:00
dtime = dateutil.parser.parse(dtime_str)
if current_dtime.date() == dtime.date(): # hourly weather
forecast_obj = {'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast']}
forecast_speech = self.nlg.forecast(forecast_obj)
elif current_dtime.date() < dtime.date(): # sometime in the future ... get the weekly forecast/ handle specific days
forecast_obj = {'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast']}
forecast_speech = self.nlg.forecast(forecast_obj)
skip_weather = True
if 'Weather_Type' in nlu_entities:
weather_type = nlu_entities['Weather_Type'][0]['value']
print weather_type
if weather_type == "current":
forecast_obj = {'forecast_type': 'current', 'forecast': weather_obj['current_forecast']}
forecast_speech = self.nlg.forecast(forecast_obj)
elif weather_type == 'today':
forecast_obj = {'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast']}
forecast_speech = self.nlg.forecast(forecast_obj)
elif weather_type == 'tomorrow' or weather_type == '3 day' or weather_type == '7 day':
forecast_obj = {'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast']}
forecast_speech = self.nlg.forecast(forecast_obj)
skip_weather = True
weather_data = {"temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour}
requests.post("http://localhost:8080/weather", data=json.dumps(weather_data))
if not skip_weather:
self.speech.synthesize_text(weather_speech)
if forecast_speech is not None:
self.speech.synthesize_text(forecast_speech)
def __maps_action(self, nlu_entities=None):
location = None
map_type = None
if nlu_entities is not None:
if 'location' in nlu_entities:
location = nlu_entities['location'][0]["value"]
if "Map_Type" in nlu_entities:
map_type = nlu_entities['Map_Type'][0]["value"]
if location is not None:
maps_url = self.knowledge.get_map_url(location, map_type)
maps_action = "Sure. Here's a map of %s." % location
body = {'url': maps_url}
requests.post("http://localhost:8080/image", data=json.dumps(body))
self.speech.synthesize_text(maps_action)
else:
self.__text_action("I'm sorry, I couldn't understand what location you wanted.")
def __holidays_action(self):
holidays = self.knowledge.get_holidays()
next_holiday = self.__find_next_holiday(holidays)
requests.post("http://localhost:8080/holidays", json.dumps({"holiday": next_holiday}))
self.speech.synthesize_text(self.nlg.holiday(next_holiday['localName']))
def __find_next_holiday(self, holidays):
today = datetime.datetime.now()
for holiday in holidays:
date = holiday['date']
if (date['day'] > today.day) and (date['month'] > today.month):
return holiday
# next year
return holidays[0]
if __name__ == "__main__":
bot = Bot()
bot.start()
# speech.py
# speechrecognition, pyaudio, brew install portaudio
import speech_recognition as sr
import os
import requests
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
class Speech(object):
def __init__(self, launch_phrase="mirror mirror", debugger_enabled=False):
self.launch_phrase = launch_phrase
self.debugger_enabled = debugger_enabled
self.__debugger_microphone(enable=False)
def google_speech_recognition(self, recognizer, audio):
speech = None
try:
speech = recognizer.recognize_google(audio)
print("Google Speech Recognition thinks you said " + speech)
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))
return speech
def listen_for_audio(self):
# obtain audio from the microphone
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source)
self.__debugger_microphone(enable=True)
print "I'm listening"
audio = r.listen(source)
self.__debugger_microphone(enable=False)
print "Found audio"
return r, audio
def is_call_to_action(self, recognizer, audio):
speech = self.google_speech_recognition(recognizer, audio)
if speech is not None and self.launch_phrase in speech.lower():
return True
return False
def synthesize_text(self, text):
tts = gTTS(text=text, lang='en')
tts.save("tmp.mp3")
song = AudioSegment.from_mp3("tmp.mp3")
play(song)
os.remove("tmp.mp3")
def __debugger_microphone(self, enable=True):
if self.debugger_enabled:
try:
r = requests.get("http://localhost:8080/microphone?enabled=%s" % str(enable))
if r.status_code != 200:
print("Used wrong endpoint for microphone debugging")
except Exception as e:
print(e)
最佳答案
如果您通过 Traceback 错误回读,基本上它是说在第 13 行,您成功从 speech 导入了 Speech,但在该文件内部它试图导入一个名为 speech_recognition 的模块。显然,由于我不知道的原因,它找不到该文件。
一些潜在的修复
鉴于我以前从未使用过这种模块,其他人可能会就修复提供一些更好的建议,但这是我最好的选择。
抱歉没能提供更多帮助!快乐编码。
关于python - 语音识别错误 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328169/
我有一个说一些短语的音板应用程序,但是现在我希望能够从男声/女声中改变出来,问题是我不知道该怎么做。任何帮助,将不胜感激。 我正在使用AVFoundation/AVAudioPlayer播放声音。 谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
因为我想在后台录制音频,所以我使用了服务..但是我无法在服务中录制音频。 我在 Activity 中尝试了相同的代码,它对我有用。但是如何在输入语音/语音时在后台进行录音,这意味着如果有语音输入就应该
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我有一个音频流,我会从中提取单词(语音)。因此,例如使用 audio.wav 我会得到 001.wav、002.wav、003.wav 等,其中每个 XXX.wav 是一个词。 我正在寻找一个库或程序
不幸的是,我只能说四种语言,那么如果我知道文本的语言,我如何知道我必须使用哪种 OS X 语音?我在Apple的文档中找不到任何有关它的信息。至少有一张有语音/语言的 table 吗? 最佳答案 您可
有没有办法从命令行使用 MS Speech 实用程序?我可以在 Mac 上完成,但在 Windows XP 上找不到任何引用。 最佳答案 我在这个主题上的 2 美分,命令行单行: 在 Win 上使用
所以我开始了我的不和谐机器人的音乐部分。现在,正如我在上一个问题中所做的那样,这里只是音乐命令的片段:Pastebin #1 在显示 if (msg.member.voiceConnection ==
有谁知道有什么好的 API 或库可以听(语音)文本。我尝试听三种语言的(语音)文本,我想知道最好从哪里开始以及如何开始。我可以对所有三种语言使用通用语音吗?我将使用 eclipse 和 java 作为
首先,我只是一个爱好者,如果这是一个愚蠢的问题或者我太天真了,我很抱歉。 (这也意味着我买不起昂贵的库) 情况是这样的:我正在使用 C#.NET 构建一个简单的语音聊天应用程序(类似于 Ventril
我正在制作一个模块,可以生成和传输语音 IP 数据包。我知道我必须为服务类型字段设置一些值。这个值是多少? 最佳答案 该值应该是x。 关于c - 语音 ip 的服务类型字段集,我们在Stack Ove
有人能帮帮我吗?我使用 SAPI 语音文本,但我不能设置女声,这是代码,它用男声说话,但我想改变它,我想要女声 #include "stdafx.h" using namespace std; voi
我正在寻找一种方法来为一个项目在 Java 中识别预注册的语音命令,但我还没有想出一个好的方法,我研究了快速傅里叶 和处理 wave 文件 的不同方法,但我无法决定我应该如何实现它。 这个想法很简单,
我在 android 的语音识别 API 工作。 我是 Speech Recognition Api 的新手,我的要求是西类牙语语音,并从 Android 的语音识别 API 中获得西类牙语的最佳匹配
我在 Java 中使用一组名为(MaryTTS[实际上还有更多])的库来将 text to speech 转换为该目的,使用以下代码: public class TextToSpeech {
我正在尝试使用webRTC和php作为服务器端来实现单向语音传输。 查看samples ,我无法理解webRTC机制。 在我看来,流程应该是这样的: 调用者和接收者在服务器上注册 接收者监听来电 调用
我的名字是 Joey,我想知道是否有一种在 C++ 中使用语音的方法,如果有人可以给我指出引用资料和书籍,非常感谢...... 最佳答案 你应该看看 Windows Text-To-Speech AP
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在使用 Java 语音识别 API - Jarvis,位于 https://github.com/lkuza2/java-speech-api 但是,当我运行应用程序时,出现错误:服务器返回 HT
我们正在做一个需要讲阿拉伯语的项目,我们找到了一个开源工具,Mbrola project , 可以做到这一点。 但是,我还需要一些方法将阿拉伯语文本转换为 SAMPA 语音。那么有人可以帮助我将阿拉伯
我是一名优秀的程序员,十分优秀!