首页 > 系统相关 >【嵌入式linux开发】旭日x3派部署自己训练的yolov5模型(安全帽识别、视频流推理、yolov5)

【嵌入式linux开发】旭日x3派部署自己训练的yolov5模型(安全帽识别、视频流推理、yolov5)

时间:2024-09-16 21:26:15浏览次数:3  
标签:yolov5 img 安全帽 image 视频流 cv2 model data

旭日x3派部署自己训练的模型(安全帽识别、视频流推理、yolov5-6.2)windows,框架pytorch,python3.7

效果

<iframe allowfullscreen="true" data-mediaembed="youku" frameborder="0" id="XHK9yNQM-1710507983460" src="https://player.youku.com/embed/XNjM4MTU5MTg2OA=="></iframe>

模型训练

进官网可克隆yolov5:https://github.com/ultralytics/yolov5/tree/v6.2,这里选择6.2。数据集直接使用现成的:安全帽识别。
整个训练过程参考:炮哥带你学
需要注意的是这里是6.2,参考博客是5.0,过程会有一些不同,训练时报错直接百度即可。
最终需要的pt文件位于:runs/train/exp/weights下:在这里插入图片描述

模型转换

docker环境搭建及启动、挂载文件参考上一篇博客:https://blog.csdn.net/m0_71523511/article/details/136546588

1、pt模型文件转onnx

①修改export.py文件:
在这里插入图片描述
②导出onnx:
运行export.py文件:
在这里插入图片描述

2、检查onnx模型

在挂载目录中的BPUCodes文件夹中新建文件夹yolov5-6.2_hat_2,将上一步得到的onnx模型复制一份进来。
在这里插入图片描述

打开docker桌面版,按下win+R进行命令符,在命令符中进入docker并将一些文件挂载进去,这里的命令是上一篇一样的:

docker run -it --rm -v "G:\bushu_xiangguan\horizon_xj3_open_explorer_v2.2.3a_20220701":/open_explorer -v "G:\bushu_xiangguan\Codes\dateset":/data/horizon_x3/data -v "G:\bushu_xiangguan\BPUCodes":/data/horizon_x3/codes openexplorer/ai_toolchain_centos_7:v1.13.6

在这里插入图片描述
输入以下指令进行检查:

hb_mapper checker --model-type onnx --march bernoulli2 --model best.onnx

在这里插入图片描述

3、准备校准数据

在yolov5-6.2_hat_2文件夹下新建prepare_calibration存放待校准数据,新建一个prepare_calibration_data.py文件,执行之后就可以在calibration_data下生成校准数据。

# prepare_calibration_data.py
import os
import cv2
import numpy as np

src_root = '/data/horizon_x3/codes/yolov5-6.2_hat_2/prepare_calibration'    #存放待校准图片的文件夹
cal_img_num = 100  
dst_root = '/data/horizon_x3/codes/yolov5-6.2_hat_2/calibration_data'     #存放输出校准数据的文件夹


num_count = 0
img_names = []
for src_name in sorted(os.listdir(src_root)):
    if num_count > cal_img_num:
        break
    img_names.append(src_name)
    num_count += 1

if not os.path.exists(dst_root):
    os.system('mkdir {0}'.format(dst_root))


def imequalresize(img, target_size, pad_value=127.):
    target_w, target_h = target_size
    image_h, image_w = img.shape[:2]
    img_channel = 3 if len(img.shape) > 2 else 1

    scale = min(target_w * 1.0 / image_w, target_h * 1.0 / image_h)
    new_h, new_w = int(scale * image_h), int(scale * image_w)

    resize_image = cv2.resize(img, (new_w, new_h))

    pad_image = np.full(shape=[target_h, target_w, img_channel], fill_value=pad_value)

    dw, dh = (target_w - new_w) // 2, (target_h - new_h) // 2
    pad_image[dh:new_h + dh, dw:new_w + dw, :] = resize_image

    return pad_image

for each_imgname in img_names:
    img_path = os.path.join(src_root, each_imgname)

    img = cv2.imread(img_path)  # BRG, HWC
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # RGB, HWC
    img = imequalresize(img, (640, 640))      #训练时是多少就写多少
    img = np.transpose(img, (2, 0, 1))  # RGB, CHW

    dst_path = os.path.join(dst_root, each_imgname + '.rgbchw')
    print("write:%s" % dst_path)
    img.astype(np.uint8).tofile(dst_path) 

print('finish')

执行python3 prepare_calibration_data.py即可:
在这里插入图片描述
此时目录结构如下:
在这里插入图片描述

4、onnx转bin

