python调用百度语音识别实现大音频文件语音识别功能调用百度语音识别实现大音频文件语音识别功能
主要为大家详细介绍了python调用百度语音识别实现大音频文件语音识别功能,具有一定的参考价值,感兴趣
的小伙伴们可以参考一下
本文为大家分享了python实现大音频文件语音识别功能的具体代码,供大家参考,具体内容如下
实现思路:先用ffmpeg将其他非wav格式的音频转换为wav格式,并转换音频的声道(百度支持声道为1),采样率(值为
8000),格式转换完成后,再用ffmpeg将音频切成百度。
支持的时长(30秒和60秒2种,本程序用的是30秒)。
# coding: utf-8
import json
import time
import base64
from inc import rtysdb
import urllib2
import requests
import os
import uuid
from inc import db_config
class BaiduRest:
def __init__(self, cu_id, api_key, api_secert):
self.token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
self.getvoice_url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s"
self.upvoice_url = 'http://vop.baidu.com/server_api'
self.cu_id = cu_id
self.get_token(api_key, api_secert)
return
def get_token(self, api_key, api_secert):
token_url = self.token_url % (api_key, api_secert)
r_str = urllib2.urlopen(token_url).read()
token_data = json.loads(r_str)
self.token_str = token_data['access_token']
return True
# 语音合成
def text2audio(self, text, filename):
get_url = self.getvoice_url % (urllib2.quote(text), self.cu_id, self.token_str)
voice_data = urllib2.urlopen(get_url).read()
voice_fp = open(filename, 'wb+')
voice_fp.write(voice_data)
voice_fp.close()
return True
##语音识别
def audio2text(self, filename):
data = {}
data['format'] = 'wav'
data['rate'] = 8000
data['channel'] = 1
data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename, 'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
# data['speech'] = base64.b64encode(voice_data).decode('utf-8')
data['speech'] = base64.b64encode(voice_data).replace('', '')
# post_data = json.dumps(data)
result = requests.post(self.upvoice_url, json=data, headers={'Content-Type': 'application/json'})
data_result = result.json()
if(data_result['err_msg'] == 'success.'):
return data_result['result'][0]
else:
return False
def test_voice(voice_file):
api_key = "vossGHIgEETS6IMRxBDeahv8"
api_secert = "3c1fe6a6312f41fa21fa2c394dad5510"
bdr = BaiduRest("0-57-7B-9F-1F-A1", api_key, api_secert)
评论0
最新资源