rm old
parent
8ffcb02652
commit
eb99e9f571
|
|
@ -1,99 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
# https://github.com/Uberi/speech_recognition/blob/master/examples/microphone_recognition.py
|
|
||||||
|
|
||||||
|
|
||||||
# NOTE: this example requires PyAudio because it uses the Microphone class
|
|
||||||
|
|
||||||
import speech_recognition as sr
|
|
||||||
import time
|
|
||||||
import threading
|
|
||||||
import queue
|
|
||||||
from sys import stderr
|
|
||||||
from os import environ
|
|
||||||
|
|
||||||
class Recognizer(threading.Thread):
|
|
||||||
def __init__(self, q):
|
|
||||||
threading.Thread.__init__(self)
|
|
||||||
self.q = q
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
while True:
|
|
||||||
got = self.q.get()
|
|
||||||
if not got:
|
|
||||||
break
|
|
||||||
self.one(got)
|
|
||||||
|
|
||||||
def one(self, audio):
|
|
||||||
r = sr.Recognizer()
|
|
||||||
## recognize speech using Sphinx
|
|
||||||
#try:
|
|
||||||
# start = time.time()
|
|
||||||
# print("Sphinx thinks you said " + r.recognize_sphinx(audio))
|
|
||||||
# print("/Sphinx", int(time.time()-start))
|
|
||||||
#except sr.UnknownValueError:
|
|
||||||
# print("Sphinx could not understand audio")
|
|
||||||
#except sr.RequestError as e:
|
|
||||||
# print("Sphinx error; {0}".format(e))
|
|
||||||
|
|
||||||
# recognize speech using whisper
|
|
||||||
try:
|
|
||||||
start = time.time()
|
|
||||||
print(r.recognize_whisper(audio, language="english"))
|
|
||||||
print("/Whisper", int(time.time()-start), file=stderr)
|
|
||||||
except sr.UnknownValueError:
|
|
||||||
print("Whisper could not understand audio")
|
|
||||||
except sr.RequestError as e:
|
|
||||||
print("Could not request results from Whisper")
|
|
||||||
|
|
||||||
class Listener():
|
|
||||||
def __init__(self):
|
|
||||||
self.name = environ.get("MIC_NAME", "pulse_monitor")
|
|
||||||
if not self.name:
|
|
||||||
from sys import stdin
|
|
||||||
for index, name in enumerate(sr.Microphone.list_microphone_names()):
|
|
||||||
print("[{0}] Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
|
|
||||||
exit()
|
|
||||||
self._mic = None
|
|
||||||
|
|
||||||
def mic(self):
|
|
||||||
if not self._mic:
|
|
||||||
self._mic = self.new_mic()
|
|
||||||
return self._mic
|
|
||||||
|
|
||||||
def new_mic(self):
|
|
||||||
idx = [idx for idx,v in enumerate(sr.Microphone.list_microphone_names()) if v in self.name.split(",")][0]
|
|
||||||
mic = sr.Microphone(device_index=idx)
|
|
||||||
mic.__enter__()
|
|
||||||
return mic
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
try:
|
|
||||||
return self._run()
|
|
||||||
except Exception:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _run(self):
|
|
||||||
mic_timeout=int(environ.get("MIC_TIMEOUT", 5))
|
|
||||||
# obtain audio from the microphone
|
|
||||||
r = sr.Recognizer()
|
|
||||||
return r.listen(self.mic(), timeout=mic_timeout, phrase_time_limit=mic_timeout)
|
|
||||||
#return r.record(self.mic(), duration=mic_timeout)
|
|
||||||
|
|
||||||
q = queue.Queue(maxsize=1)
|
|
||||||
l = Listener()
|
|
||||||
t = Recognizer(q)
|
|
||||||
t.start()
|
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
got = l.run()
|
|
||||||
q.put(got)
|
|
||||||
if not got:
|
|
||||||
break
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
if l._mic:
|
|
||||||
l._mic.__exit__(None, None, None)
|
|
||||||
t.join()
|
|
||||||
Loading…
Reference in New Issue