# -*- coding: utf-8 -*-
########################################################################
#
# Copyright (c) 2017 aibot.me, Inc. All Rights Reserved
#
########################################################################
"""
File: util_voice.py
Author: darrenwang([email protected])
Date: 2017/03/24 11:29:50
Brief:
"""
import sys
import json
import time
import base64
import urllib
import urllib2
import requests
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('\n', '')
#post_data = json.dumps(data)
result = requests.post(self.upvoice_url, json=data, headers={'Content-Type': 'application/json'})
data_result = result.json()
print data_result
return data_result['result'][0]
def test_voice():
api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
bdr = BaiduRest("test_python", api_key, api_secert)
#生成
start = time.time()
bdr.text2audio("你好啊", "out.wav")
using = time.time() - start
print using
#识别
start = time.time()
#result = bdr.audio2text("test.wav")
#result = bdr.audio2text("weather.pcm")
using = time.time() - start
print using, result
return True
if __name__ == "__main__":
test_voice()
调用腾讯云的语音识别(一句话识别)接口-Python版。安装语音识别,语音识别器编码,处理Ubuntu服务器,处理WSL。
要运行我们代码的语音识别库,我们首先需要安装语音识别,然后还必须安装PyAudio。首先,我们从主包开始:sudo pip3 install SpeechRecognition安装应该遵循完全相同的格式,但我似乎缺少了让它正常工作的软件包,试图安装PyAudio会出错。这些软件包应该删除该错误。那时我不需要更新apt,但先更新它并不坏处。sudo apt-get install libasound-dev portaudio19-dev libportaudio2libportaudiocpp0有了它,您应该可以安装PyAudio:sudo pip3 install PyAudio语音识别器编码。
运行WSL(Linux版Windows子系统)的Windows机器。它也使用了Ubuntu 16.04,所以安装过程是一样的。不过说到使用麦克风,WSL并不那么简单。为了通过Ubuntu终端应用程序控制麦克风,需要安装PulseAudio。
python语音识别源于 20 世纪 50 年代早期在贝尔实验室所做的研究。早期语音识别系统仅能识别单个讲话者以及只有约十几个单词的词汇量。现代语音识别系统已经取得了很大进步,可以识别多个讲话者,并且拥有识别多种语言的庞大词汇表。语音识别的首要部分当然是语音。通过麦克风,语音便从物理声音被转换为电信号,然后通过模数转换器转换为数据。一旦被数字化,就可适用若干种模型,将音频转录为文本。
大多数现代语音识别系统都依赖于隐马尔可夫模型(HMM)。其工作原理为:语音信号在非常短的时间尺度上(比如 10 毫秒)可被近似为静止过程,即一个其统计特性不随时间变化的过程。