yolov5 应用整理_yolov5应用

如得到yolov5模型的输出为: float32[1,25200,85]
一个yolov5 onnx模型输出
如何得到目标的坐标呢? 可以理解为一个三维数组 out[1][25200][85]
25200: 有25200个目标,需要进行过滤处理.
85: [0] [1]为中心点xy坐标;
[2] [3] 为wh;
[4]为置信度,
后面的80个为80个分类的置信度,取最大值的位置为分类目标, 值*[4]=最终置信度.
然后进行nms处理, 过滤掉目标多余的坐标框.

BM1684

参考: 算能demon https://github.com/sophgo/sophon-demo
官方的Demon对图片转换有些不足,你需要进行对其处理的完善.

算能官方将ffmpegopencv两个库进行了改编,增加了自己的编解码,入门难度简单很多.
对于ffmpeg只需要进行AVFramebm_image的相互转换即可.
目前bm1684官方说明为64对其.

	AVFrame *dst = av_frame_alloc();
	dst->width = src->width;
	dst->height = src->height;
	dst->format = AV_PIX_FMT_YUV420P;
	av_frame_get_buffer(dst, 64);

昇腾

见昇腾社区:
https://www.hiascend.com/document/detail/zh/canncommercial/63RC2/inferapplicationdev/aclcppdevg/aclcppdevg_0000.html

目前已经研究出 昇腾310 上的yolov5的转换, 昇腾在yolov5的前处理进行AIPP功能增加,使其能进行yuv数据的输入.

目前经过研究:在鲲鹏atlas800-3000中,纯使用cpu进行ffmpeg拉流解码可以达到80路视频

atc转换om模型说明

需要先将pt模型转换成onnx模型

# 将yolov5s.onnx模型转换为om模型
# 使用aipp时,输入格式为多种,这里是使用int8数据的NV12图片640大小输入
atc --model=yolov5s.onnx --framework=5 --output=yolov5s_aipp --input_format=NCHW --input_shape="input_image:1,3,640,640" --log=info --soc_version=Ascend310B1 --insert_op_conf=./aipp_YOLOv5.config 

aipp_YOLOv5.config参考 昇腾CANN => 色域转换配置说明

aipp_op {
    aipp_mode:static
    input_format : YUV420SP_U8
    src_image_size_w : 640
    src_image_size_h : 640
    crop: false
    load_start_pos_h : 0
    load_start_pos_w : 0
    crop_size_w : 640
    crop_size_h: 640
    csc_switch : true
    rbuv_swap_switch : false
    # 色域转换
    matrix_r0c0: 256
    matrix_r0c1: 0
    matrix_r0c2: 359
    matrix_r1c0: 256
    matrix_r1c1: -88
    matrix_r1c2: -183
    matrix_r2c0: 256
    matrix_r2c1: 454
    matrix_r2c2: 0
    input_bias_0: 0
    input_bias_1: 128
    input_bias_2: 128
    # 均值归一化
    min_chn_0 : 0
    min_chn_1 : 0
    min_chn_2 : 0
    var_reci_chn_0: 0.003921568627451
    var_reci_chn_1: 0.003921568627451
    var_reci_chn_2: 0.003921568627451
    }

通过其API来获取模型的信息:

#使用RGB输入格式时(可以用opencv读取图片/视频缩放后直接输入)
input[0] size:4915200 fmt:0(ACL_FORMAT_NCHW) dtype:0(ACL_FLOAT) dims(4):input_image= 1,3,640,640
#其中输入数据长度计算方式: 数据大小见 `sizeof(FLOAT)=4`
#1*640*640*3*4=4,915,200

#使用AIPP的NV12格式时(需要dvpp进行解码成NV12并进行缩放)
input num:1
input[0] size:614400 fmt:1(ACL_FORMAT_NHWC) dtype:4(ACL_UINT8) dims(4):input_image
1,640,640,3,
out num:1
output[0] fmt:2(ACL_FORMAT_ND) dtype:0(ACL_FLOAT) dims(3):Concat_309:0:output0
1,25200,85,
# 其中: 1*640*640*1.5=614400 

一个调用模型并检测视频的例子

