-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
65 lines (58 loc) · 1.86 KB
/
demo.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
import whisper
import pyaudio
import wave
import audioop
def record_wav(filepath="audio/output.wav"):
start = False
slience_frame = 0
THRESHOLD = 50
audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16, channels=1,
rate=44100, input=True, frames_per_buffer=1024)
frames = []
try:
print('Recording...')
while True:
data = stream.read(1024)
frames.append(data)
rms = audioop.rms(data, 2)
if not start and rms > THRESHOLD:
start = True
if start and rms < THRESHOLD:
slience_frame += 1
if rms > THRESHOLD:
slience_frame = 0
if rms < THRESHOLD and int(slience_frame) > 80:
print('Recording stopped')
break
except KeyboardInterrupt:
pass
stream.stop_stream()
stream.close()
audio.terminate()
sound_file = wave.open(filepath, 'wb')
sound_file.setnchannels(1)
sound_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
sound_file.setframerate(44100)
sound_file.writeframes(b''.join(frames))
sound_file.close()
print('Finished recording.')
return filepath
def recognize(audiofile=None, data=False):
model = whisper.load_model("base")
beam_size = 5
best_of = 5
temperature = (0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
decode_options = dict(language="en", best_of=best_of,
beam_size=beam_size, temperature=temperature, fp16=False)
transcribe_options = dict(task="transcribe", **decode_options)
if audiofile:
result = model.transcribe(audiofile, **transcribe_options)
if data:
return result
else:
return result['text'].strip()
if __name__ == '__main__':
file = record_wav()
res = recognize(audiofile=file)
print(res)