两百行C++代码实现yolov5车辆计数部署(通俗易懂版)
创始人
2024-04-21 12:26:56
0

这周用opencv简单实现了一下基于yolov5检测器的单向车辆计数功能,方法是撞线计数,代码很简单一共就两百多行,测试视频是在b站随便下载的。注:该代码只能演示视频demo效果,一些功能未完善,离实际工程应用还有距离。
实现流程:
(1)训练yolov5模型,这里就没有自己训练了,直接使用官方的开源模型yolov5s.pt;
(2)运行yolov5工程下面的export.py,将pt模型转成onnx模型;
(3)编写yolov5部署的C++工程,包括前处理、推理和后处理部分;
(4)读取视频第一帧,用yolov5检测第一帧图像的车辆目标,计算这些检测框的中心点,
(5)读取视频的后续帧,用yolov5检测每帧图像上的车辆目标,计算新目标和上一帧图像中检测框中心点的距离矩阵;
(6)通过距离矩阵确定新旧目标检测框之间的对应关系;
(7)计算对应新旧目标检测框中心点之间的连线,判断和事先设置的虚拟撞线是否相交,若相交则计数加1;
(8)重复(5)-(7)。
实际实现的时候采取的是隔帧判断而不是使用相邻帧,v1的代码实现如下:

