Android实现红绿灯检测(含Android源码 可实时运行) 您所在的位置:网站首页 红绿灯检测图怎么看 Android实现红绿灯检测(含Android源码 可实时运行)

Android实现红绿灯检测(含Android源码 可实时运行)

2024-07-15 00:35| 来源: 网络整理| 查看: 265

Android实现红绿灯检测(含Android源码 可实时运行)

目录

Android实现红绿灯检测(含Android源码 可实时运行)

1. 前言

2. 红绿灯检测数据集说明

3. 基于YOLOv5的红绿灯检测模型训练

4.红绿灯检测模型Android部署

(1) 将Pytorch模型转换ONNX模型

(2) 将ONNX模型转换为TNN模型

(3) Android端上部署模型

(4) 一些异常错误解决方法

5. 红绿灯检测效果

6.项目源码下载

1. 前言

本篇博客是《深度学习目标检测:YOLOv5实现红绿灯检测(含红绿灯数据集+训练代码)》续作Android篇,主要分享将Python训练后的YOLOv5的红绿灯检测模型移植到Android平台。我们将开发一个简易的、可实时运行的红绿灯检测Android Demo。

考虑到原始YOLOv5的模型计算量比较大,鄙人在YOLOv5s基础上,开发了一个非常轻量级的的红绿灯检测模型yolov5s05_320。从效果来看,Android红绿灯检测模型的检测效果还是可以的,高精度版本YOLOv5s平均精度平均值mAP_0.5=0.93919,而轻量化版本yolov5s05_416平均精度平均值mAP_0.5=0.71944左右。APP在普通Android手机上可以达到实时的检测识别效果,CPU(4线程)约30ms左右,GPU约25ms左右 ,基本满足业务的性能需求。

 先展示一下Android Demo效果:

【Android APP体验】Android实现红绿灯检测APP(可实时运行))

【项目源码下载】 Android实现红绿灯检测(含Android源码 可实时运行)

【尊重原创,转载请注明出处】https://blog.csdn.net/guyuealian/article/details/128240334

更多项目《智能驾驶 车牌检测和识别》系列文章请参考:

智能驾驶 车牌检测和识别(一)《CCPD车牌数据集》:https://blog.csdn.net/guyuealian/article/details/128704181智能驾驶 车牌检测和识别(二)《YOLOv5实现车牌检测(含车牌检测数据集和训练代码)》:https://blog.csdn.net/guyuealian/article/details/128704068智能驾驶 车牌检测和识别(三)《CRNN和LPRNet实现车牌识别(含车牌识别数据集和训练代码)》:https://blog.csdn.net/guyuealian/article/details/128704209智能驾驶 车牌检测和识别(四)《Android实现车牌检测和识别(可实时车牌识别)》:https://blog.csdn.net/guyuealian/article/details/128704242智能驾驶 车牌检测和识别(五)《C++实现车牌检测和识别(可实时车牌识别)》:https://blog.csdn.net/guyuealian/article/details/128704276智能驾驶 红绿灯检测(一)《红绿灯(交通信号灯)数据集》:https://blog.csdn.net/guyuealian/article/details/128222850智能驾驶 红绿灯检测(二)《YOLOv5实现红绿灯检测(含红绿灯数据集+训练代码)》:https://blog.csdn.net/guyuealian/article/details/128240198智能驾驶 红绿灯检测(三)《Android实现红绿灯检测(含Android源码 可实时运行)》:https://blog.csdn.net/guyuealian/article/details/128240334

智能驾驶 车辆检测(一)《UA-DETRAC BITVehicle车辆检测数据集》:https://blog.csdn.net/guyuealian/article/details/127907325

智能驾驶 车辆检测(二)《YOLOv5实现车辆检测(含车辆检测数据集+训练代码)》:https://blog.csdn.net/guyuealian/article/details/128099672

智能驾驶 车辆检测(三)《Android实现车辆检测(含Android源码 可实时运行)》:https://blog.csdn.net/guyuealian/article/details/128190532

2. 红绿灯检测数据集说明

目前收集了约1W+的红绿灯(交通信号灯)检测数据集:Traffic-Lights-Dataset-Domestic+Traffic-Lights-Dataset-Foreign: 

关于红绿灯检测数据集使用说明和下载,详见另一篇博客说明:《红绿灯(交通信号灯)检测数据集》

3. 基于YOLOv5的红绿灯检测模型训练

官方YOLOv5给出了YOLOv5l,YOLOv5m,YOLOv5s等模型。考虑到手机端CPU/GPU性能比较弱鸡,直接部署yolov5s运行速度十分慢。所以本人在yolov5s基础上进行模型轻量化处理,即将yolov5s的模型的channels通道数全部都减少一半,并且模型输入由原来的640×640降低到416×416或者320×320,该轻量化的模型我称之为yolov5s05。从性能来看,yolov5s05比yolov5s快5多倍,而mAP下降了16%(0.93→0.77),对于手机端,这精度勉强可以接受。

下面是yolov5s05和yolov5s的参数量和计算量对比:

