opencv进阶-YOLOV3模型-实时物体检测
Posted 殇堼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv进阶-YOLOV3模型-实时物体检测相关的知识,希望对你有一定的参考价值。
全部代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
using namespace cv;
using namespace cv::dnn;
void image_detection();
String yolo_cfg = "D:/opencv-4.1.0/models/yolov3/yolov3.cfg";
String yolo_model = "D:/opencv-4.1.0/models/yolov3/yolov3.weights";
int main()
{
image_detection();
}
void image_detection() {
//加载网络模型
Net net = readNetFromDarknet(yolo_cfg, yolo_model);
//net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
net.setPreferableTarget(DNN_TARGET_CPU);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
for (int i = 0; i < outNames.size(); i++) {
printf("output layer name : %s\\n", outNames[i].c_str());
}
vector<string> classNamesVec;
ifstream classNamesFile("D:/opencv-4.1.0/models/yolov3/object_detection_classes_yolov3.txt");
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
classNamesVec.push_back(className);
}
// 加载图像
VideoCapture capture(0);
Mat frame;
while (capture.read(frame))
{
flip(frame, frame, 1);
//imshow("input", frame);
Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false);
net.setInput(inputBlob);
// 输出检测频率和每帧耗时
std::vector<Mat> outs;
net.forward(outs, outNames);
vector<double> layersTimings;
double freq = getTickFrequency() / 1000;
double time = net.getPerfProfile(layersTimings) / freq;
ostringstream ss;
ss << "FPS" << 1000 / time << ";time:" << time << "ms";
putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255),2,8);
// 输出检测框和置信度
vector<Rect> boxes;
vector<int> classIds;
vector<float> confidences;
for (size_t i = 0; i < outs.size(); ++i)
{
// Network produces output blob with a shape NxC where N is a number of
// detected objects and C is a number of classes + 4 where the first 4
// numbers are [center_x, center_y, width, height]
float* data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > 0.5)
{
int centerX = (int)(data[0] * frame.cols);
int centerY = (int)(data[1] * frame.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
vector<int> indices;
NMSBoxes(boxes, confidences, 0.5, 0.2, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
String className = classNamesVec[classIds[idx]];
putText(frame, format("%.2f,%s", confidences, className.c_str()), box.tl(), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2, 8);
rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
}
imshow("YOLOv3-Detections", frame);
char c = waitKey(5);
if (c == 27) { // ESC退出
break;
}
}
capture.release();//释放资源
waitKey(0);
return;
}
reference:
[OpenCV实战]7 使用YOLOv3和OpenCV进行基于深度学习的目标检测
基于OpenCV和YOLOv3深度学习的目标检测
死磕YOLO系列,不会 AI没关系,用OpenCV 调用YOLO 做目标检测
以上是关于opencv进阶-YOLOV3模型-实时物体检测的主要内容,如果未能解决你的问题,请参考以下文章