python语音识别代码_python实现百度语音识别api
时间: 2023-09-03 11:06:24 浏览: 141
python-voicere.zip_python语音识别_语音 python_语音识别 api_语音识别 python_语音识
以下是Python实现百度语音识别API的示例代码:
```python
import wave
import json
import base64
import requests
# 设置参数
URL = "http://vop.baidu.com/server_api"
FORMAT = 'wav'
RATE = 16000
CUID = "123456PYTHON"
DEV_PID = 1536
# 读取文件,转为base64编码
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return base64.b64encode(fp.read()).decode('utf-8')
# 发送post请求
def request_api(audio):
data = {
"format": FORMAT,
"rate": RATE,
"dev_pid": DEV_PID,
"speech": audio,
"cuid": CUID,
"len": len(audio),
"channel": 1,
"token": "24.79f6b6f4d5c1bdc94d70a4e20431d436.2592000.1560852516.282335-16244860"
}
headers = {'content-type': 'application/json'}
response = requests.post(URL, json.dumps(data), headers=headers)
return response.json()
# 识别wav格式音频文件
def recognize_wave_file(filePath):
audio = get_file_content(filePath)
result = request_api(audio)
if 'result' in result:
return result['result'][0]
else:
return ""
# 识别mp3格式音频文件
def recognize_mp3_file(filePath):
import subprocess
# 转换为wav格式
wav_file = filePath.replace(".mp3", ".wav")
subprocess.call(["ffmpeg", "-i", filePath, "-ar", str(RATE), wav_file])
# 识别wav格式文件
result = recognize_wave_file(wav_file)
# 删除中间文件
subprocess.call(["rm", wav_file])
return result
# 测试
if __name__ == '__main__':
print(recognize_wave_file("test.wav"))
print(recognize_mp3_file("test.mp3"))
```
其中,需要注意的是:
1. 需要替换`URL`、`CUID`和`token`等参数为自己的;
2. `DEV_PID`表示识别语种,1536表示普通话;
3. 可以识别的音频格式为16位采样率为16kHz的wav格式或者128kbps的mp3格式,若要识别mp3格式文件,需要安装`ffmpeg`并将其加入环境变量。
阅读全文