模型input-sizeparams(M)GFLOPsmAP0.5yolov5s640×6407.216.50.93919yolov5s05416×4161.71.80.77174yolov5s05320×3201.71.10.71944

yolov5s05和yolov5s训练过程完全一直,仅仅是配置文件不一样而已;碍于篇幅,本篇博客不在赘述,详细训练过程请参考: 《深度学习目标检测:YOLOv5实现红绿灯检测(含红绿灯数据集+训练代码)》

4.红绿灯检测模型Android部署 (1) 将Pytorch模型转换ONNX模型

训练好yolov5s05或者yolov5s模型后,你需要将模型转换为ONNX模型,并使用onnx-simplifier简化网络结构

# 转换yolov5s05模型 python export.py --weights "runs/yolov5s05_320/weights/best.pt" --img-size 320 320 # 转换yolov5s模型 python export.py --weights "runs/yolov5s_640/weights/best.pt" --img-size 640 640

GitHub: https://github.com/daquexian/onnx-simplifier Install:  pip3 install onnx-simplifier 

(2) 将ONNX模型转换为TNN模型

目前CNN模型有多种部署方式,可以采用TNN,MNN,NCNN,以及TensorRT等部署工具,鄙人采用TNN进行Android端上部署:

TNN转换工具:

(1)将ONNX模型转换为TNN模型,请参考TNN官方说明:TNN/onnx2tnn.md at master · Tencent/TNN · GitHub(2)一键转换,懒人必备:一键转换 Caffe, ONNX, TensorFlow 到 NCNN, MNN, Tengine   (可能存在版本问题,这个工具转换的TNN模型可能不兼容,建议还是自己build源码进行转换,2022年9约25日测试可用)

(3) Android端上部署模型

项目实现了Android版本的红绿灯检测Demo,部署框架采用TNN,支持多线程CPU和GPU加速推理,在普通手机上可以实时处理。Android源码核心算法YOLOv5部分均采用C++实现,上层通过JNI接口调用

package com.cv.tnn.model; import android.graphics.Bitmap; public class Detector { static { System.loadLibrary("tnn_wrapper"); } /*** * 初始化模型 * @param model: TNN *.tnnmodel文件文件名(含后缀名) * @param root:模型文件的根目录,放在assets文件夹下 * @param model_type:模型类型 * @param num_thread:开启线程数 * @param useGPU:关键点的置信度,小于值的坐标会置-1 */ public static native void init(String model, String root, int model_type, int num_thread, boolean useGPU); /*** * 检测 * @param bitmap 图像(bitmap),ARGB_8888格式 * @param score_thresh:置信度阈值 * @param iou_thresh: IOU阈值 * @return */ public static native FrameInfo[] detect(Bitmap bitmap, float score_thresh, float iou_thresh); }

如果你想在这个Android Demo部署你自己训练的YOLOv5模型,你可将训练好的Pytorch模型转换ONNX ,再转换成TNN模型,然后把TNN模型代替你模型即可。

(4) 一些异常错误解决方法

TNN推理时出现:Permute param got wrong size

官方YOLOv5:  GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite 

如果你是直接使用官方YOLOv5代码转换TNN模型,部署TNN时会出现这个错误Permute param got wrong size,这是因为TNN最多支持4个维度计算,而YOLOv5在输出时采用了5个维度。你需要修改model/yolo.py文件 

 export.py文件设置model.model[-1].export = True:

..... # Exports if 'torchscript' in include: export_torchscript(model, img, file, optimize) if 'onnx' in include: model.model[-1].export = True # TNN不支持5个维度,修改输出格式 export_onnx(model, img, file, opset, train, dynamic, simplify=simplify) if 'coreml' in include: export_coreml(model, img, file) # Finish print(f'\nExport complete ({time.time() - t:.2f}s)' f"\nResults saved to {colorstr('bold', file.parent.resolve())}" f'\nVisualize with https://netron.app') ..... TNN推理时效果很差,检测框一团麻

 这个问题,大部分是模型参数设置错误,需要根据自己的模型,修改C++推理代码YOLOv5Param模型参数。

struct YOLOv5Param { ModelType model_type; // 模型类型,MODEL_TYPE_TNN,MODEL_TYPE_NCNN等 int input_width; // 模型输入宽度,单位:像素 int input_height; // 模型输入高度,单位:像素 bool use_rgb; // 是否使用RGB作为模型输入(PS:接口固定输入BGR,use_rgb=ture时,预处理将BGR转换为RGB) bool padding; int num_landmarks; // 关键点个数 NetNodes InputNodes; // 输入节点名称 NetNodes OutputNodes; // 输出节点名称 vector anchors; vector class_names; // 类别集合 };

input_width和input_height是模型的输入大小;vector anchors需要对应上,注意Python版本的yolov5s的原始anchor是

anchors: - [10,13, 16,30, 33,23] # P3/8 - [30,61, 62,45, 59,119] # P4/16 - [116,90, 156,198, 373,326] # P5/32

而yolov5s05由于input size由原来640变成320,anchor也需要做对应调整:

anchors: - [ 1,3, 2,5, 3,7 ] - [ 4,9, 9,5, 5,12 ] - [ 7,17, 9,25, 17,38 ]

因此C++版本的yolov5s和yolov5s05的模型参数YOLOv5Param如下设置

//YOLOv5s模型参数 static YOLOv5Param YOLOv5s_640 = {MODEL_TYPE_TNN, 640, 640, true, true, 0, {{{"images", nullptr}}}, //InputNodes {{{"boxes", nullptr}, //OutputNodes {"scores", nullptr}}}, { {"434", 32, {{12, 32}, {17, 47}, {35, 76}}}, {"415", 16, {{6, 17}, {18, 8}, {9, 22}}}, {"output", 8, {{2, 6}, {3, 9}, {5, 12}}}, }, CLASS_NAME }; //YOLOv5s05模型参数 static YOLOv5Param YOLOv5s05_ANCHOR_416 = {MODEL_TYPE_TNN, 416, 416, true, true, 0, {{{"images", nullptr}}}, //InputNodes {{{"boxes", nullptr}, //OutputNodes {"scores", nullptr}}}, { {"434", 32, {{7, 18}, {10, 27}, {18, 45}}}, {"415", 16, {{4, 11}, {5, 13}, {12, 6}}}, {"output", 8, {{2, 4}, {2, 6}, {3, 8}}}, // }, CLASS_NAME }; //YOLOv5s05模型参数 static YOLOv5Param YOLOv5s05_ANCHOR_320 = {MODEL_TYPE_TNN, 320, 320, true, true, 0, {{{"images", nullptr}}}, //InputNodes {{{"boxes", nullptr}, //OutputNodes {"scores", nullptr}}}, { {"434", 32, {{7, 17}, {9, 25}, {17, 38}}}, {"415", 16, {{4, 9}, {9, 5}, {5, 12}}}, {"output", 8, {{1, 3}, {2, 5}, {3, 7}}}, // }, CLASS_NAME }; 运行APP闪退:dlopen failed: library "libomp.so" not found

参考解决方法:解决dlopen failed: library “libomp.so“ not found_PKing666666的博客-CSDN博客_dlopen failed

5. 红绿灯检测效果

【Android APP体验】Android实现红绿灯检测APP(可实时运行))

APP在普通Android手机上可以达到实时的红绿灯检测效果,CPU(4线程)约30ms左右,GPU约25ms左右 ,基本满足业务的性能需求。

6.项目源码下载

 【Android APP体验】Android实现红绿灯检测APP(可实时运行))

 【红绿灯检测Android源码下载】 Android实现红绿灯检测(含Android源码 可实时运行)

整套Android项目源码内容包含:

提供快速版yolov5s05红绿灯检测模型,在普通手机可实时检测识别,CPU(4线程)约30ms左右,GPU约25ms左右提供高精度版本yolov5s红绿灯检测模型,CPU(4线程)约250ms左右,GPU约100ms左右Demo支持图片,视频,摄像头测试所有依赖库都已经配置好,可直接build运行,若运行出现闪退,请参考dlopen failed: library “libomp.so“ not found 解决。

如果你需要红绿灯检测数据集:《红绿灯(交通信号灯)检测数据集》

如果你需要红绿灯训练代码:《YOLOv5实现红绿灯检测(含红绿灯数据集+训练代码)》

更多项目《智能驾驶 车牌检测和识别》系列文章请参考:

智能驾驶 车牌检测和识别(一)《CCPD车牌数据集》:https://blog.csdn.net/guyuealian/article/details/128704181智能驾驶 车牌检测和识别(二)《YOLOv5实现车牌检测(含车牌检测数据集和训练代码)》:https://blog.csdn.net/guyuealian/article/details/128704068智能驾驶 车牌检测和识别(三)《CRNN和LPRNet实现车牌识别(含车牌识别数据集和训练代码)》:https://blog.csdn.net/guyuealian/article/details/128704209智能驾驶 车牌检测和识别(四)《Android实现车牌检测和识别(可实时车牌识别)》:https://blog.csdn.net/guyuealian/article/details/128704242智能驾驶 车牌检测和识别(五)《C++实现车牌检测和识别(可实时车牌识别)》:https://blog.csdn.net/guyuealian/article/details/128704276智能驾驶 红绿灯检测(一)《红绿灯(交通信号灯)数据集》:https://blog.csdn.net/guyuealian/article/details/128222850智能驾驶 红绿灯检测(二)《YOLOv5实现红绿灯检测(含红绿灯数据集+训练代码)》:https://blog.csdn.net/guyuealian/article/details/128240198智能驾驶 红绿灯检测(三)《Android实现红绿灯检测(含Android源码 可实时运行)》:https://blog.csdn.net/guyuealian/article/details/128240334

智能驾驶 车辆检测(一)《UA-DETRAC BITVehicle车辆检测数据集》:https://blog.csdn.net/guyuealian/article/details/127907325

智能驾驶 车辆检测(二)《YOLOv5实现车辆检测(含车辆检测数据集+训练代码)》:https://blog.csdn.net/guyuealian/article/details/128099672

智能驾驶 车辆检测(三)《Android实现车辆检测(含Android源码 可实时运行)》:https://blog.csdn.net/guyuealian/article/details/128190532



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有