Python说什么时如何开始录音



我正在尝试制作一个使用语音识别的程序。现在我遇到了一个问题,那就是你必须按下一个按钮或Enter才能启动语音识别。有没有一种方法可以让你说出一个短语(有点像嘿,谷歌(,它开始在Python 3中识别语音
这是我的代码:

录制音频代码:

r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
x = r.recognize_google(audio)
print("I'm listening!")
try:
print("You said: " + r.recognize_google(audio))
except speech_recognition.UnknownValueError:
print("I am sorry but I couldn't understand you, try again.")
except speech_recognition.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))

提前感谢!

是的,基本上你必须将你的识别分为两部分:关键词识别(只听关键词(和主识别(识别用户在关键词后说的话(。要知道,这意味着你的节目将一直在听。

对于关键字识别,您可以使用Recognizer()listen_in_background方法,并在任何回调中扫描关键字。如果找到了关键字,则调用Recognizer().listen(source)

由于听关键词需要你的程序不断地听和识别,你不想使用任何需要互联网连接的语音识别API(Bing、Google、Watson、Houndify等…(。这是因为所有这些都有每月API限制,你很容易就会消耗掉。您希望保存这些API以供实际识别。我相信你唯一的离线选择是使用recognize_sphinx或snowboy热词检测。我从来没有真正使用过Snowboy(尽管我听说它很好(,因为它在Windows上不起作用(或者至少在我写程序时不起作用(,但Sphinx有一个关键词检测工具。

基本上,您通过元组传递sphinx_identifier关键字,以及它对提取这些关键字的敏感程度,然后它将尝试专注于在语音中查找这些单词。注意,你对关键词越敏感,你得到的误报就越多。

这里有一个例子:

import speech_recognition as sr
import time
r = sr.Recognizer()
# Words that sphinx should listen closely for. 0-1 is the sensitivity
# of the wake word.
keywords = [("google", 1), ("hey google", 1), ]
source = sr.Microphone()

def callback(recognizer, audio):  # this is called from the background thread
try:
speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords)
print(speech_as_text)
# Look for your "Ok Google" keyword in speech_as_text
if "google" in speech_as_text or "hey google":
recognize_main()
except sr.UnknownValueError:
print("Oops! Didn't catch that")

def recognize_main():
print("Recognizing Main...")
audio_data = r.listen(source)
# interpret the user's words however you normally interpret them

def start_recognizer():
r.listen_in_background(source, callback)
time.sleep(1000000)

start_recognizer()

这个链接在使用speech_recognition库时非常有用:

https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst

代码:

from pocketsphinx import LiveSpeech
import os
for i in LiveSpeech():
print(i)
if "hey Google" in str(i):
os.startfile("Your File.your_format")
#works Perfectly and if you want to hide python console then save as youfile.pyw

最新更新