python开发一款翻译工具


Posted in Python onOctober 10, 2020

最近,某水果手机厂在万众期待中开了一场没有发布万众期待的手机产品的发布会,发布了除手机外的其他一些产品,也包括最新的水果14系统。几天后,更新了系统的吃瓜群众经过把玩突然发现新系统里一个超有意思的功能——翻译,比如这种:

python开发一款翻译工具

奇怪的翻译知识增加了!

相比常见的翻译工具,同声翻译工具更具有实用价值,想想不精通其他语言就能和歪果朋友无障碍交流的场景,真是一件美事,不如自己动手实现个工具备用!一个同声翻译工具,逻辑大概可以是先识别,而后翻译,翻译能否成功,识别的准确率是个关键因素。为了降低难度,我决定分两次完成工具开发。首先来实现试试语音识别的部分。

轻车熟路,本次的demo继续调用有道智云API,实现实时语音识别。

效果展示

先看看界面和结果哈:

可以选择多种语音,这里只写了四种常见的:

python开发一款翻译工具

偶分别测试的中文、韩文、英文。看着还不错哦~

python开发一款翻译工具

调用API接口的准备工作

首先,是需要在有道智云的个人页面上创建实例、创建应用、绑定应用和实例,获取调用接口用到的应用的id和密钥。具体个人注册的过程和应用创建过程详见文章分享一次批量文件翻译的开发过程

python开发一款翻译工具

开发过程详细介绍

下面介绍具体的代码开发过程。

首先是根据实时语音识别文档来分析接口的输入输出。接口设计的目的是对连续音频流的实时识别,转换成文本信息并返对应文字流,因此通信采用websocket,调用过程分为认证、实时通信两阶段。

在认证阶段,需发送以下参数:

参数 类型 必填 说明 示例
appKey String 已申请的应用ID ID
salt String UUID UUID
curtime String 时间戳(秒) TimeStamp
sign String 加密数字签名。 sha256
signType String 数字签名类型 v4
langType String 语言选择,参考支持语言列表 zh-CHS
format String 音频格式,支持wav wav
channel String 声道,支持1(单声道) 1
version String api版本 v1
rate String 采样率 16000

签名sign生成方法如下:
signType=v4;
sign=sha256(应用ID+salt+curtime+应用密钥)。

认证之后,就进入了实时通信阶段,发送音频流,获取识别结果,最后发送结束标志结束通信,这里需要注意的是,发送的音频最好是16bit位深的单声道、16k采样率的清晰的wav音频文件,这里我开发时最开始因为音频录制设备有问题,导致音频效果极差,接口一直返回错误码304(手动捂脸)。

Demo开发:

这个demo使用python3开发,包括maindow.py,audioandprocess.py,recobynetease.py三个文件。界面部分,使用python自带的tkinter库,来进行语言选择、录音开始、录音停止并识别的操作。audioandprocess.py实现了录音、音频处理的逻辑,最后通过recobynetease.py中的方法来调用实时语音识别API。

1.界面部分:

主要元素:

root=tk.Tk()
root.title("netease youdao translation test")
frm = tk.Frame(root)
frm.grid(padx='80', pady='80')
# label1=tk.Label(frm,text="选择待翻译文件:")
# label1.grid(row=0,column=0)
label=tk.Label(frm,text='选择语言类型:')
label.grid(row=0,column=0)
combox=ttk.Combobox(frm,textvariable=tk.StringVar(),width=38)
combox["value"]=lang_type_dict
combox.current(0)
combox.bind("<<ComboboxSelected>>",get_lang_type)
combox.grid(row=0,column=1)

btn_start_rec = tk.Button(frm, text='开始录音', command=start_rec)
btn_start_rec.grid(row=2, column=0)

lb_Status = tk.Label(frm, text='Ready', anchor='w', fg='green')
lb_Status.grid(row=2,column=1)

