自学内容网 自学内容网

基于yolov8的游戏人物自动锁定功能

1️⃣加载模型
2️⃣获取屏幕内容
3️⃣获取人物坐标
4️⃣鼠标移动到指定位置

import math
import random
import time
import mss
from PIL import Image
import pyautogui
import torch
from pynput.mouse import Controller
from ultralytics import YOLO
#import win32api, win32con

# 加载本地模型
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = YOLO('yolov8.pt').to(device)
print("Model loaded.")

# 定义屏幕宽高
screen_width, screen_height = pyautogui.size()
game_width = screen_width
game_height = screen_height

rect = (0, 0, game_width, game_height)
m = mss.mss()
mouse_controller = Controller()  # 初始化鼠标控制器
#Windows
# def move(dx, dy):
#     # 鼠标相对当前位置移动
#     win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE,
#                          int(dx * 65536 / game_width), int(dy * 65536 / game_height), 0, 0)
#mac
def move(dx, dy):
    # 鼠标相对当前位置移动
    pyautogui.moveRel(dx, dy)



def window_capture():
    # 使用mss库进行截图
    with mss.mss() as sct:
        sct_img = sct.grab(rect)
        img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
    return img

# 主循环
while True:

    start_time = time.time()

    try:
        # 截取屏幕
        screen_shot = window_capture()

        # 使用模型进行推理
        results = model(screen_shot)

        # 提取检测结果
        boxes = results[0].boxes.cpu().numpy()

        # 存储有效检测结果
        detections = []
        for box in boxes:
            class_id = int(box.cls)
            confidence = float(box.conf)
            if class_id == 0 and confidence > 0.5:
                xmin, ymin, xmax, ymax = box.xyxy[0]
                center_x = int((xmin + xmax) / 2)
                #center_y = int((ymin + ymax) / 2)
                center_y = int(ymax-(ymax - ymin) * 0.82)
                detections.append((center_x, center_y))
        print(detections)
        if detections:
            # 获取鼠标当前位置
            mouse_x, mouse_y = mouse_controller.position

            # 计算每个检测结果与鼠标位置的距离,并找到最近的目标
            distances = [math.sqrt((mouse_x - x) ** 2 + (mouse_y - y) ** 2) for x, y in detections]
            closest_index = distances.index(min(distances))
            closest_target = detections[closest_index]

            # 控制鼠标移动到最近目标处
            delta_x = closest_target[0] - mouse_x
            delta_y = closest_target[1] - mouse_y
            move(delta_x, delta_y)

    except Exception as e:
        print(f"An error occurred: {e}")

    elapsed_time = time.time() - start_time
    print(f"Elapsed time: {elapsed_time:.2f} seconds")

原文地址:https://blog.csdn.net/qq_62975494/article/details/142567333

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!