-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessnifi.py
54 lines (44 loc) · 1.27 KB
/
processnifi.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
from deepspeech import Model
import json
import datetime
# forked from https://progur.com/2018/02/how-to-use-mozilla-deepspeech-tutorial.html
import scipy.io.wavfile as wav
import sys
"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""
import pyaudio
import wave
RATE = 16000
BLOCKS_PER_SECOND = 50
BLOCK_SIZE = int(RATE / float(BLOCKS_PER_SECOND))
FORMAT = pyaudio.paInt16
CHANNELS =1
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
CHUNK = 1024
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=BLOCK_SIZE)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
now = datetime.datetime.now()
row = { }
row['systemtime'] = now.strftime('%m/%d/%Y %H:%M:%S')
ds = Model(sys.argv[1], 26, 9, sys.argv[2], 500)
fs, audio = wav.read(WAVE_OUTPUT_FILENAME)
row ['voice_string'] = ds.stt(audio, fs)
json_string = json.dumps(row)
print (json_string)