#include 
#include 
#include // 常量
const float INPUT_WIDTH = 640.0;
const float INPUT_HEIGHT = 640.0;
const float SCORE_THRESHOLD = 0.5;
const float NMS_THRESHOLD = 0.45;
const float CONFIDENCE_THRESHOLD = 0.45;const std::vector class_name = {
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
"hair drier", "toothbrush" };// 画框函数
void draw_label(cv::Mat& input_image, std::string label, int left, int top)
{int baseLine;cv::Size label_size = cv::getTextSize(label, 0.7, 0.7, 1, &baseLine);top = std::max(top, label_size.height);cv::Point tlc = cv::Point(left, top);cv::Point brc = cv::Point(left , top + label_size.height + baseLine);cv::putText(input_image, label, cv::Point(left, top + label_size.height), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 1);
}// 预处理
std::vector preprocess(cv::Mat& input_image, cv::dnn::Net& net)
{cv::Mat blob;cv::dnn::blobFromImage(input_image, blob, 1. / 255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false);net.setInput(blob);std::vector preprcess_image;net.forward(preprcess_image, net.getUnconnectedOutLayersNames());return preprcess_image;
}// 后处理
std::vector postprocess(std::vector& preprcess_image, cv::Mat& output_image)
{std::vector class_ids;std::vector confidences;std::vector boxes;std::vector boxes_nms;float x_factor = output_image.cols / INPUT_WIDTH;float y_factor = output_image.rows / INPUT_HEIGHT;float* data = (float*)preprcess_image[0].data;const int dimensions = 85;const int rows = 25200;for (int i = 0; i < rows; ++i){float confidence = data[4];if (confidence >= CONFIDENCE_THRESHOLD){float* classes_scores = data + 5;cv::Mat scores(1, class_name.size(), CV_32FC1, classes_scores);cv::Point class_id;double max_class_score;cv::minMaxLoc(scores, 0, &max_class_score, 0, &class_id);if (max_class_score > SCORE_THRESHOLD){confidences.push_back(confidence);class_ids.push_back(class_id.x);float cx = data[0];float cy = data[1];float w = data[2];float h = data[3];int left = int((cx - 0.5 * w) * x_factor);int top = int((cy - 0.5 * h) * y_factor);int width = int(w * x_factor);int height = int(h * y_factor);boxes.push_back(cv::Rect(left, top, width, height));}}data += 85;}std::vector indices;cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, indices);for (size_t i = 0; i < indices.size(); i++){int idx = indices[i];cv::Rect box = boxes[idx];boxes_nms.push_back(box);int left = box.x;int top = box.y;int width = box.width;int height = box.height;cv::rectangle(output_image, cv::Point(left, top), cv::Point(left + width, top + height), cv::Scalar(255, 0, 0), 1);std::string label = cv::format("%.2f", confidences[idx]);label = class_name[class_ids[idx]] + ":" + label;draw_label(output_image, label, left, top);}return boxes_nms;
}std::vector get_center(std::vector detections)
{std::vector detections_center(detections.size());for (size_t i = 0; i < detections.size(); i++){detections_center[i] = cv::Point(detections[i].x + detections[i].width / 2, detections[i].y + detections[i].height / 2);}return detections_center;
}float get_distance(cv::Point p1, cv::Point p2)
{return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}bool is_cross(cv::Point p1, cv::Point p2)
{if (p1.x == p2.x) return false;int y = 500;  //line1: y = 500float k = (p1.y - p2.y) / (p1.x - p2.x);  //float b = p1.y - k * p1.x; //line2: y = kx + bfloat x = (y - b) / k;return (x > std::min(p1.x, p2.x) && x < std::max(p1.x, p2.x));
}int main(int argc, char** argv)
{cv::VideoCapture capture("test.mp4");cv::Mat frame;cv::dnn::Net net = cv::dnn::readNet("yolov5s-f32.onnx");int frame_num = 0;int count = 0;std::vector detections_center_old;std::vector detections_center_new;while(cv::waitKey(1) < 0){capture >> frame;if (frame.empty())break;std::cout << "******************************************************************* frame_num: " << frame_num << std::endl;cv::Mat image = frame.clone();std::vector preprcess_image = preprocess(image, net);std::vector detections = postprocess(preprcess_image, image);if (frame_num == 0){detections_center_old = get_center(detections);std::cout << "detections_center:" << std::endl;for (size_t i = 0; i < detections_center_old.size(); i++){std::cout << detections_center_old[i] << std::endl;}}else if (frame_num % 2 == 0){detections_center_new = get_center(detections);std::cout << "detections_center:" << std::endl;for (size_t i = 0; i < detections_center_new.size(); i++){std::cout << detections_center_new[i] << std::endl;}std::vector> distance_matrix(detections_center_new.size(), std::vector(detections_center_old.size()));std::cout << "distance_matrix:" << std::endl;for (size_t i = 0; i < detections_center_new.size(); i++){for (size_t j = 0; j < detections_center_old.size(); j++){distance_matrix[i][j] = get_distance(detections_center_new[i], detections_center_old[j]); //std::cout << distance_matrix[i][j] << " ";}std::cout << std::endl;}std::cout << "min_index:" << std::endl;std::vector min_indices(detections_center_new.size());for (size_t i = 0; i < detections_center_new.size(); i++){std::vector distance_vector = distance_matrix[i];int min_index = std::min_element(distance_vector.begin(), distance_vector.end()) - distance_vector.begin();min_indices[i] = min_index;std::cout << min_index << " ";}std::cout << std::endl;for (size_t i = 0; i < detections_center_new.size(); i++){cv::Point p1 = detections_center_new[i];cv::Point p2 = detections_center_old[min_indices[i]];std::cout << p1 << " " << p2 << std::endl;if (is_cross(p1, p2)){std::cout << "is_cross" << p1 << " " << p2 << std::endl;count++;}}detections_center_old = detections_center_new;}frame_num++;cv::putText(image, "car num: " + std::to_string(count), cv::Point(20, 50), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 1);cv::line(image, cv::Point(0, 500), cv::Point(1280, 500) , cv::Scalar(0, 0, 255));cv::imshow("output", image);cv::imwrite(std::to_string(frame_num) + ".jpg", image);}capture.release();return 0;
}

在调试中,发现v1的实现存在如下问题:出现新目标的时候,计算新旧检测框的对应关系出现匹配错误,导致计数偏多。因此在v2中设置匹配的距离阈值,并简化了判断检测框中心点连线和撞线是否相交的方法。
v2的代码实现如下:

#include 
#include #define DEBUG// 常量
const float INPUT_WIDTH = 640.0;
const float INPUT_HEIGHT = 640.0;
const float SCORE_THRESHOLD = 0.5;
const float NMS_THRESHOLD = 0.25;
const float CONFIDENCE_THRESHOLD = 0.5;const std::vector class_name = {"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light","fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow","elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee","skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard","tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple","sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch","potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone","microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear","hair drier", "toothbrush" };const int IMAGE_WIDTH = 1280;
const int IMAGE_HEIGHT = 720;
const int LINE_HEIGHT = IMAGE_HEIGHT / 2;//画出检测框和标签
void draw_label(cv::Mat& input_image, std::string label, int left, int top)
{int baseLine;cv::Size label_size = cv::getTextSize(label, 0.7, 0.7, 1, &baseLine);top = std::max(top, label_size.height);cv::Point tlc = cv::Point(left, top);cv::Point brc = cv::Point(left , top + label_size.height + baseLine);cv::putText(input_image, label, cv::Point(left, top + label_size.height), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 1);
}//预处理
std::vector preprocess(cv::Mat& input_image, cv::dnn::Net& net)
{cv::Mat blob;cv::dnn::blobFromImage(input_image, blob, 1. / 255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false);net.setInput(blob);std::vector preprcess_image;net.forward(preprcess_image, net.getUnconnectedOutLayersNames());return preprcess_image;
}//后处理
std::vector postprocess(std::vector& preprcess_image, cv::Mat& output_image)
{std::vector class_ids;std::vector confidences;std::vector boxes;std::vector boxes_nms;float x_factor = output_image.cols / INPUT_WIDTH;float y_factor = output_image.rows / INPUT_HEIGHT;float* data = (float*)preprcess_image[0].data;const int dimensions = 85;const int rows = 25200;for (int i = 0; i < rows; ++i){float confidence = data[4];if (confidence >= CONFIDENCE_THRESHOLD){float* classes_scores = data + 5;cv::Mat scores(1, class_name.size(), CV_32FC1, classes_scores);cv::Point class_id;double max_class_score;cv::minMaxLoc(scores, 0, &max_class_score, 0, &class_id);if (max_class_score > SCORE_THRESHOLD){confidences.push_back(confidence);class_ids.push_back(class_id.x);float cx = data[0];float cy = data[1];float w = data[2];float h = data[3];int left = int((cx - 0.5 * w) * x_factor);int top = int((cy - 0.5 * h) * y_factor);int width = int(w * x_factor);int height = int(h * y_factor);boxes.push_back(cv::Rect(left, top, width, height));}}data += 85;}std::vector indices;cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, indices);for (size_t i = 0; i < indices.size(); i++){int idx = indices[i];cv::Rect box = boxes[idx];boxes_nms.push_back(box);int left = box.x;int top = box.y;int width = box.width;int height = box.height;cv::rectangle(output_image, cv::Point(left, top), cv::Point(left + width, top + height), cv::Scalar(255, 0, 0), 1);std::string label = cv::format("%.2f", confidences[idx]);//label = class_name[class_ids[idx]] + ":" + label;label = "car";draw_label(output_image, label, left, top);}return boxes_nms;
}//计算检测框的中心
std::vector get_center(std::vector detections)
{std::vector detections_center(detections.size());for (size_t i = 0; i < detections.size(); i++){detections_center[i] = cv::Point(detections[i].x + detections[i].width / 2, detections[i].y + detections[i].height / 2);}return detections_center;
}//计算两点间距离
float get_distance(cv::Point p1, cv::Point p2)
{return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}//判断连接相邻两帧对应检测框中心的线段是否与红线相交
bool is_cross(cv::Point p1, cv::Point p2)
{return (p1.y <= LINE_HEIGHT && p2.y > LINE_HEIGHT) || (p1.y > LINE_HEIGHT && p2.y <= LINE_HEIGHT);
}int main(int argc, char** argv)
{cv::VideoCapture capture("test.mp4");cv::Mat frame;cv::dnn::Net net = cv::dnn::readNet("yolov5s-f32.onnx");int frame_num = 0;int count = 0;std::vector detections_center_old;std::vector detections_center_new;while(cv::waitKey(1) < 0){capture >> frame;if (frame.empty())break;std::cout << "******************************************************************* frame_num: " << frame_num << std::endl;cv::Mat image = frame.clone();std::vector preprcess_image = preprocess(image, net);std::vector detections = postprocess(preprcess_image, image);if (frame_num == 0){detections_center_old = get_center(detections);#ifdef DEBUGstd::cout << "detections_center:" << std::endl;for (size_t i = 0; i < detections_center_old.size(); i++){std::cout << detections_center_old[i] << std::endl;}
#endif // DEBUG}else if (frame_num % 2 == 0){detections_center_new = get_center(detections);#ifdef DEBUGstd::cout << "detections_center:" << std::endl;for (size_t i = 0; i < detections_center_new.size(); i++){std::cout << detections_center_new[i] << std::endl;}
#endif // DEBUGstd::vector> distance_matrix(detections_center_new.size(), std::vector(detections_center_old.size())); //距离矩阵for (size_t i = 0; i < detections_center_new.size(); i++){for (size_t j = 0; j < detections_center_old.size(); j++){distance_matrix[i][j] = get_distance(detections_center_new[i], detections_center_old[j]); }}#ifdef DEBUGstd::cout << "min_index:" << std::endl;
#endif // DEBUGstd::vector min_indices(detections_center_new.size());for (size_t i = 0; i < detections_center_new.size(); i++){std::vector distance_vector = distance_matrix[i];float min_val = *std::min_element(distance_vector.begin(), distance_vector.end());int min_index = -1;if (min_val < LINE_HEIGHT / 5)min_index = std::min_element(distance_vector.begin(), distance_vector.end()) - distance_vector.begin();min_indices[i] = min_index;
#ifdef DEBUGstd::cout << min_index << " ";
#endif // DEBUG}std::cout << std::endl;for (size_t i = 0; i < detections_center_new.size(); i++){if (min_indices[i] < 0)continue;cv::Point p1 = detections_center_new[i];cv::Point p2 = detections_center_old[min_indices[i]];#ifdef DEBUGstd::cout << p1 << " " << p2 << std::endl;
#endif // DEBUGif (is_cross(p1, p2)){
#ifdef DEBUGstd::cout << "is_cross" << p1 << " " << p2 << std::endl;
#endif // DEBUGcount++;}}detections_center_old = detections_center_new;}cv::putText(image, "car num: " + std::to_string(count), cv::Point(20, 50), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 1);cv::line(image, cv::Point(0, LINE_HEIGHT), cv::Point(IMAGE_WIDTH, LINE_HEIGHT), cv::Scalar(0, 0, 255));cv::imshow("output", image);#ifdef DEBUGif (frame_num % 2 == 0)cv::imwrite(std::to_string(frame_num) + ".jpg", image);
#endif // DEBUGframe_num++;}capture.release();return 0;
}

检测效果实现如下,效果还是可以的。完整视频中有一次计数异常,是因为检测器不准导致车辆检测框位置漂移,可以后续优化。注:由于官方提供的coco80类的开源权重文件用于车辆检测效果不是很好,LZ把检测出的类别直接固定为car,实际应自己重新训练一个车辆检测的模型。
在这里插入图片描述

代码、测试视频和转好的权重文件放在下载链接:点击跳转

相关内容

热门资讯

鸿蒙怎样还原安卓系统,系统切换... 你有没有想过,鸿蒙系统竟然能还原安卓系统?这听起来是不是有点像魔法一样神奇?没错,今天就要带你一探究...
电脑安卓转苹果系统,系统迁移攻... 你有没有想过,有一天你的安卓手机突然变成了苹果的忠实粉丝,想要跳槽到iOS的阵营呢?这可不是什么天方...
安卓xp系统下载地址,轻松获取... 你有没有想过,手机系统也能穿越时空?没错,今天我要给你揭秘的就是这样一个神奇的存在——安卓XP系统。...
安卓系统怎么清理相册,安卓系统... 手机里的相册是不是越来越臃肿了?每次打开都感觉像是在翻山越岭,找一张照片都要费老鼻子劲。别急,今天就...
安卓系统安装ios转移,轻松实... 你有没有想过,手机系统之间的转换竟然也能如此神奇?没错,今天就要来聊聊安卓系统安装iOS转移这个话题...
安卓系统与ios系统的优势,系... 你有没有想过,为什么你的手机里装的是安卓系统而不是苹果的iOS系统呢?或者反过来,为什么你的朋友用的...
安卓系统游戏如何升级,轻松实现... 亲爱的安卓玩家们,你是否也和我一样,对安卓系统游戏升级这件事充满了好奇和期待呢?每次游戏更新,都仿佛...
安卓系统蛋仔派对,安卓系统下的... 你有没有发现,最近你的手机里多了一个超级好玩的游戏?没错,就是安卓系统上的“蛋仔派对”!这款游戏可是...
坚果3安卓原生系统,深度体验原... 你有没有听说过坚果3这款手机?它可是最近在数码圈里火得一塌糊涂呢!今天,我就要来给你详细介绍一下这款...
安卓子系统点不开,排查与解决指... 最近是不是你也遇到了安卓子系统点不开的烦恼?这可真是让人头疼啊!别急,今天就来给你详细解析一下这个问...
安卓系统经常误删文件,如何有效... 你有没有遇到过这种情况?手机里的文件突然不见了,找来找去,怎么也找不到。别急,这可能是安卓系统的小调...
安卓51系统如何破解,轻松解锁... 安卓51系统如何破解——探索未知的技术边界在数字化时代,手机已经成为我们生活中不可或缺的一部分。而安...
安卓系统怎么换回主题,安卓系统... 亲爱的手机控们,你是不是也和我一样,对安卓系统的主题换换换乐此不疲呢?不过,有时候换着换着,突然发现...
黑莓安卓系统 太垃圾,令人失望... 你有没有用过黑莓的安卓系统?别告诉我你没有,因为现在这个系统真的是太垃圾了!是的,你没听错,就是那个...
修改安卓系统权限代码,安卓系统... 你有没有想过,你的安卓手机里那些神秘的系统权限代码?它们就像隐藏在手机里的秘密通道,有时候让你觉得既...
虚拟大师安卓系统教程,教程详解... 你有没有想过,手机里的世界可以变得更加神奇?今天,就让我带你一起探索虚拟大师安卓系统的奥秘吧!想象你...
基于安卓系统个人博客,轻松构建... 你有没有想过,在这个信息爆炸的时代,拥有一片属于自己的网络小天地是多么酷的事情啊!想象每天都能在这里...
安卓怎么传到苹果系统,从安卓到... 你是不是也有过这样的烦恼:手机里存了好多好用的安卓应用,可是一换到苹果系统,就发现这些宝贝们都不见了...
安卓改系统字体app,安卓系统... 你有没有想过,手机上的字体也能变得个性十足?没错,就是那个安卓改系统字体app,它可是让手机界面焕然...
安卓系统重启密码错误,破解与预... 手机突然重启了,屏幕上竟然出现了密码输入的界面!这可怎么办?别急,让我来帮你一步步解决这个安卓系统重...