forked from PantsuDango/Dango-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
216 lines (184 loc) · 6.54 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
from API import baidu_orc, youdao, caiyun, jinshan, yeekit, ALAPI, baidu, tencent, caiyunAPI
from baidufanyi import BaiduWeb
from Tencent import TencentTrans
from Google import GoogleTranslate
from Bing import BingTranslate
from PIL import ImageGrab
from skimage.measure import compare_ssim
from cv2 import imread,cvtColor,COLOR_BGR2GRAY
import time
import json
from pyperclip import copy
from traceback import print_exc
from PyQt5.QtWidgets import QApplication
# 截图
def image_cut(window, data):
x1 = data["range"]['X1']
y1 = data["range"]['Y1']
x2 = data["range"]['X2']
y2 = data["range"]['Y2']
try:
screen = QApplication.primaryScreen()
pix = screen.grabWindow(QApplication.desktop().winId(), x1, y1, x2-x1, y2-y1)
pix.save('.\\config\\image.jpg')
except Exception:
print_exc()
# 判断图片相似度
def compare_image(imageA,imageB):
grayA = cvtColor(imageA, COLOR_BGR2GRAY)
grayB = cvtColor(imageB, COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
score = float(score)
return score
# 翻译主函数
def translate(window, data):
text = window.translateText.toPlainText()
if text[:5] == "团子翻译器" or (not text[:1]):
score = 0.97
image_cut(window, data)
else:
imageA = imread('.\\config\\image.jpg')
image_cut(window, data)
imageB = imread('.\\config\\image.jpg')
try:
score = compare_image(imageA, imageB)
except Exception:
score = 0.97
if score < 0.98:
sign, original = baidu_orc(data)
if sign and original and (original != window.original):
# 过滤不需要加入翻译的字符
try:
with open(".\\config\\filter.txt") as file:
char = file.read()
char.split('''&''')
for ch in char:
original = original.replace(ch,'')
except Exception:
print_exc()
# 是否复制到剪贴板
if data["showClipboard"] == 'True':
copy(original)
youdaoUse = data["youdaoUse"] # 有道
caiyunUse = data["caiyunUse"] # 公共彩云
jinshanUse = data["jinshanUse"] # 金山
yeekitUse = data["yeekitUse"] #yeekit
alapiUse = data["alapiUse"] # alapi
baiduwebUse = data["baiduwebUse"] # 百度网页版
tencentwebUse = data["tencentwebUse"] # 腾讯网页版
googleUse = data["googleUse"] # google网页版
BingUse = data["BingUse"] # Bing网页版
baiduUse = data["baiduUse"] # 百度私人版
tencentUse = data["tencentUse"] # 腾讯私人版
caiyunPrivateUse = data["caiyunPrivateUse"] # 私人彩云
yeekitLanguage = data["yeekitLanguage"] # yeekit翻译语种
BingLanguage = data["BingLanguage"] # Bing翻译语种
# 有道
if youdaoUse == "True":
result_youdao = youdao(original)
else:
result_youdao = ''
# 公共彩云
if caiyunUse == "True":
result_caiyun = caiyun(original)
else:
result_caiyun = ''
# 金山
if jinshanUse == "True":
result_jinshan = jinshan(original)
else:
result_jinshan = ''
#yeekit
if yeekitUse == "True":
result_yeekit = yeekit(original, yeekitLanguage)
else:
result_yeekit = ''
# alapi
if alapiUse == "True":
result_alapi = ALAPI(original)
else:
result_alapi = ''
# 百度网页版
if baiduwebUse == "True":
baiduweb = BaiduWeb(original)
result_baiduweb = baiduweb.run()
else:
result_baiduweb = ''
# 腾讯网页版
if tencentwebUse == "True":
Tencent = TencentTrans()
result_tencentweb = Tencent.get_trans_result(original)
else:
result_tencentweb = ''
# 谷歌网页版
if googleUse == "True":
google = GoogleTranslate()
result_google = google.translate(original)
else:
result_google = ''
# Bing网页版
if BingUse == "True":
bing = BingTranslate()
result_Bing = bing.translate(BingLanguage, original)
else:
result_Bing = ''
# 百度私人版
if baiduUse == "True":
result_baidu = baidu(original, data)
else:
result_baidu = ''
# 腾讯私人版
if tencentUse == "True":
result_tencent = tencent(original, data)
else:
result_tencent = ''
# 彩云私人版
if caiyunPrivateUse == "True":
result_caiyunPrivate = caiyunAPI(original, data)
else:
result_caiyunPrivate = ''
else:
result_youdao = ''
result_caiyun = ''
result_jinshan = ''
result_yeekit = ''
result_alapi = ''
result_baiduweb = ''
result_tencentweb = ''
result_google = ''
result_Bing = ''
result_baidu = ''
result_tencent = ''
result_caiyunPrivate = ''
else:
result_youdao = ''
result_caiyun = ''
result_jinshan = ''
result_yeekit = ''
result_alapi = ''
result_baiduweb = ''
result_tencentweb = ''
result_google = ''
result_Bing = ''
result_baidu = ''
result_tencent = ''
result_caiyunPrivate = ''
original = ''
sign = True
result = dict()
result["youdao"] = result_youdao
result["caiyun"] = result_caiyun
result["jinshan"] = result_jinshan
result["yeekit"] = result_yeekit
result["alapi"] = result_alapi
result["baiduweb"] = result_baiduweb
result["tencentweb"] = result_tencentweb
result["google"] = result_google
result["Bing"] = result_Bing
result["baidu"] = result_baidu
result["tencent"] = result_tencent
result["caiyunPrivate"] = result_caiyunPrivate
result["original"] = original
result["sign"] = sign
return result