python语音识别代码_python实现百度语音识别api
时间: 2023-09-03 15:06:33 浏览: 110
好的,我可以为您提供一份Python语音识别代码,实现百度语音识别API的功能。请注意,在使用之前需要先进行百度AI开发者账号的注册和创建应用,获取API Key和Secret Key。
```python
# 导入需要的模块
import os
import time
import urllib.request
import urllib.parse
import json
import base64
import hashlib
import pyaudio
# 设置API Key和Secret Key
API_KEY = 'your_api_key'
SECRET_KEY = 'your_secret_key'
# 获取Token,用于后续的API调用
def get_token():
token_url = 'https://aip.baidubce.com/oauth/2.0/token'
grant_type = 'client_credentials'
token_data = {'grant_type': grant_type, 'client_id': API_KEY, 'client_secret': SECRET_KEY}
token_data_encoded = urllib.parse.urlencode(token_data).encode('utf-8')
token_request = urllib.request.Request(token_url, token_data_encoded)
token_response = urllib.request.urlopen(token_request)
token_content = token_response.read().decode('utf-8')
token_json = json.loads(token_content)
access_token = token_json['access_token']
return access_token
# 进行语音识别
def speech_recognition():
token = get_token()
speech_url = 'https://vop.baidu.com/server_api'
speech_data = {
'format': 'pcm',
'rate': 16000,
'channel': 1,
'cuid': 'python',
'token': token,
'speech': '',
'len': 0,
'dev_pid': 1536 # 1536为普通话,其它语种需要修改为对应的dev_pid
}
speech_data_encoded = json.dumps(speech_data).encode('utf-8')
speech_request = urllib.request.Request(speech_url, speech_data_encoded)
speech_request.add_header('Content-Type', 'application/json')
speech_response = urllib.request.urlopen(speech_request)
speech_content = speech_response.read().decode('utf-8')
speech_json = json.loads(speech_content)
result = speech_json['result'][0].strip()
return result
# 录音,用于获取语音数据
def record_audio():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = 'audio.wav'
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
frames = []
print('开始录音...')
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print('录音结束!')
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
if __name__ == '__main__':
while True:
input('按下回车键开始录音,录音结束后按下回车键停止录音...')
record_audio()
result = speech_recognition()
print('语音识别结果:', result)
```
以上代码实现了一个简单的语音识别程序,可以通过调用百度语音识别API实现语音转文字的功能。需要注意的是,该程序需要使用pyaudio和wave两个Python模块,需要提前安装。另外,在使用之前需要修改代码中的API Key和Secret Key为自己的值。
阅读全文