main
FengHua0208 2024-10-15 21:31:44 +08:00
parent c1ca6e3d1b
commit 0e6d01bc77
16 changed files with 144 additions and 4 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,25 @@
## Yolov5s+SORT(卡尔曼+匈牙利)
利用目标跟踪对检测框进行预测的技术对目标检测任务进行加速在yolov5检测的帧之间穿插使用卡尔曼滤波进行目标检测框的预测减少对模型的调用实现目标检测帧率提升的目的。
#### 代码运行环境:
| 开发环境 | Visual Studio 2022 |
| -------- | -------------------- |
| OpenCV | opencv-4.8.0-windows |
| 调用模型 | YOLOv5s 6.0 |
#### 主要代码设置:
main.py中
![image-20241014170146823](C:\Users\FengHua\AppData\Roaming\Typora\typora-user-images\image-20241014170146823.png)
使用到的模型以及检测视频的路径。
![image-20241014170213505](C:\Users\FengHua\AppData\Roaming\Typora\typora-user-images\image-20241014170213505.png)
模型的调用频率设置设置为3表示帧率为3的倍数时进行模型的目标检测其他时候使用预测的目标框。

0
images/detect.mp4 Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -11,7 +11,7 @@ using namespace cv::dnn;
int main()
{
string img_path = "D:/VS/yoloSORT/images/1.mp4";
string img_path = "D:/VS/yoloSORT/images/test.mp4";
string model_path = "D:/VS/yoloSORT/models/yolov5s.onnx";
// 创建Yolov5模型和跟踪器
@ -43,7 +43,7 @@ int main()
}
VideoWriter writer;
writer = VideoWriter("D:/VS/yoloSORT/images/test.mp4", CAP_OPENCV_MJPEG, 20, Size(2560, 1440), true);
writer = VideoWriter("D:/VS/yoloSORT/images/detect.mp4", CAP_OPENCV_MJPEG, 20, Size(2560, 1440), true);
clock_t start, end; //计时器
start = clock();
@ -149,3 +149,118 @@ int main()
return 0;
}
/*
#include "yolo.h"
#include "kalmanboxtracker.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <time.h>
#include "sort.h"
using namespace std;
using namespace cv;
using namespace cv::dnn;
int main()
{
string img_path = "D:/VS/yoloTest/images/1.mp4";
string model_path = "D:/VS/yoloTest/models/yolov5s.onnx";
// 创建Yolov5模型和跟踪器
Yolov5 yolo;
Net net;
Sort mot_tracker = Sort(1, 3, 0.3); // 创建Sort跟踪器
// 读取Yolo模型
if (!yolo.readModel(net, model_path, true)) {
cout << "无法加载Yolo模型" << endl;
return -1;
}
// 生成随机颜色
srand(time(0));
vector<Scalar> colors;
for (int i = 0; i < 80; i++) {
int b = rand() % 256;
int g = rand() % 256;
int r = rand() % 256;
colors.push_back(Scalar(b, g, r));
}
// 读取视频
VideoCapture cap(img_path);
if (!cap.isOpened()) {
cout << "无法打开视频文件" << endl;
return -1;
}
VideoWriter writer;
writer = VideoWriter("D:/VS/yoloTest/images/test.mp4", CAP_OPENCV_MJPEG, 20, Size(2560, 1440), true);
clock_t start, end; //计时器
start = clock();
int num = 0;
Mat frame;
while (cap.read(frame)) {
cap >> frame;
if (frame.empty()) {
cout << "视频已结束" << endl;
break;
}
// 进行目标检测
vector<Output> result;
if (yolo.Detect(frame, net, result)) {
// 跟踪已检测到的目标
vector<Rect> detection_rects; // 存储检测结果的矩形框
for (int i = 0; i < result.size(); i++) {
int x = result[i].box.x;
int y = result[i].box.y;
int w = result[i].box.width;
int h = result[i].box.height;
Rect rect(x, y, w, h);
detection_rects.push_back(rect);
}
// 更新目标跟踪器
vector<vector<float>> trackers = mot_tracker.update(detection_rects);//x,y,w,h,id
// 绘制跟踪结果
for (int i = 0; i < trackers.size(); i++) {
Rect rect(trackers[i][0], trackers[i][1], trackers[i][2] - trackers[i][0], trackers[i][3] - trackers[i][1]);
//cout << "0:" << trackers[i][0] << endl; //跟踪目标的 x 坐标(左上角的 x 坐标)。
//cout << "1:" << trackers[i][1] << endl; //跟踪目标的 y 坐标(左上角的 y 坐标)。
//cout << "2:" << trackers[i][2] << endl; //跟踪目标的宽度。
//cout << "3:" << trackers[i][3] << endl; //跟踪目标的高度。
//cout << "4:" << trackers[i][4] << endl; //跟踪目标的 ID。
int id = static_cast<int>(trackers[i][4]);
//cout << "id:" << id << endl;
//string label = yolo.className[result[id].id] + ":" + to_string(result[id].confidence);
rectangle(frame, rect, Scalar(0, 255, 0), 2);
cout << trackers[i][5] << endl;
putText(frame, "id:" + to_string(int(trackers[i][4])), Point(rect.x, rect.y), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
}
}
imshow("img", frame);
writer << frame;
if (waitKey(10) == 27) {
break; // 退出循环
}
num++;
}
end = clock();
cout << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; //输出时间(单位:s)
cout << "frame num: " << num << endl;
cout << "Speed = " << double(end - start) * 1000 / CLOCKS_PER_SEC / num << "ms" << endl; //输出时间单位m
cap.release();
destroyAllWindows();
return 0;
}
*/

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -76,8 +76,8 @@ D:\VS\yoloSORT\yoloSORT\main.cpp(111,40): warning C4244: [
D:\VS\yoloSORT\yoloSORT\main.cpp(111,40): warning C4244: _Tp=int
D:\VS\yoloSORT\yoloSORT\main.cpp(111,40): warning C4244: ]
正在生成代码
1 of 1116 functions (<0.1%) were compiled, the rest were copied from previous compilation.
0 of 1116 functions ( 0.0%) were compiled, the rest were copied from previous compilation.
0 functions were new in current compilation
1 functions had inline decision re-evaluated but remain unchanged
0 functions had inline decision re-evaluated but remain unchanged
已完成代码的生成
yoloSORT.vcxproj -> D:\VS\yoloSORT\x64\Release\yoloSORT.exe