基于YOLOV5的自动瞄准(附代码)

本篇使用CS:GO 进行测试,测试环境:3060 Laptop i7-12700h 底部有源码

在对YOLOV5 detect.py摸索几天后发现 YOLOV5 的架构真的很清晰,从应用的角度上来说是真的方便

YOLOV5 官方的detect.py 中有着非常完整的功能,从读取图片到调用摄像头都已经写好了代码,只需要阅读一下官方文档,更改下启动参数基本就可以完成物体检测的任务。

本次代码是基于 detect.py 精简和改进而来,代码都是现有的,但是自己上手体会一下流程才能更好理解

先说结论:

我个人感觉帧率还是偏低,对移动的物体的跟踪还是不够强,也勉强够用,后续优化应该会更好

但是相较于原版的detect.py 速度“快”了大半(这个快后面有解释)


过程分析:

基本流程:

对于一个辅助瞄准来说有着以下基本的流程:

获取屏幕 -> 目标检测 - 获得坐标 - 移动鼠标 - 判断是否已经瞄准 - 按下开火

我选择的是将 “判断是否已经瞄准”放到下一帧去处理(因为判断是需要再获取一次物体坐标的)


具体步骤:

获取屏幕:

https://www.simplifiedpython.net/python-screenshot/
Python获取屏幕截图的4种方法_jokerzhango.O的博客

除了这几种,YOLOV5中还有自带的截图模块:

dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
for path, im, im0s, vid_cap, s in dataset:

在这个5个截图方式中,我选择了 win32api(PyQt的性能我测的是没有win32api好的,也许是我的原因)

win32 api 截取 640* 640 的图片大概需要 10ms 左右, 不同的配置会有所差异

目标检测:

这倒是简单,毕竟YOLOV5中已经有源代码了,只需要正确地把图片送进去就可以

关于这个正确的是怎么个正确法 在下面有写踩坑,这里就不复述了

获得坐标:

for *xyxy, conf, cls in reversed(det):
    ppl_list = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()

ppl_list 中的是坐标框的xywh(检测框中心的 x y 坐标 , 检测框的宽度和高度)

要注意的是这几个数据都是百分比的形式(0.52314) 需要乘以图片的长宽才可以用

移动鼠标:

由于大部分游戏都有限制,导致例如pyautogui 类的库都没法使用,在我网上找了大半圈后,本着就简的原则 发现了pydirectinput 这个库,能够在游戏内移动视角,但那篇帖子找不到啦

判断是否已经瞄准:

这个是倒数第二简单的,用 ppl_list 中获得的检测框中心的(x,y)坐标与屏幕中心来计算距离

choose_close[0] <= 200

当这个距离小于一定值的时候就可以认为是瞄准了

按下开火:

这个是最简单的,当判断已经瞄准后直接点击:

    pydirectinput.click()

当然,可以按照实际情况执行下面这个命令

    time.sleep(1.5)

一些遇到的坑:

截图:

在利用 win32api 截图时,返回的是一个标准的 [x , y , c] (横 ,纵 , 颜色)的list,但是对于

im = torch.from_numpy(im).to(model.device)

其中的 im 需要的是一个 [c , x , y] 格式的图片,并且这个 c 是需要 [BGR]格式的

而非win32api返回的[RGB]格式

控制鼠标移动:

有时候会出现在游戏内无法控制鼠标,但是在鼠标焦点脱离游戏后就可以控制。

我的解决办法是管理员权限打开 PyChram 并且在游戏打开后运行程序

多线程:

利用 multiprocessing.Queue 传递变量的时候,变量的存放是“阻塞” 的

还有一些比较细致的问题就不放啦,还是需要自己实际体会下的 >_<

“全部”多进程:

利用multiprocessing 将 截图 |推理 | 后处理 | 控制鼠标移动、点击

这几项分开,通过 Queue 来传递变量,结果真的一言难尽,看到敌人头甚至会抖到转圈 (???)

初步推断是因为截图速度过快,导致推理的时候获得的数据是之前的,导致抖动

异步鼠标控制:

这个主要是pydirectinput.moveRel 有一个默认的保护机制,当点击完后会自动暂停 100ms

    pydirectinput.PAUSE = 0.0

一些测试数据:

