-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSneezeDetector.py
More file actions
74 lines (58 loc) · 1.92 KB
/
Copy pathSneezeDetector.py
File metadata and controls
74 lines (58 loc) · 1.92 KB
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
import sys
import collections
import pyaudio as pyaudio
import sys
import os
sys.path.append(os.path.abspath('./HotWordDetection/hotword_detection'))
import wordRecorder as wr
import hwDetector as hd
wrdRec = wr.wordRecorder()
hwDet = hd.hwDetector()
wrdRec = wr.wordRecorder()
hwDet = hd.hwDetector()
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK_DURATION_MS = 30 # supports 10, 20 and 30 (ms)
PADDING_DURATION_MS = 1000
CHUNK_SIZE = int(RATE * CHUNK_DURATION_MS / 1000)
NUM_PADDING_CHUNKS = int(PADDING_DURATION_MS / CHUNK_DURATION_MS)
pa = pyaudio.PyAudio()
stream = pa.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
start=False,
# input_device_index=2, ### maybe that line is causing microphone errors - natalia
frames_per_buffer=CHUNK_SIZE)
got_a_sentence = False
leave = False
# endless cycle #1
while not leave:
ring_buffer = collections.deque(maxlen=NUM_PADDING_CHUNKS)
ring_buffer_chunknum = 0
buffer_in = ''
print("* recording")
stream.start_stream()
# endless cycle #2
while not got_a_sentence:
chunk = stream.read(CHUNK_SIZE)
ring_buffer_chunknum += 1
ring_buffer.append(chunk)
# ring buffer is full
if ring_buffer_chunknum == NUM_PADDING_CHUNKS:
sys.stdout.write('-')
sys.stdout.write('\n')
# moving the full ring buffer to data (?)
data = b''.join(ring_buffer)
### now we can try to find if it is about sneezing or not: ring_buffer (hajer part)
print(
hwDet.isHotword(data)) ### totally not sure how it works, but it doesnt accept current string - natalia
###
ring_buffer.clear()
ring_buffer_chunknum = 0
stream.stop_stream()
print("* done recording")
# got_a_sentence = False
# leave = True
stream.close()