当前位置: 首页 > news >正文

行空板上YOLO和Mediapipe视频物体检测的测试

Introduction

经过前面三篇教程帖子(yolov8n在行空板上的运行(中文),yolov10n在行空板上的运行(中文),Mediapipe在行空板上的运行(中文))的介绍,我们对如何使用官方代码在行空板上运行物体检测的AI模型有了基本的概念,并对常见的模型进行了简单的测试和对比。

在行空板上YOLO和Mediapipe图片物体检测的测试(中文)中我们对于行空板上使用YOLO和Mediapipe进行图片物体检测进行了测试。

进一步的,本文将

  • 对不同模型的视频物体检查进行详细的对比分析;
  • 进行针对在行空板上的视频物体检测进行代码编写和优化;
  • 对不同模型的帧率对比测试。

Note: 因为视频物体检测和图片物体检测用的是相同的模型,所以在检测准确性上不会有区别,所以检测结果的准确性可以直接参考以行空板上YOLO和Mediapipe图片物体检测的测试(中文)所做的测试。

yolo视频物体检测

不同onnx模型导出设置的表现对比

设置和代码

导出onnx模型的官方代码为:

from ultralytics import YOLO# Load a pretrained YOLOv10n model
model = YOLO("yolov10n.pt")#export onnx
model.export(format='onnx')

yolo系列模型在使用官方代码导出onnx格式模型的时候,有几个不同的选项:

参数类型默认值描述
imgszint or tuple640模型输入所需的图像大小。对于正方形图像可以是整数,对于特定尺寸可以是元组(高度,宽度)。
halfboolFalse支持FP16(半精度)量化,减少模型尺寸,并可能加快对支持硬件的推理。
dynamicboolFalse允许ONNX和TensorRT导出的动态输入大小,增强处理不同图像尺寸的灵活性。
simplifyboolFalse简化ONNX导出的模型图,潜在地提高性能和兼容性。
  • 其中,dynamic与imgsz不兼容;dynamic与half不兼容。

我们将分别对这几个选项的不同组合进行对比测试,分辨率都采用640。共包括以下模型:

设置代码
默认model.export(format=‘onnx’, imgsz=640)
dynamicmodel.export(format=‘onnx’, dynamic=True)
simplifymodel.export(format=‘onnx’, simplify=True, imgsz=640)
simplify和dynamicmodel.export(format=‘onnx’, simplify=True, dynamic=True)
halfmodel.export(format=‘onnx’, half=True, imgsz=640)
simlify和halfmodel.export(format=‘onnx’, simplify=True, half=True, imgsz=640)
测试结果

对不同设置的模型进行行空板+USB摄像头的视频物体检测测试,结果如下:

模型大小simplifyhalfdynamicimgsz帧率
YOLOv10n8.99MB×××6400.3029
YOLOv10n8.86MB×××0.3002
YOLOv10n8.95MB××6400.3009
YOLOv10n8.95MB××0.3011
YOLOv10n8.99MB××6400.2999
YOLOv10n8.95MB×6400.3017
分析

通过测试结果的统计,可以分析得到以下特点:

  • dynamic设置可以略微减小模型大小,也会略微降低运行速度;
  • simplify也可以略微减小模型大小,但极不显著,也会略微降低运行速度;
  • 如果simplify和dynamic同时开启,不如值开启dynamic在模型尺寸上减少的明显;
  • half设置并没有速度上的提升,反而有降低,这是因为行空板没有半精度优化的硬件支持。
小结
  1. 如果能事先知道输入图片的尺寸,就不要使用任何参数设置,只用imgsz设置图片尺寸;
  2. 如果实现不知道图片输入的尺寸,就使用dynamic=True;
  3. 不要设置half=True和simplify=True

不同尺寸输入的表现对比

yolo的测试结果
模型模型大小dynamicimgsz帧率
YOLOv10n8.99MB×6400.30
YOLOv10n8.91MB×4480.59
YOLOv10n8.87MB×3201.07
YOLOv10n8.86MB×2561.70
YOLOv10n8.84MB×1285.01
YOLOv8n12.2MB×6400.26
YOLOv8n12.1MB4480.53
YOLOv8n12.1MB3200.99
YOLOv8n12.0MB2561.59
YOLOv8n12.0MB1284.80
YOLOv8n12.0MB649.17
分析

可以看出,随着输入尺寸的减小,帧率显著提高。

yolov10n的帧率比yolov8n略高10%左右,同时模型大小减少25%。