'''
测试数据(3060 Laptop,i7 12700h):

######################################显示图片##############################################
无目标时(无鼠标移动)(调用win32api截图):
    异步显示图片:
        debug:512x640 (no detections), inference: 6.6ms, total: 20.23ms
        debug:512x640 (no detections), inference: 6.6ms, total: 20.77ms
        debug:512x640 (no detections), inference: 9.9ms, total: 14.29ms
        debug:512x640 (no detections), inference: 6.0ms, total: 20.88ms
    同步显示图片:
        debug:512x640 (no detections), inference: 9.4ms, total: 28.12ms
        debug:512x640 (no detections), inference: 9.3ms, total: 27.71ms
        debug:512x640 (no detections), inference: 9.3ms, total: 34.73ms
        debug:512x640 (no detections), inference: 9.3ms, total: 28.24ms
        
######################################移动鼠标############################################
无实时预览时(3个目标)(关闭鼠标暂停)
    同步移动鼠标
            SYNC[x y mouse_move_x mouse_move_y ] 305 304 243 -16 -7
        screen: 640,304,640,512: 512x640 4 persons, inference: 12.5ms, total: 28.05ms
            SYNC[x y mouse_move_x mouse_move_y ] 305 304 243 -16 -7
        screen: 640,304,640,512: 512x640 4 persons, inference: 6.0ms, total: 12.29ms
            SYNC[x y mouse_move_x mouse_move_y ] 305 304 243 -16 -7
        screen: 640,304,640,512: 512x640 4 persons, inference: 12.0ms, total: 24.37ms
            SYNC[x y mouse_move_x mouse_move_y ] 305 304 243 -16 -7
        screen: 640,304,640,512: 512x640 4 persons, inference: 23.9ms, total: 45.60ms
    异步移动鼠标
        暂时不考虑,pydirectinput 效率似乎比较低下,会有很明显的滞后

#####################################屏幕截图################################################
    使用yolo自带的屏幕获取(拖动森林图片移动)
        screen: 640,304,640,512: 512x640 (no detections), inference: 13.5ms, total: 21.08ms
        screen: 640,304,640,512: 512x640 (no detections), inference: 8.9ms, total: 27.76ms
        screen: 640,304,640,512: 512x640 (no detections), inference: 10.1ms, total: 20.67ms
        screen: 640,304,640,512: 512x640 (no detections), inference: 8.4ms, total: 20.55ms
    调用win32api获取屏幕(拖动森林图片移动)
        debug:512x640 (no detections), inference: 8.9ms, total: 21.25ms
        debug:512x640 (no detections), inference: 11.7ms, total: 19.96ms
        debug:512x640 (no detections), inference: 8.9ms, total: 20.19ms
        debug:512x640 (no detections), inference: 10.5ms, total: 20.80ms
'''
YOLO/Auto_Aiming.py at main · RedWhiteLuo/YOLO (github.com) 源码在这里
需要安装的库:
pip install pywin32
pip install pydirectinput
其余的在pycharm中都可以用pycharm安装
此文件放入yolo的根目录就可以
  • 10
    点赞
  • 167
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要实现 YOLOv5 自动瞄准,需要进行以下步骤: 1. 使用 YOLOv5 模型进行目标检测,获取图像中物体的位置和类别。可以使用 PyTorch 官方提供的 YOLOv5 模型或者从 GitHub 上下载其他开源实现。 2. 根据检测到的目标位置和图像中心位置计算出目标相对于中心点的偏移量。 3. 根据偏移量计算出需要调整的云台角度,可以使用串口或者其他通信方式将控制信号发送至云台。 4. 不断重复上述过程,实现自动瞄准的功能。 以下是一个简单的 Python 代码示例,用于实现基于 YOLOv5自动瞄准: ```python import cv2 import numpy as np import serial # 初始化串口通信 ser = serial.Serial('/dev/ttyUSB0', 9600) # 加载 YOLOv5 模型 model = cv2.dnn.readNet('yolov5s.onnx') while True: # 读取图像 ret, frame = cap.read() # 进行目标检测 blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False) model.setInput(blob) outputs = model.forward(model.getUnconnectedOutLayersNames()) # 解析检测结果 boxes, confidences, class_ids = [], [], [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * frame.shape[1]) center_y = int(detection[1] * frame.shape[0]) width = int(detection[2] * frame.shape[1]) height = int(detection[3] * frame.shape[0]) left = int(center_x - width / 2) top = int(center_y - height / 2) boxes.append([left, top, width, height]) confidences.append(float(confidence)) class_ids.append(class_id) # 判断是否检测到目标 if len(boxes) > 0: # 计算目标相对于中心点的偏移量 cx, cy = frame.shape[1] / 2, frame.shape[0] / 2 target_x, target_y, target_w, target_h = boxes[0] dx, dy = target_x + target_w / 2 - cx, target_y + target_h / 2 - cy # 计算需要调整的云台角度 pan_angle = dx / cx * 90 tilt_angle = dy / cy * 90 # 发送控制信号至云台 ser.write(b'P{:.2f}T{:.2f}'.format(pan_angle, tilt_angle)) ``` 注意,这只是一个简单的示例代码,实际应用中还需要考虑许多因素,例如相机和云台的校准、目标跟踪算法的优化等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值