程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Screen translation of mouse delimited area python3

編輯:Python

Go to the comics if you have nothing to do , But a lot of raw meat is hard to chew , It's also not good to type and translate one word at a time , So write a screen translation , Press the shortcut key at any position and the mouse delimits the rectangular area to identify the translation

be based on easyOCR Interface with Youdao translation ( Refer to what the boss wrote Python 3 The latest Youdao translation crawl , Crack the anti climbing mechanism , solve {“errorCode”:50} error )

The effect is as follows ( The recognition rate and speed depend on OCR Speed and translation interface network speed )

Mainly used libraries :
PySimpleGUIQt
pynput
PIL
easyocr
numpy

Need to download before use easyOCR Model of , If you want to GPU If you want to recognize the acceleration, you need to download CUDA

Code ability is not good , Look at the big guys Haihan , generous with your criticism

Code :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @FileName: main.py
import PySimpleGUIQt as sg
import pynput
from PIL import ImageGrab
import easyocr
import numpy as np
import json
import youdao
key_start = "f10"
key_end = "f9"
#languages = ['en', 'ch_sim']
languages = ['en']
language_src = 'en'
language_dest = 'zh-CHS'
GPU_CUDA_ONOROFF = True
# Use GPU Acceleration will be many times faster ( It is said that ), Need to download CUDA
MODULE_PATH = r'.\module'
# Model address , Because I don't like the default c Disk path 
D = "Y2FYu%TNSbMCxc3t2u^XT"
# Youdao translation generates salt The key of , This will change , So it's easy to change 
def listen_keyboark(key_start, key_end):
# Listen for shortcuts in the background 
# Use this function to block , Until get shortcut_key_start, It's a strange idea hh
with pynput.keyboard.Events() as event:
for i in event:
if isinstance(i, pynput.keyboard.Events.Press):
try:
if i.key.name == key_start:
return True
elif i.key.name == key_end:
return False
except AttributeError:
# This means that this is an ordinary key .
if i.key.char == key_start:
return True
elif i.key.char == key_end:
return False
return False
def get_xy():
# Monitor the position coordinates when the mouse is pressed and released 
x1, y1, x2, y2 = 0,0,0,0
with pynput.mouse.Events() as event:
for i in event:
if isinstance(i, pynput.mouse.Events.Click):
# Mouse click event .
#print(i.x, i.y, i.button, i.pressed)
# This i.button That's what I said above “ Mouse button ” One of them , use is Sentence judgment is enough .
if(i.button == pynput.mouse.Button.left):
if(i.pressed == True):
x1, y1 = i.x, i.y
if(i.pressed == False):
x2, y2 = i.x, i.y
break
return x1, y1, x2, y2
def screenshot(xyxy):
# The simple screenshot is converted to OCR acceptable np
im = ImageGrab.grab(xyxy)
#im.show()
im = np.array(im)
return im
def OCR(languages, im):
#OCR distinguish 
reader = easyocr.Reader(languages, gpu=GPU_CUDA_ONOROFF, model_storage_directory = MODULE_PATH)
result = reader.readtext(im, detail = 0)
str = ""
for i in result:
str = str + " " + i
str = str[1:]
return str
def language_translator(str, y, src, dest):
# Translation interface 
y.set_msg(str)
return y.get_result()
def show(str, xyxy):
# Use PySimpleGUIQt draw , The goal is to be simple without borders , You can modify the color layout 
# After clicking, enter any key on the keyboard to close the window 
x1, y1, x2, y2 = xyxy
size = (abs(x1-x2), abs(y1-y2))
layout = [[sg.Multiline(str)]]
window = sg.Window('', layout=layout, return_keyboard_events=True, keep_on_top=True, no_titlebar=True, alpha_channel=0.8, size=size, location=[x1, y1])
while True:
event,values = window.read()
#return_keyboard_events=True Return the keyboard keys to event, It won't block 
# Here is any key to close , If you want to close a specific key, you need to judge 
window.close()
break
def main():
while listen_keyboark(key_start, key_end):
xyxy = get_xy()
im = screenshot(xyxy)
str = OCR(languages, im)
print(str)
y = youdao.Youdao(D)
str = language_translator(str, y, language_src, language_dest)
show(str, xyxy)
if __name__ == '__main__':
main()

