Merge branch 'master' of ssh://git.house:/usr/local/git/base/kiosk
[kiosk.git] / test_listen.py
1 #!/usr/bin/env python3
2
3 import os
4 import struct
5 from datetime import datetime
6 from threading import Thread
7
8 import numpy as np
9 import pvporcupine
10 import pyaudio
11 import soundfile
12 import speech_recognition as sr
13
14 recognizer = sr.Recognizer()
15 raw = audio_stream.read(porcupine.frame_length, exception_on_overflow=False)
16 pcm = struct.unpack_from("h" * porcupine.frame_length, raw)
17 result = porcupine.process(pcm)
18
19 speech = sr.AudioData(
20     frame_data = bytes(raw),
21     sample_rate = porcupine.sample_rate,
22     sample_width = 2,  # 16 bits
23 )
24 command = recognizer.recognize_google(speech)
25 print('[%s] >>>>>>>>>>>>> Google says command was %s' % (
26     str(datetime.now()), command)
27 )