实现自动输入密码
This commit is contained in:
239
touch2.py
Normal file
239
touch2.py
Normal file
@@ -0,0 +1,239 @@
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
import serial
|
||||
|
||||
from ocr import get_text, mouse_touch, get_scr, get_boll_count
|
||||
from log import log_init, myprint
|
||||
from win32 import get_wind_pos
|
||||
|
||||
# 识别文字的截图区域
|
||||
_ocr_pos=[1545,157,367,384]
|
||||
|
||||
# 识别小圆点的截图区域
|
||||
_ocr_boll=[1644,440+3,172,27]
|
||||
|
||||
# 自动点击屏幕的区域
|
||||
_mouse_touch=[1550,760,140,70]
|
||||
|
||||
# 获取最新的窗口位置
|
||||
def flush_wind_pos():
|
||||
global _ocr_pos,_ocr_boll,_mouse_touch
|
||||
not_find=False
|
||||
while(True):
|
||||
pos=get_wind_pos()
|
||||
if(pos[0]<=0 and pos[1]<=0 and pos[2]<=0 and pos[3]<=0):
|
||||
if not_find==False:
|
||||
myprint("未找到相机窗口")
|
||||
not_find=True
|
||||
time.sleep(1)
|
||||
continue
|
||||
_ocr_pos=[pos[0]+0,pos[1]+180,375,296]
|
||||
_ocr_boll=[pos[0]+99,pos[1]+440,172,27]
|
||||
_mouse_touch=[pos[0]+0,pos[1]+603,145,70]
|
||||
return
|
||||
|
||||
# 识别屏幕文字
|
||||
def identify_text():
|
||||
flush_wind_pos()
|
||||
mouse_touch(_mouse_touch[0],_mouse_touch[1],_mouse_touch[2],_mouse_touch[3])
|
||||
text=get_text(_ocr_pos[0],_ocr_pos[1],_ocr_pos[2],_ocr_pos[3])
|
||||
print(text)
|
||||
return text
|
||||
def find_text(text:str,key:str):
|
||||
for item in text:
|
||||
if(item.find(key)>=0):
|
||||
return True
|
||||
return False
|
||||
|
||||
# 识别已输入数量
|
||||
def find_input_count():
|
||||
flush_wind_pos()
|
||||
ret=get_boll_count(_ocr_boll[0],_ocr_boll[1],_ocr_boll[2],_ocr_boll[3])
|
||||
if(ret[0]==6):
|
||||
return ret[1]
|
||||
myprint("未识别到已输入个数")
|
||||
return 0
|
||||
|
||||
def data_save(data:dict):
|
||||
data_file="index.json"
|
||||
with open(data_file,mode='w+') as f:
|
||||
f.write(json.dumps(data,sort_keys=True, indent=2, separators=(',', ': ')))
|
||||
|
||||
|
||||
|
||||
class ser(object):
|
||||
# 1 x_step 18 y_step 15
|
||||
step_x=16
|
||||
step_y=14
|
||||
touch_table={
|
||||
"1":(53,70),
|
||||
"2":(53+step_x,70),
|
||||
"3":(53+step_x*2,70),
|
||||
"4":(53,70+step_y),
|
||||
"5":(53+step_x,70+step_y),
|
||||
"6":(53+step_x*2,70+step_y),
|
||||
"7":(53,70+step_y*2),
|
||||
"8":(53+step_x,70+step_y*2),
|
||||
"9":(53+step_x*2,70+step_y*2),
|
||||
"0":(53+step_x,70+step_y*3),
|
||||
}
|
||||
def __init__(self):
|
||||
self.ser=None
|
||||
self.x=0
|
||||
self.y=0
|
||||
def open(self,com:str):
|
||||
try:
|
||||
self.ser = serial.Serial(port=com, baudrate=93450, timeout=60)
|
||||
except Exception:
|
||||
print(f"serial com:{com} open failed.")
|
||||
sys.exit(-1)
|
||||
time.sleep(2)
|
||||
def close(self):
|
||||
if not (self.ser is None):
|
||||
self.ser.close()
|
||||
self.ser=None
|
||||
def write(self,t:str):
|
||||
# print("send:",t)
|
||||
if not (self.ser is None):
|
||||
self.ser.write(t.encode(encoding="utf-8"))
|
||||
def togo(self,x:int,y:int):
|
||||
if(x<0 or x>100 or y<0 or y>150):
|
||||
print(f"位置超出范围 x={x}, y={y}")
|
||||
return
|
||||
self.write(f"X{x}Y{y}\r")
|
||||
# 电机运行速度160mm/s
|
||||
disx=abs(self.x-x)
|
||||
disy=abs(self.y-y)
|
||||
time.sleep((disx+disy)/160+0.1)
|
||||
self.x=x
|
||||
self.y=y
|
||||
def touch(self):
|
||||
self.write(f"Z6\r")
|
||||
time.sleep(0.1)
|
||||
self.write(f"Z0\r")
|
||||
time.sleep(0.05)
|
||||
def reset(self):
|
||||
self.write(f"Z0\r")
|
||||
time.sleep(0.05)
|
||||
self.togo(0,0)
|
||||
def enter(self):
|
||||
self.togo(70+20,27)
|
||||
self.touch()
|
||||
def back(self):
|
||||
self.togo(70,132)
|
||||
self.touch()
|
||||
# 避让摄像头 也是在取消按钮的位置
|
||||
def avoid(self):
|
||||
self.togo(70,132)
|
||||
# 按键位置测试
|
||||
def password_test(self,p:str):
|
||||
for i in p:
|
||||
pos=self.touch_table[i]
|
||||
myprint(f"点击 {i}:{pos}")
|
||||
self.togo(pos[0],pos[1])
|
||||
time.sleep(2)
|
||||
def password(self,p:str):
|
||||
index=0
|
||||
p_len=len(p)
|
||||
while index<p_len:
|
||||
pos=self.touch_table[p[index]]
|
||||
myprint(f"点击 index={index},{p[index]}")
|
||||
self.togo(pos[0],pos[1])
|
||||
self.touch()
|
||||
if p[index] not in "890":
|
||||
# 890 不需要避让
|
||||
self.avoid()
|
||||
if(find_text(identify_text(),"手机已锁定")):
|
||||
if(index==5):
|
||||
myprint(f"手机锁定中,已全部输入")
|
||||
return True
|
||||
myprint(f"密码输入状态异常 index={index},password={p}")
|
||||
return False
|
||||
num=find_input_count()
|
||||
myprint(f"已输入个数为 {num}")
|
||||
if(num==index+1):
|
||||
index+=1
|
||||
elif(num==0 and index==5):
|
||||
return True
|
||||
else:
|
||||
myprint(f"点击未识别,重试 index={index}")
|
||||
|
||||
|
||||
|
||||
def data_get()->dict:
|
||||
data_file="index.json"
|
||||
if os.path.exists(data_file):
|
||||
with open(data_file,mode='r') as f:
|
||||
return json.loads(f.read())
|
||||
return {"index":0}
|
||||
|
||||
|
||||
if __name__ == "__main1__":
|
||||
s=ser()
|
||||
s.open("com8")
|
||||
password=data_get().get("index",0)
|
||||
log_init("log.txt")
|
||||
myprint(f"起始密码:{password}")
|
||||
s.avoid()
|
||||
unknown_scr=False
|
||||
while(True):
|
||||
try:
|
||||
text=identify_text()
|
||||
if(find_text(text,"安装系统软件")):
|
||||
unknown_scr=False
|
||||
myprint("进入密码界面")
|
||||
s.enter()
|
||||
# s.avoid()
|
||||
elif(find_text(text,"请输入解锁密码")):
|
||||
unknown_scr=False
|
||||
pw=f"{password:06d}"
|
||||
myprint(f"开始尝试:{pw}")
|
||||
if(s.password(pw)):
|
||||
myprint(f"密码已输入")
|
||||
password+=1
|
||||
s.avoid()
|
||||
flush_wind_pos()
|
||||
get_scr(_ocr_pos[0],_ocr_pos[1],_ocr_pos[2],_ocr_pos[3],f"{pw}")
|
||||
elif(find_text(text,"手机已锁定")):
|
||||
time.sleep(1)
|
||||
else:
|
||||
if(unknown_scr==False):
|
||||
myprint("进入未知界面")
|
||||
myprint(f"界面信息为;{text}")
|
||||
unknown_scr=True
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
myprint(e)
|
||||
data_save({"index":password})
|
||||
print("终止运行")
|
||||
break
|
||||
except KeyboardInterrupt as k:
|
||||
myprint(k)
|
||||
data_save({"index":password})
|
||||
print("终止运行")
|
||||
break
|
||||
myprint(f"终止密码:{password}")
|
||||
s.reset()
|
||||
s.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# identify_text()
|
||||
# get_scr(_ocr_pos[0],_ocr_pos[1],_ocr_pos[2],_ocr_pos[3],f"test")
|
||||
# find_input_count()
|
||||
s=ser()
|
||||
s.open("com8")
|
||||
s.avoid()
|
||||
while True:
|
||||
try:
|
||||
s.password_test("1234567890")
|
||||
except Exception as e:
|
||||
myprint(e)
|
||||
break
|
||||
except KeyboardInterrupt as k:
|
||||
myprint(k)
|
||||
break
|
||||
s.reset()
|
||||
s.close()
|
Reference in New Issue
Block a user