当我说打开日历时,它不会打开日历。我不仅限于日历。不适用于笔记本电脑上的所有其他应用程序。
我尝试过移动这些功能。我已经尝试过再次安装这些库,但是它只是说它们已经安装。
import speech_recognition as sr
import os
import subprocess
import webbrowser
import requests
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
# This function will pass your text to the machine learning model
# and return the top result with the highest confidence
def classify(text):
key = "b6360ce0-e77b-11e9-b28c-e367055df979bcc2f3ae-ebb9-4777-a39a-5b5f5e6155d2"
url = "https://machinelearningforkids.co.uk/api/scratch/"+ key + "/classify"
response = requests.get(url, params={ "data" : text })
if response.ok:
responseData = response.json()
topMatch = responseData[0]
return topMatch
else:
response.raise_for_status()
r = sr.Recognizer()
mic = sr.Microphone()
try_light = 0
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
transcript = r.recognize_google(audio)
transcript = str(transcript).lower()
# Basic Commands go here
print("STT Heard this: " + transcript)
if transcript.find('google') != -1:
try_light = 0
url = "https://www.google.com.tr/search?q={}".format(transcript)
webbrowser.open_new_tab(url)
print("Made it to google")
elif transcript.find('open') != -1:
try_light = 0
d = '/Applications'
records = []
apps = os.listdir(d)
for app in apps:
record = {}
record['voice_command'] = 'open ' + app.split('.app')[0]
record['sys_command'] = 'open ' + d +'/%s' %app.replace(' ','\ ')
records.append(record)
es = Elasticsearch(['localhost:9200'])
bulk(es, records, index='voice_assistant', doc_type='text', raise_on_error=True)
def search_es(query):
res = es.search(index="voice_assistant", doc_type="text", body={
"query" :{
"match": {
"voice_command": {
"query": query,
"fuzziness": 2
}
}
},
})
return res['hits']['hits'][0]['_source']['sys_command']
search_es('open calendar')
else:
try_light = 1
if try_light == 1:
demo = classify(transcript)
label = demo["class_name"]
confidence = demo["confidence"]
print ("result: '%s' with %d%% confidence" % (label, confidence))
预期结果是打开我所说的应用程序。实际结果是我得到了这个结果:
回溯(最近通话最近): 文件“ speech_to_text.py”,第30行,在 成绩单= r.recognize_google(音频) 在ognize_google中的文件“ /usr/local/lib/python2.7/site-packages/speech_recognition/init.py”中,第858行 如果不是isinstance(actual_result,dict)或len(actual_result.get(“ alternative”,[]))== 0:引发UnknownValueError() speech_recognition.UnknownValueError