def auto_click(): util.log_h1(f'前置准备') if sc.dir_check(): auto.open_driver() dm.model_load() while (True): util.log_h1_start(f'开始') start_time = time.time() if sc.task(): min_index = dm.model_predict(c.crop_4_img_paths) sc.save_data_img(min_index) target_x, target_y = sc.find_xy_indesktop( c.crop_4_img_paths[min_index]) if target_x == 0 and target_y == 0: util.log_title('匹配失败') else: auto.move_to(target_x, target_y) if sc.shot(): now_x, now_y = sc.find_mouse_in_desktop() move_x = target_x - now_x + c.mouse_move_shape[0] move_y = target_y - now_y + c.mouse_move_shape[1] auto.move_rel_click(move_x, move_y) end_time = time.time() cost_time = end_time - start_time util.log_h1_end(f'结束 耗时 %.3f' % cost_time) time.sleep(3)
def crop_4(): util.log_title('弹窗人物切分') w = 90 h = 120 for i in range(len(c.crop_4_img_names)): shape = (w * i, 0, w * (i + 1), h) crop(c.popup_sub_img_path, c.crop_4_img_paths[i], shape)
def image_check(img_path, size): util.log_title('截图检查') with Image.open(img_path) as img: if img.size[1] == size[1]: print(f'\t\tsize={size}\t\tok') return True print('Imgae Size Error') return False
def is_fight(): util.log_title('状态判断') rate = compare_image(c.fighting_flag_img_path, c.fighting_img_path) if rate > 0.95: print('战斗 状态') return True else: print('非战斗 状态') return False
def dir_check(): util.log_title('文件夹检查') dir_List = [ c.img_dir_path, c.flag_dir_path, c.sub_dir_path, c.data_dir_path, c.train_dir, c.front_img_dir, c.others_img_dir, c.new_front_img_dir, c.new_others_img_dir ] for path in dir_List: dir_create(path) print(f'\t{path}\t\tok') return True
def find_xy(template_path): util.log_title('坐标查找') shape, score = template_match(template_path, c.img_sc_path) print(f'最高得分区域 {shape} 得分为 {score}') if score >= 3: x = (shape[2] + shape[0]) // 2 y = (shape[3] + shape[1]) // 2 print(f'中心点坐标为 {(x,y)}') return x, y else: print(f'所有区域得分均小于3,匹配失败') return 0, 0
def move_learn(): sc.dir_check() util.log_title('图片朝向确认') confirm = input( f'请确认路径 {os.path.abspath(c.new_front_img_dir)} 下图片朝向均为 > 前 < : (确认后输入 Y , 输入其他退出) ' ) if confirm == 'Y' or confirm == 'y': confirm = input( f'请确认路径 {os.path.abspath(c.new_others_img_dir)} 下图片朝向均为 > 左 右 后 < : (确认后输入 Y , 输入其他退出)' ) if confirm == 'Y' or confirm == 'y': util.log_h1_start('开始') sc.move_new_to_train() util.log_h1_end('结束')
def popup_sub_crop(): util.log_title('弹窗判断') shape_dict = {} for i in range(len(c.popup_flag_img_paths)): shape, score = template_match(c.popup_flag_img_paths[i], c.img_sc_path) shape_dict[shape] = (score, i) print(shape_dict) max_shape = max(shape_dict, key=shape_dict.get) score, i = shape_dict[max_shape] print(f'最大区域 {max_shape} 最终得分为 {score}') if score >= 3: sub_shape = (max_shape[0] + c.popup_move_shapes[i][0], max_shape[1] + c.popup_move_shapes[i][1], max_shape[2] + c.popup_move_shapes[i][2], max_shape[3] + c.popup_move_shapes[i][3]) print(f'弹框区域 {sub_shape}') return crop(c.img_sc_path, c.popup_sub_img_path, sub_shape) print(f'没有弹框') return False
def shot(): util.log_title('截图') win32gui.EnumWindows(get_all_hwnd, 0) mhxy_title = '' for h, t in hwnd_title.items(): if t.startswith('梦幻西游 ONLINE'): mhxy_title = t print(mhxy_title) hwnd = win32gui.FindWindow(None, mhxy_title) app = QApplication(sys.argv) desktop_id = app.desktop().winId() screen = QApplication.primaryScreen() img_desk = screen.grabWindow(desktop_id).toImage() img_sc = screen.grabWindow(hwnd).toImage() img_desk.save(c.img_desktop_path) img_sc.save(c.img_sc_path) print( f'img_desktop save to -> {os.path.abspath(c.img_desktop_path)}' ) print(f'img_mhxy save to -> {os.path.abspath(c.img_sc_path)}') if mhxy_title == '': print('mhxy not start') return False return True
def fight_crop(): util.log_title('战斗标识截图') return crop(c.img_sc_path, c.fighting_img_path, c.fight_shape)