|
废话不多说,直接上代码,先安装环境需要安装的包:jsonpyaudionumpyvosk1234新建一个py文件写入:importjsonimportpyaudioimportnumpyasnpfromvoskimportModel,KaldiRecognizer,SetLogLeveldefSaveWave(model):#设置音频参数FORMAT=pyaudio.paInt16#音频流的格式RATE=44100#采样率,单位HzCHUNK=4000#单位帧THRESHOLDNUM=30#静默时间,超过这个个数就保存文件THRESHOLD=100#设定停止采集阈值audio=pyaudio.PyAudio()stream=audio.open(format=FORMAT,channels=1,rate=RATE,input=True,frames_per_buffer=CHUNK)frames=[]print("开始录音...")count=0whilecount0:count-=1frames.append(data)print("停止录音!")stream.stop_stream()stream.close()audio.terminate()rec=KaldiRecognizer(model,RATE)rec.SetWords(True)str_ret=""fordatainframes:ifrec.AcceptWaveform(data):result=json.loads(rec.Result())if'text'inresult:str_ret+=result['text']result=json.loads(rec.FinalResult())if'text'inresult:str_ret+=result['text']str_ret="".join(str_ret.split())returnstr_retif__name__=="__main__":model=Model("vosk-model-small-cn-0.22")SetLogLevel(-1)while1:res=SaveWave(model)ifres!=""andres!=None:print(res)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465下载https://alphacephei.com/vosk/models/vosk-model-small-cn-0.22.zip并解压到和上述文件同目录下并开始运行即可!运行效果:开始录音...停止录音我说一句话123方便救急,比配其他环境快得多,5分钟就能跑起来!!!给出一个比较大的模型:https://alphacephei.com/vosk/models/vosk-model-cn-0.15.zip替换路径即可
|
|