加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wav2text.py 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from aip import AipSpeech
import speech_recognition as sr
#import Sphinxbase
import pocketsphinx
#从百度AI开放平台创建应用处获取
APP_ID = '17307022'
API_KEY = 'Gp5H0e6fmvRQWKSk5aYgnViG'
SECRET_KEY = 'Hq7BVIpAuxijNP2GNSILHSBnF0oiCLNz '
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 读取文件
#def get_file_content(filePath):
# with open(filePath, 'rb') as fp:
# return fp.read()
# 识别本地文件
def get_text(audio):
#result = client.asr(get_file_content('/home/pi/modules.ai/tmp.wave/record.wav'), 'wav', 16000, {
result = client.asr(audio, 'wav', 16000, {
'dev_pid': 1536,})
print(result)
text = result['result'][0]
return text
# 进行离线语音识别
def get_text_offline(audio):
r = sr.Recognizer()
print(r)
try:
text = r.recognize_sphinx(audio)
print(text)
return text
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
if __name__ == '__main__':
get_text()
print(get_text())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化