小结
  • 对于视频检测,在行空板上以128的分辨率运行勉强可以使用;
  • 建议选择yolov10n,相较v8,内存占用和速度方面都有优势。

Mediapipe视频物体检测

设置

针对Mediapipe中的三个模型,我们分别测试了非量化和int8量化下的不同分辨率。

分辨率包括:

640、448、320、256、128

测试结果

模型\分辨率64044832025612864
efficientdet_lite00.420.420.420.420.420.43
efficientdet_lite0_int80.850.850.850.850.850.86
efficientdet_lite20.130.130.130.130.130.13
efficientdet_lite2_int80.270.270.270.270.270.27
ssd_mobilenet_v20.610.610.610.610.610.61
ssd_mobilenet_v2_int80.610.610.610.610.610.62

小结

  • 与图片的目标检测结果类似,不同的分辨率对这些模型而言没有影响;
  • 对于efficientdet_lite0和efficientdet_lite2,int8量化可以显著提速;int8量化对于ssd_mobilenet_v2没有影响

视频物体检测总结

在这里插入图片描述

我们在统计测试后发现了以下特点:

  • yolo系列随着图片分辨率下调,检测耗时显著减少;而Mediapipe不明显。这说明在小分辨率图片检测中,yolo系列有显著的速度优势;
  • det的两个模型进行int8量化之后速度显著提升,几乎不会损失准确性;
  • Mediapipe的模型在较大分辨率的时候相比yolo有显著的速度优势,但是准确性略低一点。在应用的时候,需要在准确性和速度上进行权衡。

模型选择建议

  • 限于视频帧率问题,我们认为最多使用320分辨率,使帧率达到1;
  • 如果物体较近或者较大,在低分辨率图片上也可以方便地提取特征,这种情况下推荐使用较低分辨率而速度较快的模型,如64帧率的yolov8n
  • 如果物体较远或者物体较小,则需要分辨率更高才能提取到足够的特征,这种情况下推荐选择yolov10n。如果需要更高分辨率的图片,而且可以接收帧率较低,可以采用ssd_mobilenet_v2.tflite模型。可以参考下面的流程来选取模型:

在这里插入图片描述

附录

使用yolov10n视频检测的代码

行空板+USB摄像头使用yolov10n进行物体检测的代码代码如下:

import cv2
import numpy as np
import onnxruntime as ort
import yaml
import time def preprocess(frame, input_size):#这里的resize使用nearest,可以提速大约0.3-0.5帧image = cv2.resize(frame, input_size,interpolation=cv2.INTER_NEAREST)# 转换图片到数组image_data = np.array(image).transpose(2, 0, 1)  # 转换成CHWimage_data = image_data.astype(np.float32)image_data /= 255.0  # 归一化image_data = np.expand_dims(image_data, axis=0)  # 增加batch维度return image_datadef postprocess(output, image, input_size, show_size, classes):for detection in output:x1, y1, x2, y2, conf , class_id = detectionif conf > 0.4:x1 = int(x1 / input_size[0] * show_size[0])x2 = int(x2 / input_size[0] * show_size[0])y1 = int(y1 / input_size[1] * show_size[1])y2 = int(y2 / input_size[1] * show_size[1])class_id = int(class_id)  cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)  # 画框class_name = classes[class_id]cv2.putText(image, class_name, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)return imagedef main():input_size = (128, 128)with open('ultralytics/cfg/datasets/coco.yaml', 'r', encoding='utf-8') as f:data = yaml.safe_load(f)classes = data['names']window_name = 'FullScreen Image'cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)# 加载模型session = ort.InferenceSession('yolov10n.onnx')input_name = session.get_inputs()[0].name# 打开摄像头。cap = cv2.VideoCapture(0)if not cap.isOpened():print("Cannot open camera")exit()prev_time = 0while True:ret, frame = cap.read()show_size = [320,240]if not ret:print("Can't receive frame (stream end?). Exiting ...")breakcurrent_time = time.time()# 预处理图像input_tensor = preprocess(frame, input_size)# 进行推理outputs = session.run(None, {input_name: input_tensor})output = outputs[0][0]# 后处理show_image = postprocess(output, frame, input_size, show_size, classes)fps = 1.0 / (current_time - prev_time)prev_time = current_time  # 更新前一帧的时间cv2.putText(show_image, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)# 显示结果cv2.imshow(window_name, show_image)if cv2.waitKey(1) == ord('q'):break# 释放资源cap.release()cv2.destroyAllWindows()if __name__ == '__main__':main()

使用Mediapipe视频检测的代码