btn_sure=tk.Button(frm,text="结束并识别",command=get_result)
btn_sure.grid(row=3,column=0)

root.mainloop()

2.音频录制部分,引入pyaudio库(需通过pip安装)来调用音频设备,录制接口要求的wav文件,并通过wave库存储文件:

def __init__(self, audio_path, language_type,is_recording):
 self.audio_path = audio_path,
 self.audio_file_name=''
 self.language_type = language_type,
 self.language=language_dict[language_type]
 print(language_dict[language_type])
 self.is_recording=is_recording
 self.audio_chunk_size=1600
 self.audio_channels=1
 self.audio_format=pyaudio.paInt16
 self.audio_rate=16000

def record_and_save(self):
 self.is_recording = True
 # self.audio_file_name=self.audio_path+'/recordtmp.wav'
 self.audio_file_name='/recordtmp.wav'

 threading.Thread(target=self.record,args=(self.audio_file_name,)).start()

def record(self,file_name):
 print(file_name)
 p=pyaudio.PyAudio()
 stream=p.open(
 format=self.audio_format,
 channels=self.audio_channels,
 rate=self.audio_rate,
 input=True,
 frames_per_buffer=self.audio_chunk_size
 )
 wf = wave.open(file_name, 'wb')
 wf.setnchannels(self.audio_channels)
 wf.setsampwidth(p.get_sample_size(self.audio_format))
 wf.setframerate(self.audio_rate)

 # 读取数据写入文件
 while self.is_recording:
 data = stream.read(self.audio_chunk_size)
 wf.writeframes(data)
 wf.close()
 stream.stop_stream()
 stream.close()
 p.terminate()

3.翻译接口调用部分:

def recognise(filepath,language_type):
 global file_path
 file_path=filepath
 nonce = str(uuid.uuid1())
 curtime = str(int(time.time()))
 signStr = app_key + nonce + curtime + app_secret
 print(signStr)
 sign = encrypt(signStr)

 uri = "wss://openapi.youdao.com/stream_asropenapi?appKey=" + app_key + "&salt=" + nonce + "&curtime=" + curtime + \
  "&sign=" + sign + "&version=v1&channel=1&format=wav&signType=v4&rate=16000&langType=" + language_type
 print(uri)
 start(uri, 1600)


def encrypt(signStr):
 hash = hashlib.sha256()
 hash.update(signStr.encode('utf-8'))
 return hash.hexdigest()



def on_message(ws, message):
 result=json.loads(message)
 try:
 resultmessage1 = result['result'][0]
 resultmessage2 = resultmessage1["st"]['sentence']
 print(resultmessage2)
 except Exception as e:
 print('')

def on_error(ws, error):
 print(error)


def on_close(ws):
 print("### closed ###")


def on_open(ws):
 count = 0
 file_object = open(file_path, 'rb')
 while True:
 chunk_data = file_object.read(1600)
 ws.send(chunk_data, websocket.ABNF.OPCODE_BINARY)
 time.sleep(0.05)
 count = count + 1
 if not chunk_data:
  break
 print(count)
 ws.send('{\"end\": \"true\"}', websocket.ABNF.OPCODE_BINARY)



def start(uri,step):

 websocket.enableTrace(True)

 ws = websocket.WebSocketApp(uri,
    on_message=on_message,
    on_error=on_error,
    on_close=on_close)

 ws.on_open = on_open
 ws.run_forever()

总结

有道智云提供的接口一如既往的好用,这次开发主要的精力全都浪费在了由于我自己录制的音频质量差而识别失败的问题上,音频质量ok后,识别结果准确无误,下一步就是拿去翻译了,有了有道智云API,实现实时翻译也可以如此简单!