Proper translation :
tips: There's a pit here , What was originally used was googletrans, Then I found that the translation results were incomparable , Switch to the Tao http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule Interface ( Notice that it's not translate_o), Then I found that translation is still grass , Go to Youdao translation website to try , As a result, the translation is completely different , After looking at the front end, I found that it calls http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule Yes translate_o edition , After careful study, it is found that the former is not encrypted but the translation result is very poor , The translation result of the latter is very good, but it is encrypted , But encryption is on the front end , It was written by the boss Python 3 The latest Youdao translation crawl , Crack the anti climbing mechanism , solve {“errorCode”:50} error solve the problem

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @FileName: youdao.py
import hashlib
import random
import time
import urllib.request as requests
import json
import urllib.parse
""" Send... To Youdao translator data, Get the translation results """
class Youdao:
def __init__(self, D):
self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
self.D = D #fanyi.min.js lookup salt
def set_msg(self, msg):
self.msg = msg
self.salt = self.get_salt()
self.sign = self.get_sign()
self.ts = self.get_ts()
def get_md(self, value):
# md5 encryption 
m = hashlib.md5()
# m.update(value)
m.update(value.encode('utf-8'))
return m.hexdigest()
def get_salt(self):
# Get... Based on the current timestamp salt Parameters 
s = int(time.time() * 1000) + random.randint(0, 10)
return str(s)
def get_sign(self):
# Use md5 Functions and other parameters , obtain sign Parameters 
s = "fanyideskweb" + self.msg + self.salt + self.D
return self.get_md(s)
def get_ts(self):
# Get... Based on the current timestamp ts Parameters 
s = int(time.time() * 1000)
return str(s)
def get_result(self):
Form_Data = {

'i': self.msg,
'type': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': self.salt,
'sign': self.sign,
'ts': self.ts,
'bv': 'c6b8c998b2cbaa29bd94afc223bc106c',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'ue' : 'UTF-8',
'typoResult': 'true',
'action': 'FY_BY_CLICKBUTTION'
}
Form_Data = urllib.parse.urlencode(Form_Data).encode('utf-8')
headers = {

# 'Accept': 'application/json, text/javascript, */*; q=0.01',
# 'Accept-Encoding': 'gzip, deflate',
# 'Accept-Language': 'zh-CN,zh;q=0.9,mt;q=0.8',
# 'Connection': 'keep-alive',
# 'Content-Length': '240',
# 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': '[email protected];',
# 'Host': 'fanyi.youdao.com',
# 'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:51.0) Gecko/20100101 Firefox/51.0',
# 'X-Requested-With': 'XMLHttpRequest'
}
req = requests.Request(self.url, Form_Data, headers, method='POST')
response = requests.urlopen(req)
html = response.read().decode('utf-8')
translate_results = json.loads(html)
# Find the translation result 
if 'translateResult' in translate_results:
#translate_results = translate_results['translateResult'][0][0]['tgt']
#print(" The result of translation is :%s" % translate_results)
dest_str = ""
for i in translate_results['translateResult'][0]:
dest_str = dest_str + i['tgt']
#print(dest_str)
return dest_str
else:
#print(translate_results)
return translate_results
if __name__ == "__main__":
D = "Y2FYu%TNSbMCxc3t2u^XT"
msg = 'I love you'
y = Youdao(D)
y.set_msg(msg)
y.get_result()

Reference resources :

Python 3 The latest Youdao translation crawl , Crack the anti climbing mechanism , solve {“errorCode”:50} error )


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved