基于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的根目录就可以

猜你喜欢

转载自blog.csdn.net/m0_74333893/article/details/129165333
今日推荐