以上就是python开发一款翻译工具的详细内容,更多关于python开发翻译工具的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
分析在Python中何种情况下需要使用断言
Apr 01 Python
Python实现遍历目录的方法【测试可用】
Mar 22 Python
python urllib爬取百度云连接的实例代码
Jun 19 Python
使用python将大量数据导出到Excel中的小技巧分享
Jun 14 Python
python 定时器,实现每天凌晨3点执行的方法
Feb 20 Python
Python中的几种矩阵乘法(小结)
Jul 10 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
Aug 04 Python
Win10+GPU版Pytorch1.1安装的安装步骤
Sep 27 Python
Python selenium抓取虎牙短视频代码实例
Mar 02 Python
django form和field具体方法和属性说明
Jul 09 Python
Python中Qslider控件实操详解
Feb 20 Python
python爬虫破解字体加密案例详解
Mar 02 Python
Python pickle模块常用方法代码实例
Oct 10 #Python
Python3.9新特性详解
Oct 10 #Python
Python random模块的使用示例
Oct 10 #Python
python 装饰器的使用示例
Oct 10 #Python
python使用bs4爬取boss直聘静态页面
Oct 10 #Python
通过案例解析python鸭子类型相关原理
Oct 10 #Python
通过实例解析python subprocess模块原理及用法
Oct 10 #Python
You might like
php面向对象的方法重载两种版本比较
2008/09/08 PHP
PHP similar_text 字符串的相似性比较函数
2010/05/26 PHP
php开启与关闭错误提示适用于没有修改php.ini的权限
2014/10/16 PHP
php中使用session防止用户非法登录后台的方法
2015/01/27 PHP
学习php设计模式 php实现单例模式(singleton)
2015/12/07 PHP
反射调用private方法实践(php、java)
2015/12/21 PHP
微信公众号开发之通过接口删除菜单
2017/02/20 PHP
PHP实现的超长文本分页显示功能示例
2018/06/04 PHP
php5.5使用PHPMailer-5.2发送邮件的完整步骤
2018/10/14 PHP
PHP $O00OO0=urldecode &amp; eval 解密,记一次商业源码的去后门
2020/09/13 PHP
javascript Firefox与IE 替换节点的方法
2010/02/24 Javascript
基于JQuery的asp.net树实现代码
2010/11/30 Javascript
JavaScript 变量作用域分析
2011/07/04 Javascript
火狐textarea输入法的bug的触发及解决
2013/07/24 Javascript
js实现页面转发功能示例代码
2013/08/05 Javascript
一个非常全面的javascript URL解析函数和分段URL解析方法
2014/04/12 Javascript
使用Nodejs开发微信公众号后台服务实例
2014/09/03 NodeJs
让JavaScript的Alert弹出框失效的方法禁止弹出警告框
2014/09/03 Javascript
JS 终止执行的实现方法
2016/11/24 Javascript
jQuery 实现鼠标画框并对框内数据选中的实例代码
2017/08/29 jQuery
微信小程序实现图片预览功能
2018/01/31 Javascript
jQuery幻灯片插件owlcarousel参数说明中文文档
2018/02/27 jQuery
vue3.0 搭建项目总结(详细步骤)
2019/05/20 Javascript
jQuery实现飞机大战小游戏
2020/07/05 jQuery
[01:44]Ti10举办地公布
2019/08/25 DOTA
Django的models模型的具体使用
2019/07/15 Python
Python MySQLdb 执行sql语句时的参数传递方式
2020/03/04 Python
Django框架models使用group by详解
2020/03/11 Python
Django设置Postgresql的操作
2020/05/14 Python
Python while true实现爬虫定时任务
2020/06/08 Python
Scrapy框架介绍之Puppeteer渲染的使用
2020/06/19 Python
浅谈TensorFlow中读取图像数据的三种方式
2020/06/30 Python
解决python pandas读取excel中多个不同sheet表格存在的问题
2020/07/14 Python
称象教学反思
2014/02/03 职场文书
因身体原因离职的辞职信范文
2015/05/12 职场文书
Nginx+Tomcat负载均衡多实例详解
2022/04/11 Servers