forked from mini-yifan/start_AI2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_wake.py
More file actions
31 lines (28 loc) · 825 Bytes
/
voice_wake.py
File metadata and controls
31 lines (28 loc) · 825 Bytes
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
import pyaudio
from vosk import Model, KaldiRecognizer
import json
import re
# 初始化语音识别模型
model = Model('vosk-model-small-en-us-0.15')
audio_interface = pyaudio.PyAudio()
audio_stream = audio_interface.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=4000,
)
recognizer = KaldiRecognizer(model, 16000)
print("开始识别")
while True:
data = audio_stream.read(4000)
if len(data) == 0:
break
if recognizer.AcceptWaveform(data):
result = json.loads(recognizer.Result())
text = result["text"]
print(text)
# 使用正则表达式识别是否有hello
if re.search(r'\bhello\s[tcdjh]', text, re.IGNORECASE):
print("已正确识别")
#break