基于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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我把把C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值