-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathasr.py
52 lines (40 loc) · 1.38 KB
/
asr.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
# using pyaudio to record voice commands and then use OpenAI whisper to recognize the voice commands
# requirements: pyaudio, wave, whisper
import pyaudio
import wave
import whisper
class SpeechRecog(object):
def __init__(self, asr_model='base'):
self.asr_model = asr_model
self.asr = whisper.Whisper(self.asr_model)
def record_audio(self, filename: str) -> None:
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# record complete
stream.stop_stream()
stream.close()
p.terminate()
print("* done recording")
# save as wav file
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames)) # join frames
wf.close()
def recognize(self, filename: str) -> str:
return self.asr.recognize(filename)