Skip to content

Commit

Permalink
ocrtranslater
Browse files Browse the repository at this point in the history
  • Loading branch information
Usagiloli committed Jun 17, 2021
1 parent 12da9ee commit ca14d58
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
32 changes: 32 additions & 0 deletions txtmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tmt.v20180321 import tmt_client, models

def txfanyi(input,st1,st2):
try:
cred = credential.Credential(st1, st2)
httpProfile = HttpProfile()
httpProfile.endpoint = "tmt.tencentcloudapi.com"

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = tmt_client.TmtClient(cred, "ap-beijing", clientProfile)

req = models.TextTranslateBatchRequest()
params = {
"Source": "auto",
"Target": "zh",
"ProjectId": 0,
"SourceTextList": [input]
}
req.from_json_string(json.dumps(params))

resp = client.TextTranslateBatch(req)
output=json.loads(resp.to_json_string())["TargetTextList"][0]
return output

except TencentCloudSDKException as err:
print(err)
108 changes: 108 additions & 0 deletions usarot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from txtmt import txfanyi
import cv2
import numpy as np
import pyautogui as pag
from time import sleep
import pynput
import pytesseract
import re
import configparser

def press(x,y,but,p):
if(p):
return False
def unpress(x,y,but,p):
if(not p):
return False

def waitclick(fx=press):
with pynput.mouse.Listener(on_click=fx) as linster:
linster.join()

def getpos3(sample=1):
while (1):
pos1 = pag.position()
sleep(sample/2)
pos2 = pag.position()
if (pos1 != pos2):
continue
sleep(sample/2)
pos1 = pag.position()
if (pos1 == pos2):
return(pos1)

def getrgn(sample=1,mode='pos3'):
while(1):
if mode=='pos3':
pag.alert(text='getrgn,pos3')
pos1=getpos3(sample)
print(pos1)
pos2=getpos3(sample)
print(pos2)
elif mode=='clk':
pag.alert(text='getrgn,clk')
waitclick(press)
pos1=pag.position()
waitclick(unpress)
pos2=pag.position()

if (pos2[0] - pos1[0] > 0 and pos2[1] - pos1[1] > 0):
break
else:
print("Out of range")
rgn = (pos1[0], pos1[1], pos2[0] - pos1[0], pos2[1] - pos1[1])
#print(rgn)
return rgn


def resformat(input):
input=re.sub('\\s+',' ',input)
return input

#res=pag.size()
#print(res)

def shotrgn(rgn):
fr = pag.screenshot(region=rgn)
img = np.asarray(fr)
imgcv = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# imgcv=cv2.bilateralFilter(imgcv,5,21,21)
imgcv = cv2.threshold(imgcv, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# imgcv=cv2.medianBlur(imgcv,1)
sleep(0.1)
return imgcv


def sltrgn(mode='pos3'):
while(1):
rgn=getrgn(mode=mode)
sleep(0.1)
imgcv=shotrgn(rgn)
cv2.imshow("opencv", imgcv)
cv2.waitKey()
cv2.destroyAllWindows()
if(pag.confirm("OK?") == 'OK'):
break
return rgn


cf=configparser.ConfigParser()
cf.read("usarot.cfg")
mode = cf.get("cfg","mode")
st1 = cf.get("cfg","id")
st2 = cf.get("cfg","key")

rgn=sltrgn(mode)

while(1):
imgcv=shotrgn(rgn)
code=pytesseract.image_to_string(imgcv,lang='eng')
resformat(code)
print(code)
fanyi=txfanyi(code,st1,st2)
fanyi=resformat(fanyi)
print(fanyi)
waitclick()

0 comments on commit ca14d58

Please sign in to comment.