如何5分钟用 lite.ai.toolkit 跑通 YOLOv5 目标检测?Linux 环境配置与编译完整指南
【免费下载链接】lite.ai.toolkitA lite C++ AI toolkit: 100+ models with MNN, ORT and TRT, including Det, Seg, Stable-Diffusion, Face-Fusion.项目地址: https://gitcode.com/gh_mirrors/lit/lite.ai.toolkit
lite.ai.toolkit 是一个轻量的 C++ AI 工具箱,内置 100+ 视觉模型的开箱即用推理实现。本文手把手带你完成 Linux 环境配置与编译,5 分钟跑通YOLOv5 目标检测:从克隆仓库、一键编译,到运行示例并得到带检测框的结果图,全程无需手动安装 OpenCV 和 ONNXRuntime。
为什么选 lite.ai.toolkit 跑 YOLOv5?🎯
| 优势 | 说明 |
|---|---|
| 统一 API | 一行new lite::cv::detection::YoloV5(...)即可检测,前处理/NMS 全封装 |
| 依赖极少 | 默认只需 OpenCV + ONNXRuntime,构建时自动下载,无需手动编译 |
| 多后端切换 | 同一模型可跑 ONNXRuntime / MNN / NCNN / TNN / TensorRT |
| 官方示例齐全 | 自带 YOLOv5 检测示例,编译后直接可执行 |
运行前的 Linux 环境检查 ✅
| 项目 | 要求 |
|---|---|
| 系统 | Linux(Ubuntu 20.04 LTS 已验证),暂不支持 Windows / macOS |
| 工具链 | cmake、gcc/g++(C++17) |
| 其他 | 无需预装 OpenCV 或 ONNXRuntime,首次构建会自动下载到third_party/目录 |
💡 如果你的机器已装好 cmake 和 gcc,就可以直接进入下一步,这也是"5 分钟"的关键。
一键编译:克隆仓库并运行 build.sh
只需 3 条命令完成克隆与编译:
git clone https://gitcode.com/gh_mirrors/lit/lite.ai.toolkit cd lite.ai.toolkit sh ./build.shbuild.sh 会自动完成以下工作:
- 创建
build/目录并执行cmake(MinSizeRel 模式,-DENABLE_TEST=ON构建全部示例); - 自动下载OpenCV 4.9.0与ONNXRuntime 1.17.1到
third_party/(见cmake/opencv.cmake、cmake/onnxruntime.cmake); make -j8编译并make install到build/install/,示例可执行文件输出到build/install/bin/。
一键运行 YOLOv5 目标检测示例 🚀
编译完成后,示例程序lite_yolov5已经就位,直接运行:
cd build/install/bin ./lite_yolov5它会自动读取仓库自带的模型与测试图:
- 模型:
examples/hub/onnx/cv/yolov5s.onnx - 测试图:
examples/lite/resources/test_lite_yolov5_1.jpg - 结果输出:
examples/logs/test_lite_yolov5_1.jpg
输入图像(原始测试图):
检测结果(YOLOv5 目标检测输出,自动画出检测框与置信度):
控制台会打印输入输出张量维度(如1x3x640x640、pred 1x25200x85)与检测到的框数量——看到Detected Boxes Num就说明 YOLOv5 目标检测已经跑通了 🎉。
示例源码见examples/lite/cv/test_lite_yolov5.cpp,核心逻辑只有 6 行:
auto *yolov5 = new lite::cv::detection::YoloV5(onnx_path); // 加载 ONNX 模型 cv::Mat img_bgr = cv::imread(test_img_path); std::vector<lite::types::Boxf> boxes; yolov5->detect(img_bgr, boxes); // 目标检测 lite::utils::draw_boxes_inplace(img_bgr, boxes); // 画框 cv::imwrite(save_img_path, img_bgr);想集成到自己的工程,参考CMakeLists.txt中的 Quick Setup:find_package(lite.ai.toolkit)后链接${lite.ai.toolkit_LIBS}即可,详见项目根目录 README 说明。
如何切换 MNN / NCNN / TNN / TensorRT 推理引擎 ⚡
同一个 YOLOv5 模型,换一行类名即可切换后端(各后端源码位于lite/mnn/cv/、lite/ncnn/cv/、lite/tnn/cv/、lite/trt/cv/):
| 后端 | 检测器类 | 模型格式 |
|---|---|---|
| ONNXRuntime(默认) | lite::cv::detection::YoloV5 | .onnx |
| MNN | lite::mnn::cv::detection::YoloV5 | .mnn |
| NCNN | lite::ncnn::cv::detection::YoloV5 | .param+.bin |
| TNN | lite::tnn::cv::detection::YoloV5 | .tnnproto+.tnnmodel |
| TensorRT | lite::trt::cv::detection::YOLOV5 | .engine |
🚀 想用 NVIDIA GPU 加速?执行
sh ./build.sh tensorrt重新编译(需 TensorRT 10.x + CUDA 12.x),再用trtexec把 ONNX 转为 engine:trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine。安装步骤见docs/tensorrt/tensorrt-linux-x86_64.zh.md。
常见编译与运行问题排查 🛠
- 运行示例报找不到
.so:build/install/下已随包安装好三方库;若自行编译示例,记得把build/install/lib、build/install/third_party/*/lib加入LD_LIBRARY_PATH。 - Windows / macOS 无法构建:
>=0.2.0版本目前仅支持 Linux(见根目录CMakeLists.txt中的平台检查)。 - 检测框为空:检查模型文件
yolov5s.onnx是否存在、输入图像是否为彩色 BGR 格式。 - 想了解模型是怎么转 ONNX 的:阅读
docs/ort/ort_yolov5.zh.md,里面完整记录了 YOLOv5 → ONNX 转换与 C++ 推理实现原理。
关键文件速查表 📚
| 路径 | 作用 |
|---|---|
examples/lite/cv/test_lite_yolov5.cpp | YOLOv5 目标检测多后端示例源码 |
lite/ort/cv/yolov5.cpp/yolov5.h | ONNXRuntime 版 YOLOv5 检测器实现 |
examples/hub/onnx/cv/yolov5s.onnx | 仓库自带的 YOLOv5s ONNX 模型 |
examples/lite/resources/test_lite_yolov5_1.jpg | 检测测试图 |
build.sh | 一键构建脚本(默认 / tensorrt 两种模式) |
跑通 YOLOv5 只是开始——同一套写法,你可以继续体验 YOLOX、NanoDet、人脸检测、抠图甚至 Stable Diffusion,全部示例都在examples/lite/cv/目录下等你探索 👀
【免费下载链接】lite.ai.toolkitA lite C++ AI toolkit: 100+ models with MNN, ORT and TRT, including Det, Seg, Stable-Diffusion, Face-Fusion.项目地址: https://gitcode.com/gh_mirrors/lit/lite.ai.toolkit
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考