forked from ShawnHymel/tflite-speech-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-rpi-tflite-audio-switch.py
executable file
·125 lines (102 loc) · 3.6 KB
/
05-rpi-tflite-audio-switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Connect a relay to board pin 8 and run this script. The relay should be on
by default when the script is first run. When it hears the word "stop,"
the program will switch the realy to off. You will need to run the script
again to turn it back on.
"""
import sounddevice as sd
import numpy as np
import scipy.signal
import timeit
import python_speech_features
import RPi.GPIO as GPIO
from tflite_runtime.interpreter import Interpreter
# Parameters
debug_time = 0
debug_acc = 0
led_pin = 8
word_threshold = 0.5
rec_duration = 0.5
window_stride = 0.5
sample_rate = 48000
resample_rate = 8000
num_channels = 1
num_mfcc = 16
model_path = 'wake_word_stop_lite.tflite'
word_flag = 0
# Sliding window
window = np.zeros(int(rec_duration * resample_rate) * 2)
# GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT, initial=GPIO.HIGH)
# Load model (interpreter)
interpreter = Interpreter(model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(input_details)
# Decimate (filter and downsample)
def decimate(signal, old_fs, new_fs):
# Check to make sure we're downsampling
if new_fs > old_fs:
print("Error: target sample rate higher than original")
return signal, old_fs
# We can only downsample by an integer factor
dec_factor = old_fs / new_fs
if not dec_factor.is_integer():
print("Error: can only decimate by integer factor")
return signal, old_fs
# Do decimation
resampled_signal = scipy.signal.decimate(signal, int(dec_factor))
return resampled_signal, new_fs
# This gets called every 0.5 seconds
def sd_callback(rec, frames, time, status):
global word_flag
# Start timing for testing
start = timeit.default_timer()
# Notify if errors
if status:
print('Error:', status)
# Remove 2nd dimension from recording sample
rec = np.squeeze(rec)
# Resample
rec, new_fs = decimate(rec, sample_rate, resample_rate)
# Save recording onto sliding window
window[:len(window)//2] = window[len(window)//2:]
window[len(window)//2:] = rec
# Compute features
mfccs = python_speech_features.base.mfcc(window,
samplerate=new_fs,
winlen=0.256,
winstep=0.050,
numcep=num_mfcc,
nfilt=26,
nfft=2048,
preemph=0.0,
ceplifter=0,
appendEnergy=False,
winfunc=np.hanning)
mfccs = mfccs.transpose()
# Make prediction from model
in_tensor = np.float32(mfccs.reshape(1, mfccs.shape[0], mfccs.shape[1], 1))
interpreter.set_tensor(input_details[0]['index'], in_tensor)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
val = output_data[0][0]
if val > word_threshold:
print("Emergency shut down detected!")
GPIO.output(led_pin, GPIO.LOW)
word_flag = 1
if debug_acc:
print(val)
if debug_time:
print(timeit.default_timer() - start)
# Start streaming from microphone
with sd.InputStream(channels=num_channels,
samplerate=sample_rate,
blocksize=int(sample_rate * rec_duration),
callback=sd_callback):
while word_flag == 0:
pass
print("Done!")