Skip to content

Commit

Permalink
feat: yolov5 未央自动爬楼 - 1 yolo不同版本训练的pt不能兼容使用
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverComet7 committed Aug 12, 2022
1 parent 0c56330 commit 91d89fb
Show file tree
Hide file tree
Showing 11 changed files with 544 additions and 401 deletions.
File renamed without changes.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
详细教学请移步b站,有很详细的讲解:https://www.bilibili.com/video/BV18r4y1A7BF/


1. 屏幕检测
2. 怪物,材料的图像识别
3. 控制打怪和捡材料
4. 下一关
视频收集数据集
标注
训练
视频检测训练效果

游戏主流程:
1. 屏幕抓取,图像检测
2. 怪物识别,和怪物距离n像素拍地板清怪,漏怪继续拍地板
3. 捡发光的材料
4. 小地图进入下一关
5. boss房间,下一局

1. 识别地图中怪物
2. 寻找位置,释放技能,清理残余,寻找下一个门,进图

测试流程:
1.纯小号检测 1级地图
2.未央爬楼视频检测,测试延迟同步操作是否能基础通过
3.跨4子账号未央检测

实战
791 changes: 396 additions & 395 deletions main2.py

Large diffs are not rendered by default.

Binary file added opencv/bgra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions opencv/bgra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cv2

img = cv2.imread("lena.jpg")
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
b, g, r, a = cv2.split(bgra)
a[:, :] = 125
bgra125 = cv2.merge([b, g, r, a])
a[:, :] = 0
bgra0 = cv2.merge([b, g, r, a])
cv2.imshow("img", img)
cv2.imshow("bgra", bgra)
cv2.imshow("bgra125", bgra125)
cv2.imshow("bgra0", bgra0)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite("bgra.png", bgra)
cv2.imwrite("bgra125.png", bgra125)
cv2.imwrite("bgra0.png", bgra0)
Binary file added opencv/bgra0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added opencv/bgra125.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/keyboard_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys

sys.path.append('../')
from directkeys import PressKey, ReleaseKey, key_down, key_up
from direction_move import move

move('RIGHT')
Binary file removed test/test.jpg
Binary file not shown.
106 changes: 106 additions & 0 deletions test/weiyang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import cv2
import numpy as np
import torch
from models.experimental import attempt_load
from utils.general import non_max_suppression, xyxy2xywh
from grabscreen import grab_screen
from directkeys import key_press
from direction_move import move
names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush']


# 1-8 自动爬楼

weights = 'best.pt' # yolo5 模型存放的位置
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
half = device.type != 'cpu'
conf_thres = 0.3 # NMS的置信度过滤
iou_thres = 0.2 # NMS的IOU阈值
classes = None
agnostic_nms = False # 不同类别的NMS时也参数过滤
model = attempt_load(weights, device)

# img0 = cv2.imread("shiwu.jpg")
img0 = grab_screen((0, 0, 1280, 800))

img = cv2.cvtColor(img0, cv2.COLOR_BGRA2BGR)
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB
img = np.ascontiguousarray(img)
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]
# Apply NMS
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]

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 = []
for idx, (*xyxy, conf, cls) in enumerate(reversed(det)):
# print(idx,conf,cls)
# if True: # Write to file
# xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
# with open('giraffe_label.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] == 'giraffe':
hero_index = idx
# 游戏
thx = 30 # 捡东西时,x方向的阈值
thy = 30 # 捡东西时,y方向的阈值
attx = 150 # 攻击时,x方向的阈值
atty = 50 # 攻击时,y方向的阈值
skillDis = 800
skillDis = 400

if 'giraffe' in cls_object:
hero_xywh = img_object[hero_index]
# else:
# continue
# 对屏幕中的monster 进行平均 最终一个
if 'orange' in cls_object:
# 打怪
for idx, (c, box) in enumerate(zip(cls_object, img_object)):
print(c, box)
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
elif 'material' in cls_object:
if 'option' in cls_object:
# 聚物捡东西 f2 xxx
else:
# 普通捡东西
elif 'boss' in cls_object:
# 打boss
key_press('g')
elif 'sinan' in cls_object:
# 下一局 出现思南选择界面 选择1-8阶随机一个思南 鼠标移动点击
# 中场清理背包
# if 1-8 in cls_object:
# nextGame
# else:
# shutDown and 通知爬楼完成,需要补货,清理背包 返回

Empty file removed test/yoloModel.py
Empty file.

0 comments on commit 91d89fb

Please sign in to comment.