行空板+USB摄像头使用Mediapipe进行物体检测的代码代码如下:

import numpy as np
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
import cv2
import timeinput_size = (640,640)
# STEP 1: Import the necessary modules.
base_options = python.BaseOptions(model_asset_path='efficientdet_lite0.tflite')
options = vision.ObjectDetectorOptions(base_options=base_options,score_threshold=0.5)
detector = vision.ObjectDetector.create_from_options(options)# STEP 2: Create an ObjectDetector object.
MARGIN = 10  # pixels
ROW_SIZE = 10  # pixels
FONT_SIZE = 1
FONT_THICKNESS = 1
TEXT_COLOR = (255, 0, 0)  # reddef visualize(image, detection_result) -> np.ndarray:"""Draws bounding boxes on the input image and return it.Args:image: The input RGB image.detection_result: The list of all "Detection" entities to be visualize.Returns:Image with bounding boxes."""for detection in detection_result.detections:# Draw bounding_boxbbox = detection.bounding_boxstart_point = bbox.origin_x, bbox.origin_yend_point = bbox.origin_x + bbox.width, bbox.origin_y + bbox.heightcv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)# Draw label and scorecategory = detection.categories[0]category_name = category.category_nameprobability = round(category.score, 2)result_text = category_name + ' (' + str(probability) + ')'text_location = (MARGIN + bbox.origin_x,MARGIN + ROW_SIZE + bbox.origin_y)cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)return image# STEP 3: Initialize the video capture from the webcam.
cap = cv2.VideoCapture(0)
prev_time = 0while cap.isOpened():ret, frame = cap.read()if not ret:breakframe = cv2.resize(frame, input_size, interpolation=cv2.INTER_NEAREST)# Convert the frame to the format required by MediaPipe.image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)# STEP 4: Detect objects in the frame.detection_result = detector.detect(image)# STEP 5: Process the detection result. In this case, visualize it.annotated_frame = visualize(frame, detection_result)# Calculate and display the frame ratecurrent_time = time.time()fps = 1 / (current_time - prev_time)prev_time = current_timefps_text = f'FPS: {fps:.2f}'print(fps_text)cv2.putText(annotated_frame, fps_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)cv2.imshow('Object Detection', annotated_frame)# Break the loop if the user presses 'q'.if cv2.waitKey(1) & 0xFF == ord('q'):break# Release the resources.
cap.release()
cv2.destroyAllWindows()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • linux搭建深度学习平台
  • MAC配置chromedriver
  • IP学习-Sixday
  • 将语义分割的标签转换为实例分割(yolo)的标签
  • Git-如何将本地项目推到GitHub
  • Linux中的echo命令
  • 【LabVIEW学习篇 - 18】:人机界面交互设计02
  • Chrome extension 谷歌浏览器插件 YouTube 监听地址栏 url 变化
  • 【Qt】Qt和JavaScript使用QWebChannel交互
  • PyTorch库学习之nn.ConvTranspose2d(模块)
  • 【每日一题】LeetCode 1652.拆炸弹(数组、滑动窗口)
  • [数据集][目标检测]轮胎检测数据集VOC+YOLO格式4629张1类别
  • Android架构组件中的MVVM应用
  • 进入docker的命令和docker命令的基础操作
  • python测试开发基础---线程和进程的概念
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • Druid 在有赞的实践
  • export和import的用法总结
  • Java Agent 学习笔记
  • JavaScript设计模式与开发实践系列之策略模式
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Sass Day-01
  • SOFAMosn配置模型
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • 分布式熔断降级平台aegis
  • 高性能JavaScript阅读简记(三)
  • 精彩代码 vue.js
  • 聊聊flink的TableFactory
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 实现菜单下拉伸展折叠效果demo
  • 使用docker-compose进行多节点部署
  • 微信公众号开发小记——5.python微信红包
  • 协程
  • 异常机制详解
  • 因为阿里,他们成了“杭漂”
  • ​【已解决】npm install​卡主不动的情况
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • # 服务治理中间件详解:Spring Cloud与Dubbo
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (13)DroneCAN 适配器节点(一)
  • (3)llvm ir转换过程
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (Note)C++中的继承方式
  • (八十八)VFL语言初步 - 实现布局
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (力扣)1314.矩阵区域和
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (四)库存超卖案例实战——优化redis分布式锁
  • (转载)PyTorch代码规范最佳实践和样式指南
  • 、写入Shellcode到注册表上线
  • .NET CORE 第一节 创建基本的 asp.net core
  • .net core 控制台应用程序读取配置文件app.config
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查