使用YoloV5实现鼠标跟踪(源码)

import cv2
import numpy as np
from PIL import ImageGrab
import pyautogui
pyautogui.FAILSAFE=False
from yolov5 import YOLOv5

def get_leftmost_person_center(detections):
    leftmost_center = None
    leftmost_x = float('inf')

    for det in detections:
        cls = det[-1]
        if cls == 0:  # 人类别的索引为0
            x1, y1, x2, y2 = det[:4]
            center_x = (x1 + x2) / 2
            center_y = (y1 + y2) / 2

            if center_x < leftmost_x:
                leftmost_x = center_x
                leftmost_center = (center_x.item(), center_y.item())

    return leftmost_center

# 初始化YOLOv5模型
model = YOLOv5('yolov5s.pt', device='cuda:0')

while True:
    # 屏幕截图
    screenshot = ImageGrab.grab()
    img = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)

    # 图像输入模型进行检测
    results = model.predict(img)

    # 获得最左侧人的检测框中心位置
    leftmost_center = get_leftmost_person_center(results.xyxy[0])

    if leftmost_center:
        # 获取当前鼠标位置
        current_position = pyautogui.position()

        # 计算x和y方向上的增量
        delta_x = leftmost_center[0] - current_position[0]
        delta_y = leftmost_center[1] - current_position[1]

        # 平滑移动鼠标到目标位置
        pyautogui.move(delta_x, delta_y, duration=0.25)

        # 打印坐标信息
        print("最左侧人的检测框中心位置:", leftmost_center)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值