yolov5代码复现及推理及可视化代码编写

本文介绍了如何在Python3.8.0及以上环境中使用YOLOv5进行训练、测试和部署,提供了从仓库克隆和安装依赖到创建Yolov5sDetector类,以及加载预训练模型和进行对象检测的详细步骤。最后展示了如何加载图像、执行推理并可视化检测结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请参阅 YOLOv5 文档,了解有关训练、测试和部署的完整文档。有关快速入门示例,请参阅下文。

1.yolov5代码复现

Python>=3.8.0 环境中克隆存储库并安装requirements.txt,包括 PyTorch>=1.8

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

2.utralytics_yolov5_master 推理及可视化

代码如下(示例):

import os
import sys
cwd = os.getcwd()
sys.path.insert(0, '/CSPLAT/TEST')
sys.path.append(cwd)

import numpy as np
import torch
import torch.nn as nn
import cv2


class Yolov5sDetector(nn.Module):
    def __init__(self, 
                 repo_or_dir = 'CSPLAT/TEST/utralytics_yolov5_master',
                 path = 'utralytics_yolov5_master/pretrain_model/yolov5s.pt', 
                 source = 'local'):
        super().__init__()
        self.detector = torch.hub.load(repo_or_dir , 'custom', path = path, source=source)
    
    def forward(self, img):
        '''
        Input: 
            img  shape:(260, 210, 3)
            img  shape:(260, 210, 3)
        Output:
            class_ids
            confidences: 
            boxes: 
            indices: an array containing the indices of the retained bounding boxes.
            
        '''
        with torch.no_grad():
            results = self.detector(img)
        person_results = results.xyxy[0][results.xyxy[0][:, 5] == 0]
        class_ids, confidences, boxes = [], [], []
        for detection in person_results:
            x1, y1, x2, y2, confidence, class_id = detection.tolist()
            class_ids.append(class_id)
            confidences.append(confidence)
            boxes.append([x1, y1, x2 - x1, y2 - y1])
        indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
        
        # for num, indice in enumerate(indices):
        #     bbox = boxes[indice]  # x,y,h,w  [0:21.7, 1:6.6, 2:155.2, 3:248.9]
        return boxes, indices
    
    def load_img(self, path, order='RGB'):
        img = cv2.imread(path, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION)
        if not isinstance(img, np.ndarray):
            raise IOError("Fail to read %s" % path)

        if order == 'RGB':
            img = img[:, :, ::-1].copy()

        img = img.astype(np.float32)
        return img
    
    def draw_bbox(self, draw_img, bbox, label = 'Human', label_color=(255,0,255), save_path=None):

        bbox = [round(bbox[0]), round(bbox[1]), round(bbox[0] + bbox[2]), round(bbox[1] + bbox[3]) ]
        
        draw_img = cv2.rectangle(draw_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(255,0,255), thickness=2)
        
        labelSize = cv2.getTextSize(label + '0', cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)[0]
        if bbox[1] - labelSize[1] - 3 < 0:
            draw_img = cv2.rectangle(draw_img,
                        (bbox[0], bbox[1] + 2),
                        (bbox[0] + labelSize[0], bbox[1] + labelSize[1] + 3),
                        color=label_color,
                        thickness=-1)
            draw_img = cv2.putText(draw_img, label,
                        (bbox[0], bbox[1] + labelSize + 3),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        0.5,
                        (0, 0, 0),
                        thickness=1)
        else:
            draw_img = cv2.rectangle(draw_img,
                        (bbox[0], bbox[1] - labelSize[1] - 3),
                        (bbox[0] + labelSize[0], bbox[1] - 3),
                        color=label_color,
                        thickness=-1)
            draw_img = cv2.putText(draw_img, label,
                        (bbox[0], bbox[1] - 3),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        0.5,
                        (0, 0, 0),
                        thickness=1)
        if save_path:
            cv2.imwrite(save_path, img=draw_img)
        return draw_img
        

if __name__ == "__main__":
    yolo_detector = Yolov5sDetector(
        repo_or_dir = '/CSPLAT/TEST/utralytics_yolov5_master',
        path = '/CSPLAT/TEST/utralytics_yolov5_master/pretrain_model/yolov5s.pt')
    
    img_path = '/CSPLAT/TEST/utralytics_yolov5_master/data/images/zidane.jpg'
    
    img = yolo_detector.load_img(
        path = img_path, 
        order='RGB'
    )
    
    boxes, indices = yolo_detector(img)
    print(boxes)
    
    draw_img = yolo_detector.draw_bbox(
        draw_img=img,
        bbox=boxes[0],
        save_path=None)

该处使用的url网络请求的数据。


可视化Human36m的检测结果

在这里插入图片描述
**注:**骨骼点检测不在本次任务中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

笔下万码生谋略

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

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

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

打赏作者

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

抵扣说明:

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

余额充值