
【简介】
在C++中实现YOLOv12的目标检测与ByteTrack的多目标追踪是一个相对复杂的过程,涉及到深度学习、计算机视觉和实时数据处理等多个领域。下面我将简单介绍这两个技术,并概述如何在C++中实现它们。
YOLOv12(You Only Look Once,版本12)是一种实时目标检测算法,它通过在单个网络中同时预测所有目标的位置和类别来实现高效的目标检测。YOLOv12在速度和精度之间取得了很好的平衡,使其成为许多实时应用的首选方法。
ByteTrack是一种多目标追踪算法,它结合了目标检测和目标追踪两个步骤。ByteTrack使用目标检测算法(如YOLOv9)来识别视频帧中的目标,并使用追踪算法来跟踪这些目标在连续帧之间的运动。ByteTrack通过关联相邻帧中的目标来实现多目标追踪,从而可以准确地跟踪多个目标的运动轨迹。
在C++中实现YOLOv12和ByteTrack的结合,需要以下几个步骤:
需要注意的是,实现这一过程需要一定的计算机视觉和深度学习基础,以及对C++编程的熟悉。此外,由于YOLOv12和ByteTrack都是比较新的技术,因此可能需要使用较新的深度学习框架和库来支持。
总的来说,在C++中实现YOLOv12和ByteTrack的多目标追踪是一个具有挑战性的任务,但它为实时目标检测和追踪提供了强大的工具。通过不断学习和实践,你可以逐渐掌握这些技术,并将其应用于各种实际应用中。
【效果展示】

【运行步骤】
需要提前准备好环境: windows x64一台 vs2019或者vs2022 cmake==3.30.1 onnxruntime==1.16.3(GPU的话需要安装cuda和下载对应版本库) opencv==4.9.0 vscode
第一步:首先安装好opencv4.9.0和onnxruntime1.16.3以及vs2019或者vs2022以及cmake=3.30.1 第二步:用vscode打开CMakeLists.txt修改opencv和onnxuntime路径 第三步:build生成exe,yolov12-onnxruntime-cplus\build\Release文件夹exe拷贝到yolov12-onnxruntime-cplus\data文件夹,替换源文件即可。 第四步:双击运行exe,即可查看效果
【调用代码】
#include <iostream>
#include <opencv2/opencv.hpp>
#include "YOLO12.hpp"
#include "cmdline.h"
#include "utils_yolo.h"
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
const float confThreshold = 0.3f;
const float iouThreshold = 0.4f;
bool isGPU =false;
const std::string labelsPath = "coco.names";
const std::string modelPath = "yolov12n.onnx";
// Initialize the YOLO detector
YOLO12Detector detector(modelPath, labelsPath, isGPU);
std::cout << "Model was initialized." << std::endl;
std::vector<Detection> result;
cv::VideoCapture cap("car.mp4");
// Get video properties
int img_w = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
int img_h = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
int fps = static_cast<int>(cap.get(cv::CAP_PROP_FPS));
std::cout << "Video properties: " << img_w << "x" << img_h << ", " << fps << " FPS" << std::endl;
long nFrame = static_cast<long>(cap.get(cv::CAP_PROP_FRAME_COUNT));
if (!cap.isOpened())
{
std::cout << "open capture failured!" << std::endl;
return -1;
}
Mat frame;
BYTETracker tracker(fps, 30);
int num_frames = 0;
int keyvalue = 0;
int total_ms = 1;
while (true)
{
cap.read(frame);
if (frame.empty())
{
std::cout << "read to end" << std::endl;
break;
}
num_frames++;
auto start = std::chrono::system_clock::now();
result.clear();
// Detect objects in the frame
result = detector.detect(frame);
// Draw bounding boxes on the frame
detector.drawBoundingBoxMask(frame, result); // Uncomment for mask drawing
std::vector<STrack> output_stracks = tracker.update(result);
auto end = chrono::system_clock::now();
total_ms = total_ms + std::chrono::duration_cast<chrono::microseconds>(end - start).count();
for (int i = 0; i < output_stracks.size(); i++)
{
vector<float> tlwh = output_stracks[i].tlwh;
bool vertical = tlwh[2] / tlwh[3] > 1.6;
if (tlwh[2] * tlwh[3] > 20 && !vertical)
{
Scalar s = tracker.get_color(output_stracks[i].track_id);
putText(frame, format("%d", output_stracks[i].track_id), Point(tlwh[0], tlwh[1] - 5),
0, 0.6, Scalar(0, 0, 255), 2, LINE_AA);
cv::rectangle(frame, Rect(tlwh[0], tlwh[1], tlwh[2], tlwh[3]), s, 2);
}
}
cv::putText(frame, format("frame: %d fps: %d num: %d", num_frames, num_frames * 1000000 / total_ms, (int)output_stracks.size()),
Point(0, 30), 0, 0.6, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
cv::imshow("demo", frame);
keyvalue =cv:: waitKey(1);
if (keyvalue == 113 || keyvalue == 81)
{
break;
}
}
cap.release();
return 0;
}演示视频:[cmake]C++使用yolov12结合bytetrack实现目标追踪演示_哔哩哔哩_bilibili