转换模型需要yaml参数文件,具体含义参考https://blog.csdn.net/Zhaoxi_Li/article/details/125516265
在yolov5-6.2_hat_2文件夹下新建model_convert.yaml文件:

model_parameters:
  onnx_model: './best.onnx' 
  output_model_file_prefix: 'hat_yolov5_6.2' 
  march: 'bernoulli2'
input_parameters:
  input_type_train: 'rgb'
  input_layout_train: 'NCHW'
  input_type_rt: 'nv12'
  norm_type: 'data_scale'
  scale_value: 0.003921568627451
  input_layout_rt: 'NHWC'
calibration_parameters:
  cal_data_dir: './calibration_data' 
  calibration_type: 'max'
  max_percentile: 0.9999
compiler_parameters:
  compile_mode: 'latency'
  optimize_level: 'O3'
  debug: False
  core_num: 2 

然后执行:hb_mapper makertbin --config model_convert.yaml --model-type onnx:
在这里插入图片描述
此时自动生成model_output文件夹,里面包含了bin模型:
在这里插入图片描述

上板视频流推理

1、图片推理

在这里插入图片描述
https://developer.horizon.cc/forumDetail/112555549341653639,这篇帖子介绍了cython,将上图有的文件全部拷到板端中,包括前面转成的bin文件。如下进行推理:在这里插入图片描述
推理结果:
在这里插入图片描述
我的这个模型只训练了五轮,对图片的识别率不错,后续的视频流推理容易出错,轮次多点应该就好了。

2、视频流推理

自己新建一个py文件,代码如下:

import numpy as np
import cv2
import os
from hobot_dnn import pyeasy_dnn as dnn
from bputools.format_convert import imequalresize, bgr2nv12_opencv

import lib.pyyolotools as yolotools

def get_hw(pro):
    if pro.layout == "NCHW":
        return pro.shape[2], pro.shape[3]
    else:
        return pro.shape[1], pro.shape[2]

def format_yolov5(frame):
    row, col, _ = frame.shape
    _max = max(col, row)
    result = np.zeros((_max, _max, 3), np.uint8)
    result[0:row, 0:col] = frame
    return result

# 加载模型和设置参数
model_path = 'hat_yolov5_6.2.bin'
classes_name_path = 'coco_classes.names'
models = dnn.load(model_path)
model_h, model_w = get_hw(models[0].inputs[0].properties)
print("Model Height:", model_h, "Model Width:", model_w)

thre_confidence = 0.4
thre_score = 0.25
thre_nms = 0.45
colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)]

# 打开摄像头
cap = cv2.VideoCapture(8)  # 使用第一个摄像头(如果有多个摄像头,可能需要更改参数)

# 主循环:读取帧,进行目标检测,显示结果
while True:
    ret, frame = cap.read()  # 读取一帧图像
    if not ret:
        print("Error: Couldn't capture frame")
        break

    inputImage = format_yolov5(frame)
    img = imequalresize(inputImage, (model_w, model_h))
    nv12 = bgr2nv12_opencv(img)

    t1 = cv2.getTickCount()
    outputs = models[0].forward(nv12)
    t2 = cv2.getTickCount()
    outputs = outputs[0].buffer
    print('Inference time: {0} ms'.format((t2 - t1) * 1000 / cv2.getTickFrequency()))

    image_width, image_height, _ = inputImage.shape
    fx, fy = image_width / model_w, image_height / model_h
    t1 = cv2.getTickCount()
    class_ids, confidences, boxes = yolotools.pypostprocess_yolov5(outputs[0][:, :, 0], fx, fy,
                                                                   thre_confidence, thre_score, thre_nms)
    t2 = cv2.getTickCount()
    print('Post-processing time: {0} ms'.format((t2 - t1) * 1000 / cv2.getTickFrequency()))
    
    with open(classes_name_path, "r") as f:
        class_list = [cname.strip() for cname in f.readlines()]

    for (classid, confidence, box) in zip(class_ids, confidences, boxes):
        color = colors[int(classid) % len(colors)]
        cv2.rectangle(frame, box, color, 2)
        cv2.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), color, -1)
        #cv2.putText(frame, str(classid), (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 0))
        cv2.putText(frame, class_list[classid], (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0,0,0))
    cv2.imshow('frame', frame)  # 显示帧
    if cv2.waitKey(1) & 0xFF == ord('q'):  # 按下 'q' 键退出循环
        break

# 释放资源并关闭窗口
cap.release()
cv2.destroyAllWindows()

