diff --git a/README.md b/README.md index b912149..21d2460 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,15 @@ # yolov5 1. 代码中涉及到使用opencv对小地图和技能栏进行模板匹配和二值化等操作,注意,不同游戏分辨率和电脑显示器分辨率是不一致的,代码中给出的(0,0,1280,800)是本人游戏中的分辨率,而small_recgonize.py和skill_recgonize.py中的img[45:65, 1107:1270], img[733: 793, 538:750, 2]是根据不同显示器的分辨率决定的,使用时需要自己调整。 - 2. 本人训练的yolov5模型还有待提高,我的训练集只有294张图片,因此效果一般。 +详细教学请移步b站,有很详细的讲解:https://www.bilibili.com/video/BV18r4y1A7BF/ + + +1. 屏幕检测 +2. 怪物,材料的图像识别 +3. 控制打怪和捡材料 +4. 下一关 1. 识别地图中怪物 -2. 寻找位置,释放技能,清理残余,寻找下一个门,进图 -3. \ No newline at end of file +2. 寻找位置,释放技能,清理残余,寻找下一个门,进图 \ No newline at end of file diff --git a/main2.py b/main2.py index 0e37edc..dca15dc 100644 --- a/main2.py +++ b/main2.py @@ -6,14 +6,13 @@ import torch from directkeys import PressKey, ReleaseKey, key_down, key_up from getkeys import key_check -from utils.torch_utils import select_device, load_classifier, time_synchronized from utils.general import ( check_img_size, non_max_suppression, apply_classifier, scale_coords, - xyxy2xywh, xywh2xyxy, plot_one_box, strip_optimizer, set_logging) + xyxy2xywh, xywh2xyxy, strip_optimizer, set_logging) from models.experimental import attempt_load from direction_move import move -# from small_recgonize import current_door, next_door -# from skill_recgnize import skill_rec +from small_recgonize import current_door, next_door +from skill_recgnize import skill_rec import random @@ -51,11 +50,12 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal # 设置所有用到的参数 -weights = r'C:\Users\Administrator\PycharmProjects\yolov5\best.pt' # yolo5 模型存放的位置 +weights = r'C:\Users\ChrisWang\code\AI\yolov5-DNF-Chris\resource\best.pt' # yolo5 模型存放的位置 device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu") model = attempt_load(weights, map_location=device) # load FP32 model +dir(model) window_size = (0, 0, 1280, 800) # 截屏的位置 -img_size = 800 # 输入到yolo5中的模型尺寸 +img_size = 640 # 输入到yolo5中的模型尺寸 paused = False half = device.type != 'cpu' view_img = True # 是否观看目标检测结果 @@ -73,7 +73,6 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal action_cache = None # 动作标记 press_delay = 0.1 # 按压时间 release_delay = 0.1 # 释放时间 -# last_time = time.time() frame = 0 # 帧 door1_time_start = -20 next_door_time = -20 @@ -84,6 +83,7 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal # print(i + 1) # time.sleep(1) + # 捕捉画面+目标检测+玩游戏 while True: if not paused: @@ -91,20 +91,21 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal img0 = grab_screen(window_size) frame += 1 if frame % fs == 0: - # img0 = cv2.imread("datasets/guiqi/yolo5_datasets/imgs/1004_14.jpg") + img0 = cv2.imread("test/test.jpg") - img0 = cv2.cvtColor(img0, cv2.COLOR_BGRA2BGR) + img = cv2.cvtColor(img0, cv2.COLOR_BGRA2BGR) # Padded resize - img = letterbox(img0, new_shape=img_size)[0] + # img = letterbox(img0, new_shape=img_size)[0] + # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB img = np.ascontiguousarray(img) - img = torch.from_numpy(img).to(device).unsqueeze(0) - img = img.half() if half else img.float() # uint8 to fp16/32 - img /= 255.0 # 0 - 255 to 0.0 - 1.0 + img = torch.from_numpy(img).to(device).unsqueeze(dim=0) + # img = img.half() if half else img.float() # uint8 to fp16/32 + # img /= 255.0 # 0 - 255 to 0.0 - 1.0 pred = model(img, augment=False)[0] @@ -112,396 +113,396 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal det = non_max_suppression(pred, conf_thres, iou_thres, classes=classes, agnostic=agnostic_nms) gn = torch.tensor(img0.shape)[[1, 0, 1, 0]] det = det[0] - print(det) - break - if det is not None and len(det): - # Rescale boxes from img_size to im0 size - det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round() - - # Print results - for c in det[:, -1].unique(): - n = (det[:, -1] == c).sum() # detections per class - - img_object = [] - cls_object = [] - # Write results - hero_conf = 0 - hero_index = 0 - for idx, (*xyxy, conf, cls) in enumerate(reversed(det)): - # if save_txt: # Write to file - # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh - # with open(txt_path + '.txt', 'a') as f: - # f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format - - xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist() - cls = int(cls) - img_object.append(xywh) - cls_object.append(names[cls]) - - if names[cls] == "hero" and conf > hero_conf: - hero_conf = conf - hero_index = idx - - if view_img: # Add bbox to image - label = '%s %.2f' % (names[int(cls)], conf) - plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=2) - - # 游戏 - thx = 30 # 捡东西时,x方向的阈值 - thy = 30 # 捡东西时,y方向的阈值 - attx = 150 # 攻击时,x方向的阈值 - atty = 50 # 攻击时,y方向的阈值 - if current_door(img0) == 1 and time.time() - door1_time_start > 10: - door1_time_start = time.time() - # move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - # release_delay=release_delay) - # ReleaseKey(direct_dic["RIGHT"]) - # directkeys.key_press("SPACE") - directkeys.key_press("CTRL") - time.sleep(1) - directkeys.key_press("ALT") - time.sleep(0.5) - action_cache = None - # 扫描英雄 - if "hero" in cls_object: - # hero_xywh = img_object[cls_object.index("hero")] - hero_xywh = img_object[hero_index] - cv2.circle(img0, (int(hero_xywh[0]), int(hero_xywh[1])), 1, (0, 0, 255), 10) - # print(hero_index) - # print(cls_object.index("hero")) - else: - continue - # 打怪 - if "monster" in cls_object or "BOSS" in cls_object: - min_distance = float("inf") - for idx, (c, box) in enumerate(zip(cls_object, img_object)): - if c == 'monster' or c == "BOSS": - dis = ((hero_xywh[0] - box[0]) ** 2 + (hero_xywh[1] - box[1]) ** 2) ** 0.5 - if dis < min_distance: - monster_box = box - monster_index = idx - min_distance = dis - if abs(hero_xywh[0] - monster_box[0]) < attx and abs(hero_xywh[1] - monster_box[1]) < atty: - if "BOSS" in cls_object: - directkeys.key_press("R") - directkeys.key_press("Q") - # time.sleep(0.5) - skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] - while True: - if skill_rec(skill_name, img0): - directkeys.key_press(skill_name) - directkeys.key_press(skill_name) - directkeys.key_press(skill_name) - break - else: - skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] - - else: - skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] - while True: - if skill_rec(skill_name, img0): - directkeys.key_press(skill_name) - directkeys.key_press(skill_name) - directkeys.key_press(skill_name) - break - else: - skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] - print("释放技能攻击") - if not action_cache: - pass - elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: - ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) - ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) - action_cache = None - elif action_cache: - ReleaseKey(direct_dic[action_cache]) - action_cache = None - # break - elif monster_box[1] - hero_xywh[1] < 0 and monster_box[0] - hero_xywh[0] > 0: - if abs(monster_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - monster_box[1] < monster_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - monster_box[1] >= monster_box[0] - hero_xywh[0]: - action_cache = move(direct="UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] < 0 and monster_box[0] - hero_xywh[0] < 0: - if abs(monster_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - monster_box[1] < hero_xywh[0] - monster_box[0]: - action_cache = move(direct="LEFT_UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - monster_box[1] >= hero_xywh[0] - monster_box[0]: - action_cache = move(direct="UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] > 0 and monster_box[0] - hero_xywh[0] < 0: - if abs(monster_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] < hero_xywh[0] - monster_box[0]: - action_cache = move(direct="LEFT_DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] >= hero_xywh[0] - monster_box[0]: - action_cache = move(direct="DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] > 0 and monster_box[0] - hero_xywh[0] > 0: - if abs(monster_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] < monster_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif monster_box[1] - hero_xywh[1] >= monster_box[0] - hero_xywh[0]: - action_cache = move(direct="DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - - # 移动到下一个地图 - if "door" in cls_object and "monster" not in cls_object and "BOSS" not in cls_object and "material" not in cls_object and "money" not in cls_object: - for idx, (c, box) in enumerate(zip(cls_object, img_object)): - if c == 'door': - door_box = box - door_index = idx - if door_box[0] < img0.shape[0] // 2: - action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] < 0 and door_box[0] - hero_xywh[0] > 0: - if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: - action_cache = None - print("进入下一地图") - # break - elif abs(door_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - door_box[1] < door_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_UP", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - door_box[1] >= door_box[0] - hero_xywh[0]: - action_cache = move(direct="UP", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] < 0 and door_box[0] - hero_xywh[0] < 0: - if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: - action_cache = None - print("进入下一地图") - # break - elif abs(door_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - door_box[1] < hero_xywh[0] - door_box[0]: - action_cache = move(direct="LEFT_UP", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - door_box[1] >= hero_xywh[0] - door_box[0]: - action_cache = move(direct="UP", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] > 0 and door_box[0] - hero_xywh[0] < 0: - if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: - action_cache = None - print("进入下一地图") - # break - elif abs(door_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] < hero_xywh[0] - door_box[0]: - action_cache = move(direct="LEFT_DOWN", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] >= hero_xywh[0] - door_box[0]: - action_cache = move(direct="DOWN", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] > 0 and door_box[0] - hero_xywh[0] > 0: - if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: - action_cache = None - print("进入下一地图") - # break - elif abs(door_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] < door_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_DOWN", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - elif door_box[1] - hero_xywh[1] >= door_box[0] - hero_xywh[0]: - action_cache = move(direct="DOWN", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - if "money" not in cls_object and "material" not in cls_object and "monster" not in cls_object \ - and "BOSS" not in cls_object and "door" not in cls_object and 'box' not in cls_object \ - and 'options' not in cls_object: - # if next_door(img0) == 0 and abs(time.time()) - next_door_time > 10: - # next_door_time = time.time() - # action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, - # release_delay=release_delay) - # # time.sleep(3) - # else: - # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - # release_delay=release_delay) - - action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, - release_delay=release_delay) - # break - - # 捡材料 - if "monster" not in cls_object and "hero" in cls_object and ( - "material" in cls_object or "money" in cls_object): - min_distance = float("inf") - hero_xywh[1] = hero_xywh[1] + (hero_xywh[3] // 2) * 0.7 - thx = thx / 2 - thy = thy / 2 - for idx, (c, box) in enumerate(zip(cls_object, img_object)): - if c == 'material' or c == "money": - dis = ((hero_xywh[0] - box[0]) ** 2 + (hero_xywh[1] - box[1]) ** 2) ** 0.5 - if dis < min_distance: - material_box = box - material_index = idx - min_distance = dis - if abs(material_box[1] - hero_xywh[1]) < thy and abs(material_box[0] - hero_xywh[0]) < thx: - if not action_cache: - pass - elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: - ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) - ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) - action_cache = None - else: - ReleaseKey(direct_dic[action_cache]) - action_cache = None - time.sleep(1) - directkeys.key_press("X") - print("捡东西") - # break - - elif material_box[1] - hero_xywh[1] < 0 and material_box[0] - hero_xywh[0] > 0: - - if abs(material_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - material_box[1] < material_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - material_box[1] >= material_box[0] - hero_xywh[0]: - action_cache = move(direct="UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] < 0 and material_box[0] - hero_xywh[0] < 0: - if abs(material_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - material_box[1] < hero_xywh[0] - material_box[0]: - action_cache = move(direct="LEFT_UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif hero_xywh[1] - material_box[1] >= hero_xywh[0] - material_box[0]: - action_cache = move(direct="UP", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] > 0 and material_box[0] - hero_xywh[0] < 0: - if abs(material_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="LEFT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] < hero_xywh[0] - material_box[0]: - action_cache = move(direct="LEFT_DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] >= hero_xywh[0] - material_box[0]: - action_cache = move(direct="DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] > 0 and material_box[0] - hero_xywh[0] > 0: - if abs(material_box[1] - hero_xywh[1]) < thy: - action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] < material_box[0] - hero_xywh[0]: - action_cache = move(direct="RIGHT_DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - elif material_box[1] - hero_xywh[1] >= material_box[0] - hero_xywh[0]: - action_cache = move(direct="DOWN", material=True, action_cache=action_cache, - press_delay=press_delay, - release_delay=release_delay) - # break - # 开箱子 - if "box" in cls_object: - box_num = 0 - for b in cls_object: - if b == "box": - box_num += 1 - if box_num >= 4: - directkeys.key_press("ESC") - print("打开箱子ESC") - # break62 - - # 重新开始 - time_option = -20 - if "options" in cls_object: - if not action_cache: - pass - elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: - ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) - ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) - action_cache = None - else: - ReleaseKey(direct_dic[action_cache]) - action_cache = None - if time.time() - time_option > 10: - directkeys.key_press("NUM0") - print("移动物品到脚下") - directkeys.key_press("X") - time_option = time.time() - directkeys.key_press("F2") - print("重新开始F2") - # break + break + # if det is not None and len(det): + # # Rescale boxes from img_size to im0 size + # det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round() + # + # # Print results + # for c in det[:, -1].unique(): + # n = (det[:, -1] == c).sum() # detections per class + # + # img_object = [] + # cls_object = [] + # # Write results + # hero_conf = 0 + # hero_index = 0 + # for idx, (*xyxy, conf, cls) in enumerate(reversed(det)): + # # if save_txt: # Write to file + # # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh + # # with open(txt_path + '.txt', 'a') as f: + # # f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format + # + # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist() + # cls = int(cls) + # img_object.append(xywh) + # cls_object.append(names[cls]) + # + # if names[cls] == "hero" and conf > hero_conf: + # hero_conf = conf + # hero_index = idx + # + # if view_img: # Add bbox to image + # label = '%s %.2f' % (names[int(cls)], conf) + # plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=2) + # + # # 游戏 + # thx = 30 # 捡东西时,x方向的阈值 + # thy = 30 # 捡东西时,y方向的阈值 + # attx = 150 # 攻击时,x方向的阈值 + # atty = 50 # 攻击时,y方向的阈值 + # + # if current_door(img0) == 1 and time.time() - door1_time_start > 10: + # door1_time_start = time.time() + # # move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # # release_delay=release_delay) + # # ReleaseKey(direct_dic["RIGHT"]) + # # directkeys.key_press("SPACE") + # directkeys.key_press("CTRL") + # time.sleep(1) + # directkeys.key_press("ALT") + # time.sleep(0.5) + # action_cache = None + # # 扫描英雄 + # if "hero" in cls_object: + # # hero_xywh = img_object[cls_object.index("hero")] + # hero_xywh = img_object[hero_index] + # cv2.circle(img0, (int(hero_xywh[0]), int(hero_xywh[1])), 1, (0, 0, 255), 10) + # # print(hero_index) + # # print(cls_object.index("hero")) + # else: + # continue + # # 打怪 + # if "monster" in cls_object or "BOSS" in cls_object: + # min_distance = float("inf") + # for idx, (c, box) in enumerate(zip(cls_object, img_object)): + # if c == 'monster' or c == "BOSS": + # dis = ((hero_xywh[0] - box[0]) ** 2 + (hero_xywh[1] - box[1]) ** 2) ** 0.5 + # if dis < min_distance: + # monster_box = box + # monster_index = idx + # min_distance = dis + # if abs(hero_xywh[0] - monster_box[0]) < attx and abs(hero_xywh[1] - monster_box[1]) < atty: + # if "BOSS" in cls_object: + # directkeys.key_press("R") + # directkeys.key_press("Q") + # # time.sleep(0.5) + # skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] + # while True: + # if skill_rec(skill_name, img0): + # directkeys.key_press(skill_name) + # directkeys.key_press(skill_name) + # directkeys.key_press(skill_name) + # break + # else: + # skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] + # + # else: + # skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] + # while True: + # if skill_rec(skill_name, img0): + # directkeys.key_press(skill_name) + # directkeys.key_press(skill_name) + # directkeys.key_press(skill_name) + # break + # else: + # skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])] + # print("释放技能攻击") + # if not action_cache: + # pass + # elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: + # ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) + # ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) + # action_cache = None + # elif action_cache: + # ReleaseKey(direct_dic[action_cache]) + # action_cache = None + # # break + # elif monster_box[1] - hero_xywh[1] < 0 and monster_box[0] - hero_xywh[0] > 0: + # if abs(monster_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - monster_box[1] < monster_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - monster_box[1] >= monster_box[0] - hero_xywh[0]: + # action_cache = move(direct="UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] < 0 and monster_box[0] - hero_xywh[0] < 0: + # if abs(monster_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - monster_box[1] < hero_xywh[0] - monster_box[0]: + # action_cache = move(direct="LEFT_UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - monster_box[1] >= hero_xywh[0] - monster_box[0]: + # action_cache = move(direct="UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] > 0 and monster_box[0] - hero_xywh[0] < 0: + # if abs(monster_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] < hero_xywh[0] - monster_box[0]: + # action_cache = move(direct="LEFT_DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] >= hero_xywh[0] - monster_box[0]: + # action_cache = move(direct="DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] > 0 and monster_box[0] - hero_xywh[0] > 0: + # if abs(monster_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] < monster_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif monster_box[1] - hero_xywh[1] >= monster_box[0] - hero_xywh[0]: + # action_cache = move(direct="DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # + # # 移动到下一个地图 + # if "door" in cls_object and "monster" not in cls_object and "BOSS" not in cls_object and "material" not in cls_object and "money" not in cls_object: + # for idx, (c, box) in enumerate(zip(cls_object, img_object)): + # if c == 'door': + # door_box = box + # door_index = idx + # if door_box[0] < img0.shape[0] // 2: + # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] < 0 and door_box[0] - hero_xywh[0] > 0: + # if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: + # action_cache = None + # print("进入下一地图") + # # break + # elif abs(door_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - door_box[1] < door_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_UP", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - door_box[1] >= door_box[0] - hero_xywh[0]: + # action_cache = move(direct="UP", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] < 0 and door_box[0] - hero_xywh[0] < 0: + # if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: + # action_cache = None + # print("进入下一地图") + # # break + # elif abs(door_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - door_box[1] < hero_xywh[0] - door_box[0]: + # action_cache = move(direct="LEFT_UP", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - door_box[1] >= hero_xywh[0] - door_box[0]: + # action_cache = move(direct="UP", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] > 0 and door_box[0] - hero_xywh[0] < 0: + # if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: + # action_cache = None + # print("进入下一地图") + # # break + # elif abs(door_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] < hero_xywh[0] - door_box[0]: + # action_cache = move(direct="LEFT_DOWN", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] >= hero_xywh[0] - door_box[0]: + # action_cache = move(direct="DOWN", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] > 0 and door_box[0] - hero_xywh[0] > 0: + # if abs(door_box[1] - hero_xywh[1]) < thy and abs(door_box[0] - hero_xywh[0]) < thx: + # action_cache = None + # print("进入下一地图") + # # break + # elif abs(door_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] < door_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_DOWN", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif door_box[1] - hero_xywh[1] >= door_box[0] - hero_xywh[0]: + # action_cache = move(direct="DOWN", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # if "money" not in cls_object and "material" not in cls_object and "monster" not in cls_object \ + # and "BOSS" not in cls_object and "door" not in cls_object and 'box' not in cls_object \ + # and 'options' not in cls_object: + # # if next_door(img0) == 0 and abs(time.time()) - next_door_time > 10: + # # next_door_time = time.time() + # # action_cache = move(direct="LEFT", action_cache=action_cache, press_delay=press_delay, + # # release_delay=release_delay) + # # # time.sleep(3) + # # else: + # # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # # release_delay=release_delay) + # + # action_cache = move(direct="RIGHT", action_cache=action_cache, press_delay=press_delay, + # release_delay=release_delay) + # # break + # + # # 捡材料 + # if "monster" not in cls_object and "hero" in cls_object and ( + # "material" in cls_object or "money" in cls_object): + # min_distance = float("inf") + # hero_xywh[1] = hero_xywh[1] + (hero_xywh[3] // 2) * 0.7 + # thx = thx / 2 + # thy = thy / 2 + # for idx, (c, box) in enumerate(zip(cls_object, img_object)): + # if c == 'material' or c == "money": + # dis = ((hero_xywh[0] - box[0]) ** 2 + (hero_xywh[1] - box[1]) ** 2) ** 0.5 + # if dis < min_distance: + # material_box = box + # material_index = idx + # min_distance = dis + # if abs(material_box[1] - hero_xywh[1]) < thy and abs(material_box[0] - hero_xywh[0]) < thx: + # if not action_cache: + # pass + # elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: + # ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) + # ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) + # action_cache = None + # else: + # ReleaseKey(direct_dic[action_cache]) + # action_cache = None + # time.sleep(1) + # directkeys.key_press("X") + # print("捡东西") + # # break + # + # elif material_box[1] - hero_xywh[1] < 0 and material_box[0] - hero_xywh[0] > 0: + # + # if abs(material_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - material_box[1] < material_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - material_box[1] >= material_box[0] - hero_xywh[0]: + # action_cache = move(direct="UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] < 0 and material_box[0] - hero_xywh[0] < 0: + # if abs(material_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - material_box[1] < hero_xywh[0] - material_box[0]: + # action_cache = move(direct="LEFT_UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif hero_xywh[1] - material_box[1] >= hero_xywh[0] - material_box[0]: + # action_cache = move(direct="UP", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] > 0 and material_box[0] - hero_xywh[0] < 0: + # if abs(material_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="LEFT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] < hero_xywh[0] - material_box[0]: + # action_cache = move(direct="LEFT_DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] >= hero_xywh[0] - material_box[0]: + # action_cache = move(direct="DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] > 0 and material_box[0] - hero_xywh[0] > 0: + # if abs(material_box[1] - hero_xywh[1]) < thy: + # action_cache = move(direct="RIGHT", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] < material_box[0] - hero_xywh[0]: + # action_cache = move(direct="RIGHT_DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # elif material_box[1] - hero_xywh[1] >= material_box[0] - hero_xywh[0]: + # action_cache = move(direct="DOWN", material=True, action_cache=action_cache, + # press_delay=press_delay, + # release_delay=release_delay) + # # break + # # 开箱子 + # if "box" in cls_object: + # box_num = 0 + # for b in cls_object: + # if b == "box": + # box_num += 1 + # if box_num >= 4: + # directkeys.key_press("ESC") + # print("打开箱子ESC") + # # break62 + # + # # 重新开始 + # time_option = -20 + # if "options" in cls_object: + # if not action_cache: + # pass + # elif action_cache not in ["LEFT", "RIGHT", "UP", "DOWN"]: + # ReleaseKey(direct_dic[action_cache.strip().split("_")[0]]) + # ReleaseKey(direct_dic[action_cache.strip().split("_")[1]]) + # action_cache = None + # else: + # ReleaseKey(direct_dic[action_cache]) + # action_cache = None + # if time.time() - time_option > 10: + # directkeys.key_press("NUM0") + # print("移动物品到脚下") + # directkeys.key_press("X") + # time_option = time.time() + # directkeys.key_press("F2") + # print("重新开始F2") + # # break t_end = time.time() print("一帧游戏操作所用时间:", (t_end - t_start) / fs) diff --git a/models/common.py b/models/common.py index 31449b1..f03e265 100644 --- a/models/common.py +++ b/models/common.py @@ -41,6 +41,61 @@ def forward(self, x): return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) +import warnings + +class SPPF(nn.Module): + # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher + def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) + super().__init__() + c_ = c1 // 2 # hidden channels + self.cv1 = Conv(c1, c_, 1, 1) + self.cv2 = Conv(c_ * 4, c2, 1, 1) + self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) + + def forward(self, x): + x = self.cv1(x) + with warnings.catch_warnings(): + warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning + y1 = self.m(x) + y2 = self.m(y1) + return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1)) + + + + +class CrossConv(nn.Module): + # Cross Convolution Downsample + def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): + # ch_in, ch_out, kernel, stride, groups, expansion, shortcut + super(CrossConv, self).__init__() + c_ = int(c2 * e) # hidden channels + self.cv1 = Conv(c1, c_, (1, k), (1, s)) + self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) + self.add = shortcut and c1 == c2 + + def forward(self, x): + return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) + +class C3(nn.Module): + # Cross Convolution CSP + def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion + super(C3, self).__init__() + c_ = int(c2 * e) # hidden channels + self.cv1 = Conv(c1, c_, 1, 1) + self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) + self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) + self.cv4 = Conv(2 * c_, c2, 1, 1) + self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) + self.act = nn.LeakyReLU(0.1, inplace=True) + self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)]) + + def forward(self, x): + y1 = self.cv3(self.m(self.cv1(x))) + y2 = self.cv2(x) + return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) + + + def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: diff --git a/resource/best.pt b/resource/best.pt new file mode 100644 index 0000000..ec8b7b3 Binary files /dev/null and b/resource/best.pt differ diff --git a/small_recgonize.py b/small_recgonize.py index 93ff65b..1590da5 100644 --- a/small_recgonize.py +++ b/small_recgonize.py @@ -2,7 +2,7 @@ import numpy as np -img_path = "datasets/guiqi/test/61_93.jpg" +img_path = "test/DNF.png" img = cv.imread(img_path) def img_show(img): diff --git a/test/DNF.png b/test/DNF.png new file mode 100644 index 0000000..72c5499 Binary files /dev/null and b/test/DNF.png differ diff --git a/test/cv2_test.py b/test/cv2_test.py new file mode 100644 index 0000000..af1fa8d --- /dev/null +++ b/test/cv2_test.py @@ -0,0 +1,9 @@ +import cv2 +from ..grabscreen import grab_screen + +img = grab_screen((0, 0, 1280, 800)) +img0 = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) +dnf = cv2.imread(img0) +cv2.imshow('rgb', dnf) +# BGR = cv2.cvtColor(dnf,cv2.COLOR_RGB2BGR) +# cv2.imshow('BGR',BGR) \ No newline at end of file diff --git a/test/test.jpg b/test/test.jpg new file mode 100644 index 0000000..f7fa6ed Binary files /dev/null and b/test/test.jpg differ diff --git a/test/yoloModel.py b/test/yoloModel.py new file mode 100644 index 0000000..e69de29 diff --git a/wx-read/globalSearch.py b/wx-read/globalSearch.py index bd57fb0..7a6fcb3 100644 --- a/wx-read/globalSearch.py +++ b/wx-read/globalSearch.py @@ -21,7 +21,7 @@ def sortWay(book): return book['bookInfo']['newRating'] -def getWXBooks(queryPath): +def getWXBooks(queryPath,searchWord): # booksTitleSet = client.hget(searchKeyWord, 'booksTitleSet') or set() # maxIdx = int(client.hget(searchKeyWord, 'maxIdx')) if client.hget(searchKeyWord, 'maxIdx') is not None else 0 maxIdx = 0 @@ -33,7 +33,7 @@ def getWXBooks(queryPath): rTime = random.randint(1, 3) # 随机延迟,从1到3内取一个整数值 time.sleep(rTime) # 把随机取出的整数值传到等待函数中 res = requests.get( - queryPath, params={'maxIdx': maxIdx, 'keyword': searchKeyWord, 'fragmentSize': 120, 'count': 20}) + queryPath, params={'maxIdx': maxIdx, 'keyword': searchWord, 'fragmentSize': 120, 'count': 20}) books = res.json()['books'] if not totalCount: totalCount = res.json()['totalCount'] @@ -60,5 +60,5 @@ def getWXBooks(queryPath): return bookList -books = getWXBooks('https://weread.qq.com/web/search/global') +books = getWXBooks('https://weread.qq.com/web/search/global',searchKeyWord) mongodbWrite(searchKeyWord, books)