Ai realizes automatic aiming of FPS games yolov5fps self-aiming

Hello everyone, I am Picasso (lock!)

Today, I will share a source code analysis and principle explanation of Yolov5 FPS and gun. The code is relatively rough. If you have any optimization methods, you can leave a message and point out, and you can communicate and learn together. 


Things to know and problems you may encounter
1. Calculate the distance between the xy coordinate point and the xy coordinate point of the current mouse


2. Obtain the window handle. This article uses the window name to obtain the handle


3. Reasoning method: GPU is used in this article (why? The speed is fast, because I have a 3060 graphics card~).


4. Obtain the width and height of the screen and the coordinates of the enemy and calculate which is the nearest enemy and control the mouse to go to the enemy's body and head. (Why is it the body and the head? Because there is another way, that is, the coordinates of the enemy’s head can be calculated only by the coordinates of the enemy’s body.) Dry goods~


5. There is also mouse button status acquisition.


Let's start with the text, let's start the code analysis from the beginning.
First, a distance calculation between two xy coordinates


class Point():
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2


class Line(Point):
    def __init__(self, x1, y1, x2, y2):
        super().__init__(x1, y1, x2, y2)

    def getlen(self):
        changdu = math.sqrt(math.pow((self.x1 - self.x2), 2) + math.pow((self.y1 - self.y2), 2))
        return changdu


The above code can be used as follows

L1 = Line(x1, y1, x2, y2)  #传入两个xy坐标
L1.getlen() #return出两个坐标点的直线距离


Next, go directly to the full code analysis, you can separate the class and method

# 这里是导入依赖,需要这些库
import math
import sys
import time

import torch
import win32api
import win32con
import win32gui
from PyQt5.QtWidgets import QApplication
from pynput.mouse import Controller
import mouse

#这里这俩class就是文章上面说的那个传入两个坐标点,计算直线距离的
class Point():
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
class Line(Point):
    def __init__(self, x1, y1, x2, y2):
        super().__init__(x1, y1, x2, y2)

    def getlen(self):
        changdu = math.sqrt(math.pow((self.x1 - self.x2), 2) + math.pow((self.y1 - self.y2), 2))
        return changdu

#第一步:我们获取到某FPS游戏的窗口句柄
hwnd = win32gui.FindWindow(None, "穿越火线")
#这个方法是获取上面句柄窗口的窗口截图,用的是PyQt截图,有速度更快更好的方式的话可以换上
#截图完毕后保存在根目录的cfbg.bmp文件
def screen_record():
    app = QApplication(sys.argv)
    screen = QApplication.primaryScreen()
    img = screen.grabWindow(hwnd).toImage()
    img.save("cfbg.bmp")


#这里就是调用我们那yolo模型来进行推理啦,我设置的是cuda,也就是英伟达的GPU,因为cpu太慢了。
#如果自己的不能使用GPU推理的话把下面这两行改改,改成cpu的就可以了。
device = torch.device("cuda")
model = torch.hub.load('F:/yolov5-master', 'custom', 'F:/yolov5-master/yolov5n6.pt',
                       source='local', force_reload=False)  # 加载本地模型
# 这里是定义屏幕宽高[其实这俩就是游戏所对应的分辨率,比如:游戏里1920*1080这里就是1920*1080]
game_width = 1024
game_height = 768
# 这边就是开始实时进行游戏窗口推理了
#无限循环 -> 截取屏幕 -> 推理模型获取到每个敌人坐标 -> 计算每个敌人中心坐标 -> 挑选距离准星最近的敌人 -> 如果左键是按下状态则控制鼠标移动到敌人的身体或者头部(本文计算方式是移动到头部)
while True:
    # 截取屏幕
    screen_record()
    # 使用模型
    model = model.to(device)
    img = 'cfbg.bmp' 
    # 开始推理
    results = model(img)
    # 过滤模型
    xmins = results.pandas().xyxy[0]['xmin']
    ymins = results.pandas().xyxy[0]['ymin']
    xmaxs = results.pandas().xyxy[0]['xmax']
    ymaxs = results.pandas().xyxy[0]['ymax']
    class_list = results.pandas().xyxy[0]['class']
    confidences = results.pandas().xyxy[0]['confidence']
    newlist = []
    for xmin, ymin, xmax, ymax, classitem, conf in zip(xmins, ymins, xmaxs, ymaxs, class_list, confidences):
        if classitem == 0 and conf > 0.5:
            newlist.append([int(xmin), int(ymin), int(xmax), int(ymax), conf])
    # 循环遍历每个敌人的坐标信息传入距离计算方法获取每个敌人距离鼠标的距离
    if len(newlist) > 0:
        # 存放距离数据
        cdList = []
        xyList = []
        for listItem in newlist:
            # 当前遍历的人物中心坐标
            xindex = int(listItem[2] - (listItem[2] - listItem[0]) / 2)
            yindex = int(listItem[3] - (listItem[3] - listItem[1]) / 2)
            mouseModal = Controller()
            x, y = mouseModal.position
            L1 = Line(x, y, xindex, yindex)
            # 获取到距离并且存放在cdList集合中
            cdList.append(int(L1.getlen()))
            xyList.append([xindex, yindex, listItem[0], listItem[1], listItem[2], listItem[3]])
        # 这里就得到了距离最近的敌人位置了
        minCD = min(cdList)
        # 如果敌人距离鼠标坐标小于150则自动进行瞄准,这里可以改大改小,小的话跟枪会显得自然些
        if minCD < 150:
            for cdItem, xyItem in zip(cdList, xyList):
                if cdItem == minCD:
                    # 锁头算法:使用win32api获取左键按下状态,如果按下则开始自动跟枪
                    if win32api.GetAsyncKeyState(0x01):
                        # 控制鼠标移动到某个点:看不懂计算方式的话看文章下面讲解吧O(∩_∩)O
                        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(xyItem[0] - game_width // 2),int(xyItem[1] - (game_height - (xyItem[3] - xyItem[5])) // 2), 0, 0)
                    break

Ok, let's explain this line of code
win32api.mouse_event (mouse behavior: MOVE relative movement is used here, x coordinate, y coordinate, 0, 0)
The first parameter, first set the behavior of the mouse: use relative movement , why not use absolute movement, because absolute movement is invalid in the game
The second parameter, the relative movement distance of the x-axis: the x coordinate of the enemy - (screen width / 2) The
third parameter, the relative movement distance of the y-axis: Enemy y coordinates - (screen height - (enemy maximum y coordinates - enemy minimum y coordinates) / 2)
The first and second parameters are relatively easy to understand, but some people may be a bit confused about the calculation of the relative movement of the third y Damn, I drew a picture, I hope I can understand it.


Example diagram


 

In other fps games, we only need to modify the window name parameter and resolution parameter.

The above is the source code analysis and principle explanation of Yolov5 FPS and gun. Thank you for your support.

I'm Bi Jiasuo looking forward to your attention

 

Guess you like

Origin blog.csdn.net/weixin_69999177/article/details/125135969