[voice] add google voice support

This commit is contained in:
wanggang
2023-03-07 14:29:59 +08:00
parent f2ae3e2fd8
commit d38fc61043
10 changed files with 132 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
"""
google voice service
"""
import subprocess
import speech_recognition
from voice.voice import Voice
class GoogleVoice(Voice):
recognizer = speech_recognition.Recognizer()
def __init__(self):
pass
def voiceToText(self, voice_file):
new_file = voice_file.replace('.mp3', '.wav')
subprocess.call('ffmpeg -i ' + voice_file + ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
with speech_recognition.AudioFile(new_file) as source:
audio = self.recognizer.record(source)
return self.recognizer.recognize_google(audio, language='zh-CN')

10
voice/voice.py Normal file
View File

@@ -0,0 +1,10 @@
"""
Voice service abstract class
"""
class Voice(object):
def voiceToText(self, voice_file):
"""
Send voice to voice service and get text
"""
raise NotImplementedError

17
voice/voice_factory.py Normal file
View File

@@ -0,0 +1,17 @@
"""
voice factory
"""
def create_voice(voice_type):
"""
create a voice instance
:param voice_type: voice type code
:return: voice instance
"""
if voice_type == 'xfyun':
from voice.xfyun.xfyun_voice import XfyunVoice
return XfyunVoice()
elif voice_type == 'google':
from voice.google.google_voice import GoogleVoice
return GoogleVoice()
raise RuntimeError

View File

@@ -0,0 +1,35 @@
"""
科大讯飞 voice service
"""
from voice.voice import Voice
# 科大讯飞语音识别
lfasr_host = 'http://raasr.xfyun.cn/api'
# 请求的接口名
api_prepare = '/prepare'
api_upload = '/upload'
api_merge = '/merge'
api_get_progress = '/getProgress'
api_get_result = '/getResult'
# 文件分片大小10M
file_piece_sice = 10485760
# ——————————————————转写可配置参数————————————————
# 参数可在官网界面https://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E8%BD%AC%E5%86%99.html查看根据需求可自行在gene_params方法里添加修改
# 转写类型
lfasr_type = 0
# 是否开启分词
has_participle = 'false'
has_seperate = 'true'
# 多候选词个数
max_alternatives = 0
# 子用户标识
suid = ''
class XfyunVoice(Voice):
def __init__(self):
pass
def voiceToText(self, voice_file):
pass