import cv2
import torch
import numpy as np
import time
import datetime

from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords, xyxy2xywh, check_img_size, xywh2xyxy
from utils.torch_utils import select_device
from utils.augmentations import letterbox

video_path = "Y:\\220411150420220411_150441.mp4"

if __name__ == '__main__':

    # 指定你的Yolov5权重文件的路径
    model_weights = "../yolov5s.pt"
    model_weights = "E:\\src\\yolov5\\lxmodels\\202204\\best.pt"

    device = select_device('cuda:0')  # 使用GPU,如果没有GPU,可以使用'cpu'

    # 加载Yolov5模型
    model = attempt_load(model_weights, map_location=device)

    # 设置模型为评估模式
    # model.eval()

    stride = int(model.stride.max())  # model stride
    names = model.module.names if hasattr(model, 'module') else model.names  # get class names
    imgsz = check_img_size([640, 640], s=stride)  # check image size
    model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters())))  # run once

    print(names)

    # 打开视频文件
    cap = cv2.VideoCapture(video_path)
    print(f"打开视频{video_path}")
    frame_index = 0

    cv2.namedWindow("Yolov5 Object Detection", cv2.WINDOW_NORMAL)

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frame_index = frame_index + 1

        #frame = cv2.imread("test.jpg")

        bkimage = cv2.copyTo(frame, None)

        # 使用Yolov5进行目标检测
        img = letterbox(frame)[0]

        # Convert
        img = img.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGB
        img = np.ascontiguousarray(img)

        img = torch.from_numpy(img).to(device)
        img = img.float()  # uint8 to fp16/32
        img = img / 255.0  # 0 - 255 to 0.0 - 1.0
        if len(img.shape) == 3:
            img = img[None]  # expand for batch dim

        pred = model(img)[0]
        pred = non_max_suppression(pred, conf_thres=0.3, iou_thres=0.5)

        for i, det in enumerate(pred):  # per image
            gn = torch.tensor(frame.shape)[[1, 0, 1, 0]]  # normalization gain whwh
            # s = '%gx%g ' % img.shape[2:]  # print string

            if len(det):
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()

                # Print results
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    # s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string

                # Write results
                for *xyxy, conf, cls in reversed(det):
                    if conf < 0.88:
                        continue
                    # [x1, y1, x2, y2]
                    # xywh = xyxy2xywh(torch.tensor(xyxy).view(1, 4)).view(-1).tolist()

                    # label format
                    # line = (cls, *xywh, conf)
                    label = f'{names[int(cls)]} {conf:.2f}'
                    # 截图
                    # try:
                    # >>> 814-306 854-331 conf:0.8818694949150085
                    # 834.0 318.5 40.0 25.0
                    # print(f">>> {int(xyxy[0])}-{int(xyxy[1])} {int(xyxy[2])}-{int(xyxy[3])} conf:{conf}")
                    # print(f"{xywh[0]} {xywh[1]} {xywh[2]} {xywh[3]}")

                    # crop = frame[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])]
                    # date_time = datetime.datetime.fromtimestamp(int(time.time() * 1000) / 1000.0).strftime(
                    #     "%Y%m%d%H%M%S-%f")
                    # cv2.imwrite(f"crop/{date_time}-{conf}.jpg", crop)
## 最后

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**

**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/8b8bc467f3d513a2d9487a3236bd6652.png)

![img](https://img-blog.csdnimg.cn/img_convert/e7ec92b91f5de9e612e612d5e57c37b7.jpeg)

![img](https://img-blog.csdnimg.cn/img_convert/ffc83e6c1da15dc79df8b313d51cc1f6.png)

 ![img](https://img-blog.csdnimg.cn/img_convert/b4f3f9f9803a47f6bf603397345714f9.png)

![img](https://img-blog.csdnimg.cn/img_convert/0050131d1beeb488fea7ba49802923bb.png)

![img](https://img-blog.csdnimg.cn/img_convert/487cee369695258cf6cb54f82c76cbd2.png)

![](https://img-blog.csdnimg.cn/img_convert/6396f1f0666a63157f7a3d2a8d908901.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**

[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!


j2kCbc-1715603688967)]

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**

[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!


  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值