我正忙于制作语音识别代码,我有这个:
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
import os
import pyaudio
import wave
import audioop
from collections import deque
import time
import math
class SpeechDetector:
def __init__(self):
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 16000
self.SILENCE_LIMIT = 1
self.PREV_AUDIO = 0.5
self.THRESHOLD = 4500
self.num_phrases = -1
MODELDIR = "../../tools/pocketsphinx/model"
DATADIR = "../../tools/pocketsphinx/test/data"
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', os.path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', os.path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
self.decoder = Decoder(config)
def setup_mic(self, num_samples=50):
print "Getting intensity …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个使用语音识别的程序。现在,我遇到了一个问题,那就是您必须按下按钮或Enter才能启动语音识别。您是否可以说出一个短语(类似于Hey Google)来开始识别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))
Run Code Online (Sandbox Code Playgroud)
提前致谢!