Files
password_try/touch2.py

265 lines
6.6 KiB
Python
Raw Normal View History

2025-03-01 23:49:41 +08:00
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
2025-03-03 12:28:44 +08:00
w=pos[2]-pos[0]
h=pos[3]-pos[1]
_ocr_pos=[pos[0]+0,pos[1]+int(180/877*h),w,int(296/877*h)]
_ocr_boll=[pos[0]+int(99/376*w),pos[1]+int(440/877*h),int(172/376*w),int(27/877*h)]
_mouse_touch=[pos[0]+0,pos[1]+int(603/877*h),int(145/376*w),int(70/877*h)]
2025-03-01 23:49:41 +08:00
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):
2025-03-03 12:28:44 +08:00
self.write(f"Z6\r")
2025-03-01 23:49:41 +08:00
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):
2025-03-05 22:31:40 +08:00
self.togo(70+20,30)
2025-03-01 23:49:41 +08:00
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
2025-03-05 22:31:40 +08:00
touch_times=0
2025-03-01 23:49:41 +08:00
p_len=len(p)
2025-03-03 12:28:44 +08:00
try_times=0
2025-03-01 23:49:41 +08:00
while index<p_len:
pos=self.touch_table[p[index]]
myprint(f"点击 index={index},{p[index]}")
2025-03-05 22:31:40 +08:00
self.togo(pos[0]+int(pow(-1,touch_times)*3),pos[1])
2025-03-01 23:49:41 +08:00
self.touch()
2025-03-02 10:27:12 +08:00
self.avoid()
2025-03-03 12:28:44 +08:00
time.sleep(0.1) # 这里延时防止界面更新延迟导致的识别异常
2025-03-05 22:31:40 +08:00
touch_times+=1
2025-03-03 12:28:44 +08:00
# if(find_text(identify_text(),"手机已锁定")):
# if(index==5):
# myprint(f"手机锁定中,已全部输入")
# return True
# myprint(f"密码输入状态异常 index={index},password={p}")
# return False
2025-03-01 23:49:41 +08:00
num=find_input_count()
myprint(f"已输入个数为 {num}")
if(num==index+1):
2025-03-03 12:28:44 +08:00
try_times=0
2025-03-01 23:49:41 +08:00
index+=1
elif(num==0 and index==5):
return True
2025-03-03 12:28:44 +08:00
elif(num==index):
try_times+=1
2025-03-01 23:49:41 +08:00
myprint(f"点击未识别,重试 index={index}")
2025-03-03 12:28:44 +08:00
if(try_times>20):
myprint(f"尝试次数过多 times={try_times},退出")
s.back()
return False
else:
myprint(f"密码输入状态异常 index={index},num={num},password={p}")
s.back()
return False
2025-03-01 23:49:41 +08:00
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}
2025-03-05 22:31:40 +08:00
def test():
identify_text()
get_scr(_ocr_pos[0],_ocr_pos[1],_ocr_pos[2],_ocr_pos[3],f"test")
find_input_count()
2025-03-02 10:27:12 +08:00
if __name__ == "__main__":
2025-03-05 22:31:40 +08:00
if len(sys.argv)>=2 and sys.argv[1]=='test':
test()
sys.exit(0)
2025-03-01 23:49:41 +08:00
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()
2025-03-03 12:28:44 +08:00
if(find_text(text,"安装系统")):
2025-03-01 23:49:41 +08:00
unknown_scr=False
myprint("进入密码界面")
s.enter()
2025-03-03 12:28:44 +08:00
time.sleep(0.2)
2025-03-01 23:49:41 +08:00
# 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()
2025-03-02 10:27:12 +08:00
if __name__ == "__main1__":
2025-03-03 12:28:44 +08:00
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()