(更新 —> Light 教程) goldendict + ocr屏幕取词 + 百度翻译 + goog
Tofloor
poster avatar
ylxdxx
deepin
2018-12-29 02:33
Author
本帖最后由 ylxdxx 于 2018-12-29 18:04 编辑

说明:
1,为 goldendict (词典应用)添加的功能:ocr屏幕取词 + 百度翻译 + google翻译
2,轻量级词典应用推荐 :https://github.com/ziqiangxu/words-pickerhttps://github.com/rekols/redict-qt5
3,需要使用的项目地址有(自己提前安装):https://github.com/goldendict/goldendict + https://github.com/ssut/py-googletrans + https://github.com/tesseract-ocr/tesseract


一,百度翻译
翻译接口需自己申请,通用的翻译每个月前200万字符免费,申请后拿到 appid 和 secretKey ,再新建一个后缀 .py 的文件填入,代码如下:
  1. #来源于:http://http://blog.csdn.net/lcyong_
  2. #coding: utf8

  3. import http.client
  4. import hashlib
  5. import json
  6. import urllib
  7. import random
  8. import sys

  9. def baidu_translate(content):
  10.     appid = ''
  11.     secretKey = ''
  12.     httpClient = None
  13.     myurl = '/api/trans/vip/translate'
  14.     q = content
  15.     fromLang = 'en' # 源语言
  16.     toLang = 'zh'   # 翻译后的语言
  17.     salt = random.randint(32768, 65536)
  18.     sign = appid + q + str(salt) + secretKey
  19.     sign = hashlib.md5(sign.encode()).hexdigest()
  20.     myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
  21.         q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
  22.         salt) + '&sign=' + sign

  23.     try:
  24.         httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
  25.         httpClient.request('GET', myurl)
  26.         # response是HTTPResponse对象
  27.         response = httpClient.getresponse()
  28.         jsonResponse = response.read().decode("utf-8")# 获得返回的结果,结果为json格式
  29.         js = json.loads(jsonResponse)  # 将json格式的结果转换字典结构
  30.         dst = str(js["trans_result"][0]["dst"])  # 取得翻译后的文本结果
  31.         print(dst) # 打印结果
  32.     except Exception as e:
  33.         print(e)
  34.     finally:
  35.         if httpClient:
  36.             httpClient.close()

  37. if __name__ == '__main__':
  38. #    while True:
  39. #       print("请输入要翻译的内容,如果退出输入q")
  40. #        content = input()
  41. #        if (content == 'q'):
  42. #            break
  43.         baidu_translate(sys.argv[1])
Copy the Code

然后在 goldendict 中的如图位置填写:


命令行一栏为:
  1. python3 /home/shui/Desktop/学习/translate-for-goldendict/Baidu/demo.py "%GDWORD%"
Copy the Code



二,Google翻译
依照上面差不多,不过这回是后缀为 .sh 的shell脚本,代码如下:

  1. #!/bin/bash

  2. #统计字数,对单个单词不作翻译
  3. num=$( echo "$1" | wc -w )

  4. if [ "$num" -ge "2" ] ;then
  5. #网络代理
  6.    export http_proxy="http://127.0.0.1:12333"
  7.    export https_proxy="http://127.0.0.1:12333"
  8.     translate -d zh-CN "$1"

  9. else
  10.     echo "^ _ ^"
  11. fi

  12. exit
Copy the Code
其中在 goldendict 中填入的命令为:
  1. /home/shui/Desktop/学习/translate-for-goldendict/Google/1.sh "%GDWORD%"
Copy the Code



三,OCR 屏幕取词
原理解说:利用深度截屏的自动保存功能将图片存储特定位置,再监控特定位置的文件变化,当有图片存入时,利用 tesseract 对图片进行识别,再将识别的内容穿给 goldendict ,从而实现所需功能。
说明:tesseract 自带的英文识别不是太好,可以到项目地址下载最新的英文识别训练文件,特殊识别可自行训练或使用各云端的OCR—api 来使用

脚本代码如下:
  1. #!/bin/bash
  2. #本地goldendict的OCR取词
  3. cd /home/shui/OCR #监控和图片存储目录
  4. while true;do
  5.      num=$(find -name "*.png" -print)  
  6.      if [ ! -n "$num" ]; then
  7.         sleep 1
  8.      else
  9.         mv *.png 1.png
  10.         tesseract 1.png ./out -l 1eng
  11.         var=$(cat out.txt)
  12.         goldendict "$var"
  13.         rm *.png
  14.         rm *.txt
  15.         sleep 1
  16.      fi
  17. done
Copy the Code
注:这个 shell 脚本需保持后台一直运行,占用内存也就几十kb

利用各种工具,外加各种组合就可以实现你想要的功能,在这里献丑了,当然,你可能有更好的方法、思路、点子,欢迎交流

效果预览:






Reply Favorite View the author
All Replies
avatar
ylxdxx
deepin
2018-12-29 02:35
#1
等忙过了,把过程记录下来
Reply View the author
avatar
176******78
deepin
2018-12-29 03:05
#2
楼主给力,大赞,坐等更新,话说,stardict通用吗?goldendict 太占资源
Reply View the author
avatar
176******78
deepin
2018-12-29 03:09
#3
楼主是调用了谷歌翻译的api么,求教程
Reply View the author
avatar
rekols
deepin
2018-12-29 03:44
#4
Reply View the author
avatar
ylxdxx
deepin
2018-12-29 07:22
#5
linuxchild 发表于 2018-12-28 19:09
楼主是调用了谷歌翻译的api么,求教程

GitHub的Google的api,不怕麻烦也可以自己抓包
Reply View the author
avatar
ylxdxx
deepin
2018-12-29 19:24
#6
https://bbs.deepin.org/post/173005
https://github.com/rekols/redict-qt5    写得很菜。

已提issues
Reply View the author
avatar
coder潘
deepin
2018-12-29 20:49
#7
https://bbs.deepin.org/post/173005
https://github.com/rekols/redict-qt5    写得很菜。

之前用过,ocr 取词功能应该没有吧,反正选择后,没有弹出翻译,后来卸载了,用了https://github.com/ziqiangxu/words-picker 很好用
Reply View the author
avatar
rekols
deepin
2018-12-29 21:50
#8
https://bbs.deepin.org/post/173005
之前用过,ocr 取词功能应该没有吧,反正选择后,没有弹出翻译,后来卸载了,用了https://github.com/ziq ...

后面我再完善一下
Reply View the author
avatar
ylxdxx
deepin
2018-12-30 01:54
#9
https://bbs.deepin.org/post/173005
楼主给力,大赞,坐等更新,话说,stardict通用吗?goldendict 太占资源

已更,stardict 我没看见程序插件功能,goldendict 不开全文搜索,编译最新版本解决内存问题,基本还行
Reply View the author