from __future__ import annotations import os from typing import Any from PIL import Image def check_have_robot_analyse(analyser) -> int | bool: """检查是否存在人机校验。 返回: - 1: 滑动图片验证 - 2: 点击图片验证(形状相同) - 3: 人脸识别验证 - False: 未检测到人机验证 """ analyser.cut_pic(130, 790, 680, 870, "", "直播验证") result = analyser.operation.analyse_pic_word("直播验证", 1) if "验证" in result: print("存在滑动图片人机校验,需要等待完成验证.") return 1 elif "形状相同" in result: print("存在点击图片人机校验,需要等待完成验证.") return 2 analyser.cut_pic(340, 1415, 700, 1518, "", "直播验证") result = analyser.operation.analyse_pic_word("直播验证") if "开始检测" in result: print("存在人脸识别人机校验,需要等待完成验证.") return 3 return False def deal_swipe_robot_analyse(analyser, distance: int = 400) -> None: """处理滑动图片的人机验证。""" if distance: targetx = (222 + distance) * analyser.resolution_ratio_x // 1080 else: targetx = 622 * analyser.resolution_ratio_x // 1080 analyser.swipe(222, 1444, targetx, 1444, 300) print("滑轨滑动{}距离解锁人机验证".format(distance)) analyser.operation.delay(1) def deal_robot_analyse(analyser) -> None: """处理人机校验,包含各种情况。""" swipe_times = 0 while swipe_times < 10: robot_result = check_have_robot_analyse(analyser) if robot_result == 1: distance = check_robot_pic_distance(analyser) deal_swipe_robot_analyse(analyser, distance) analyser.operation.delay(10) analyser._shot_and_diagnose("人机校验", "校验后复查", analyser.device_id) elif robot_result == 2: print("无法处理图片验证的人机,点击关闭退出验证,等待30分钟") analyser.click(910, 800) # 点击关闭 analyser.operation.delay(1800) break elif robot_result == 3: print("无法处理人脸识别验证的人机,点击返回退出验证,等待30分钟") analyser.operation.click_back(analyser.device_id) analyser.operation.delay(2) analyser.operation.click_back(analyser.device_id) analyser.operation.delay(1800) break else: break swipe_times += 1 if swipe_times >= 10: print("无法处理图片验证的人机,点击关闭退出验证,等待30分钟") analyser.click(910, 800) # 点击关闭 analyser.operation.delay(1800) def deal_robot_pic_change_color(analyser) -> None: """处理人机验证的图片,转为黑白图。""" analyser.cut_pic(143, 884, 936, 1380, "save", "机器人验证") path = os.path.join(os.path.dirname(__file__), "pic", "save") pic = os.path.join(path, "机器人验证.png") img = Image.open(pic) img = img.convert("RGB") width, height = img.size for x in range(5, width - 40): for y in range(20, height - 30): current_color = img.getpixel((x, y)) if current_color[0] > 240 and current_color[1] > 240 and current_color[2] > 240: img.putpixel((x, y), (255, 255, 255)) # 白色 elif current_color[0] < 35 and current_color[1] < 20 and current_color[2] < 20: img.putpixel((x, y), (0, 0, 0)) # 黑色 else: img.putpixel((x, y), (128, 128, 128)) # 灰色 os.makedirs(path, exist_ok=True) save_pic = os.path.join(path, "机器人验证_新.png") img.save(save_pic) def check_robot_pic_distance(analyser) -> int: """根据人机图片计算需要滑动的距离。""" analyser.cut_pic(143, 884, 936, 1380, "", "机器人验证") # 使用新的路径查找方法(支持新旧位置) pic_path = analyser.operation._find_pic_path("机器人验证", for_reading=True) if not pic_path or not os.path.exists(pic_path): raise FileNotFoundError(f"机器人验证图片不存在: {pic_path}") img = Image.open(pic_path) img = img.convert("RGB") width, height = img.size printed_first_result = False # 用于记录第一个结果是否已经输出过 printed_second_result = False # 用于记录第二个结果是否已经输出过 x = 0 y = 0 for y in range(20, height - 30): for x in range(5, width - 40): current_color = img.getpixel((x, y)) if current_color[0] > 240 and current_color[1] > 240 and current_color[2] > 240: if not printed_first_result: # 确保只输出一次第一个结果 print(x, y) printed_first_result = True break if printed_first_result: # 如果已经输出过第一个结果,则退出外层循环 break for x1 in range(x, width - 40): current_color = img.getpixel((x1, y)) if ( current_color[0] < 50 and current_color[1] < 55 and current_color[2] < 85 and current_color[0] + current_color[1] + current_color[2] < 150 ): if not printed_second_result: # 确保只输出一次第二个结果 print(x1, y) printed_second_result = True print("需要滑动的距离为{}".format(x1 - x)) return x1 - x # 如果没有检测到,返回一个兜底值 0 return 0