• YOLO7 姿势识别实例


    上文搭建了YOLO7开发环境,并进行了物体定位测试。参见:YOLO7环境搭建、代码测试。本文将介绍如何使用YOLO7进行姿势识别。
    姿势识别

    1. 预训练模型

    下载YOLO7姿势识别预训练模型(点击下载),将下载的yolov7-w6-pose.pt放到YOLO7项目根目录下。
    预训练模型下载

    2. 测试代码

    在项目跟目录下,新建文件pos_reg.py,并输入如下代码:

    # 姿势识别
    
    # 导入类库
    import matplotlib.pyplot as plt
    import torch
    import cv2
    from torchvision import transforms
    import numpy as np
    from utils.datasets import letterbox
    from utils.general import non_max_suppression_kpt
    from utils.plots import output_to_keypoint, plot_skeleton_kpts
    
    # 加载模型
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    weigths = torch.load('yolov7-w6-pose.pt')
    model = weigths['model']
    model = model.half().to(device)
    _ = model.eval()
    
    
    # 读取图片
    image = cv2.imread('./person.jpg')
    image = letterbox(image, 960, stride=64, auto=True)[0]
    image_ = image.copy()
    image = transforms.ToTensor()(image)
    image = torch.tensor(np.array([image.numpy()]))
    image = image.to(device)
    image = image.half()
    
    # 姿势识别
    output, _ = model(image)
    
    # 输出结果
    output = non_max_suppression_kpt(output, 0.25, 0.65, nc=model.yaml['nc'], nkpt=model.yaml['nkpt'], kpt_label=True)
    output = output_to_keypoint(output)
    nimg = image[0].permute(1, 2, 0) * 255
    nimg = nimg.cpu().numpy().astype(np.uint8)
    nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
    for idx in range(output.shape[0]):
        plot_skeleton_kpts(nimg, output[idx, 7:].T, 3)
    
    
    # 保存结果
    cv2.imwrite("pos_reg.jpg",nimg)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3. 运行代码

    找一张图片放到项目根目录下(示例图片下载),命名为person.jpg。打开并运行pos_reg.py,运行完成后会输出pos_reg.jpg,即姿势识别后的图片。
    运行代码
    姿势识别

    4. 问题

    如果提示错误:RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.,则需修改utils/plots.py的442行和443行,将将cpu().numpy()修改为cpu().detach().numpy()

    def output_to_keypoint(output):
        # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
        targets = []
        for i, o in enumerate(output):
            kpts = o[:,6:]
            o = o[:,:6]
            # 将cpu().numpy()修改为cpu().detach().numpy()
            for index, (*box, conf, cls) in enumerate(o.cpu().detach().numpy()): 
                targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf, *list(kpts.cpu().detach().numpy()[index])])
        return np.array(targets)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    加密货币这趟车,坚持的意义何在?2021-05-25
    简单案例比较Lambda和方法引用的差别
    c++模板
    Java实现手机验证码登录和SpringSecurity权限控制
    java springboot VUE粮食经销系统开发mysql数据库web结构java编程计算机网页源码maven项目
    【SpringCloud】Ribbon负载均衡原理、负载均衡策略、饥饿加载
    帧间预测一些概念
    MySQL中的锁机制详解
    三步,金蝶K3的数据可视化了
    Unity构建详解(6)——SBP的Bundle写操作生成
  • 原文地址:https://blog.csdn.net/maerdym/article/details/125867540