这个需要通过hdmi将开发板与显示屏连接,才能看到实时画面,大概10帧左右,模型还可以简化,15帧应该很轻松。
最终效果如本文开头所示。

标签:yolov5,img,安全帽,image,视频流,cv2,model,data
From: https://blog.csdn.net/m0_71523511/article/details/142307320

相关文章

  • Yolov5水果分类识别+pyqt交互式界面
    Yolov5FruitsDetectorYolov5是一种先进的目标检测算法,可以应用于水果分类识别任务。结合PyQT框架,可以创建一个交互式界面,使用户能够方便地上传图片并获取水果分类结果。以下将详细阐述Yolov5水果分类识别和PyQT交互式界面的实现。Yolov5是由Ultralytics公司开......
  • yolov5障碍物识别-雪糕筒识别(代码+教程)
    简介这是一个检测交通锥并识别颜色的项目。我使用yolov5来训练和检测视锥细胞。此外,我使用k均值来确定主色,以对锥体颜色进行分类。目前,支持的颜色为红色、黄色、绿色和蓝色。其他颜色被归类为未知。数据集和注释我使用了一个自收集的锥体数据集,其中包含303张锥体......
  • 视频智能识别安全帽佩戴
    视频智能识别安全帽佩戴系统能够从繁杂的工地、煤矿、车间等场景下同时对多个目标是否戴安全帽穿反光衣进行实时识别,当视频智能识别安全帽佩戴系统发现作业人员没有戴安全帽、穿反光衣或者戴安全带,系统会及时报警提醒,并抓拍存档。视频智能识别安全帽佩戴系统识别安全帽颜色如红色......
  • 摄像机识别未戴安全帽
    摄像机识别未戴安全帽系统利用边缘计算+机器学习与深度学习技术,摄像机识别未戴安全帽系统借助现场部署的监控摄像机RTSP协议访问摄像机视频流,实时获取,实时分析,实时报警,并且抓拍人像分析人员信息、识别是不是戴安全帽、同歩声音报警,将报警信息快照和报警视频存入数据库及时推送给相......
  • 佩戴安全帽 人员聚集识别
    佩戴安全帽人员聚集识别借助现场已经安装的监控摄像机实时监控现场画面,识别职工是不是戴安全帽,是不是人员聚集状态,进而发送警示和提醒。佩戴安全帽人员聚集识别系统选用最新神经网络算法和边缘计算,可以代替人的双眼,全自动识别各种各样违规操作如:反光衣穿戴识别、安全帽佩戴识别、......
  • 安全帽佩戴检测摄像头
    安全帽佩戴检测摄像头借助现场已有的监控摄像头或者专门安装内置算法的监控摄像头,对现场人员安全帽佩戴进行实时识别检测。安全帽佩戴检测摄像头通过RTSP协议访问摄像机视频流,实时获取分析。立即识别视频监控区域未戴安全帽的工人,并实时分析抓拍警报。施工工地是一个存有安全隐......
  • 工人是否佩戴安全帽图像识别
    工人是否佩戴安全帽图像识别系统能从繁杂的场景下对对未戴安全帽多个目标同时开展识别分析,识别、记录和预警提醒。工人是否佩戴安全帽图像识别系统若发现违规操作,直接向有关人员推送报警消息记录,协助有关管理者进行安全生产工作,大大提升了安全监督的时效性,减少了人力成本。工人......
  • 安全帽佩戴识别系统介绍
    安全帽佩戴识别系统介绍工作原理是对作业人员是否佩戴安全帽开展视频录像、识别分析、追踪和预警提醒,安全帽佩戴识别系统借助现场已有的监控摄像头,通过现场视频监控实时分析和预警,判定是否有违章不佩戴安全帽行为。安全帽佩戴识别系统一旦发现工作人员不戴安全帽或违规抽烟或者不......
  • 摄像头识别安全帽
    摄像头识别安全帽佩戴系统依据现场已经部署的监控摄像头,实时识别现场监控画面,分析工作人员是不是戴安全帽,摄像头识别安全帽佩戴系统对进到施工作业区域的员工进行全自动识别,当系统检验人员未戴安全帽时,可以立即警报,报警系统同步消息提醒到后台管理人员。摄像头识别安全帽佩戴系......
  • 安全帽图像识别算法
    安全帽图像识别算法依据AI深度学习+边缘计算,通过机器视觉ai分析检测算法可以有效识别工人是不是合规和佩戴安全帽,安全帽图像识别算法提高视频监控不同场景下的主动分析与识别报警能力。安全帽图像识别算法系统搭载了全新的人工智能图像识别技术实时分析现场监控画面图像,与人力监管......