一概述
本篇简要介绍百度语音语音识别的基本使用 (其实是斗地主时想弄个记牌器又没 money, 抓包什么的又不会, 只好搞语音识别的了)
二创建应用
打开百度语音官网, 产品与使用 -> 语音识别 -> 立即使用 -> 创建应用
出现如下页面
依照提示依次填写, 最终结果
(ps: 我就想弄个记牌的, 就起了个计数器的名)
点右方的 查看 key 记下 App ID,API Key,Secret Key 接下来要用到
需要安装模块
- pip install baidu-aip
- pip install pyaudio
语音识别代码
- from aip import AipSpeech
- """你的 APPID AK SK"""
APP_ID = 你记下的 APP_ID
API_KEY = 你记下的 API_KEY
SECRET_KEY = 你记下的 SECRET_KEY
- client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
- # 读取文件
- def get_file_content(filePath):
- with open(filePath, rb) as fp:
- return fp.read()
- # 识别本地文件
- li=client.asr(get_file_content(01.pcm), pcm, 8000, {
- lan: zh,
- })
- print(li)
- # 从 URL 获取文件识别
- # client.asr(, pcm, 16000, {
- # url: http://121.40.195.233/res/16k_test.pcm,
- # callback: http://xxx.com/receive,
- # })
python 录音代码
- import wave
- from pyaudio import PyAudio,paInt16
- framerate=8000
- NUM_SAMPLES=2000
- channels=1
- sampwidth=2
- TIME=2
- def save_wave_file(filename,data):
- save the date to the wavfile
- wf=wave.open(filename,wb)
- wf.setnchannels(channels)
- wf.setsampwidth(sampwidth)
- wf.setframerate(framerate)
- wf.writeframes(b"".join(data))
- wf.close()
- def my_record():
- pa=PyAudio()
- stream=pa.open(format = paInt16,channels=1,
- rate=framerate,input=True,
- frames_per_buffer=NUM_SAMPLES)
- my_buf=[]
- count=0
- while count<TIME*5:# 控制录音时间
- string_audio_data = stream.read(NUM_SAMPLES)
- my_buf.append(string_audio_data)
- count+=1
- print(.)
- save_wave_file(01.pcm,my_buf)
- stream.close()
- chunk=2014
- def play():
- wf=wave.open(r"01.pcm",rb)
- p=PyAudio()
- stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
- wf.getnchannels(),rate=wf.getframerate(),output=True)
- while True:
- data=wf.readframes(chunk)
- if data=="":break
- stream.write(data)
- stream.close()
- p.terminate()
- if __name__ == __main__:
- my_record()
- print(Over!)
- play()
效果如下图:
帮助文档:
百度语音帮助文档 or 手册
三后记
本代码未完全实现, 有兴趣可自行整理, 玩斗地主的时候声音可能要大点, 因为识别有时候会报 3001 错误, 音频质量过差, 不过被打可别找我
来源: http://www.bubuko.com/infodetail-2511665.html