#include "pch.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <time.h>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
;
using namespace cv;
using namespace dnn;
using namespace std;
// Initialize the parameters 初始参数
// Confidence threshold 置信度阈值
float confThreshold = 0.5;
// Non-maximum suppression threshold 非极大性抑制阈值
float nmsThreshold = 0.4;
//检测图像宽高
int inpWidth = 416;
int inpHeight = 416;
//类别参数
vector<string> classes;
// Remove the bounding boxes with low confidence using non-maxima suppression
// 基于非极大性抑制去除低置信度的检测框
void postprocess(Mat& frame, const vector<Mat>& out);
// 画预测框
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// 提取输出输出层
vector<String> getOutputsNames(const Net& net);
int main()
{
// Give the configuration and weight files for the model 模型参数文件
String modelConfiguration = "./model/yolov3.cfg";
String modelWeights = "./model/yolov3.weights";
// Load names of classes 读取分类类名
string classesFile = "./model/coco.names";
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line))
{
classes.push_back(line);
}
// Load the network 导入网络
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
//仅仅使用CPU
net.setPreferableTarget(DNN_TARGET_CPU);
// Open a video file or an image file or a camera stream.
string str, outputFile;
Mat blob;
clock_t start, finish;
//读图
Mat frame = imread("bird.jpg");
//根据需求决定是不是重置图像
//resize(frame, frame, Size(300, 300));
start = clock();
// Create a 4D blob from a frame. 创建神经网络输入图像
blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
//Sets the input to the network 设置输出
net.setInput(blob);
// Runs the forward pass to get output of the output layers 获取输出层结果
vector<Mat> outs;
net.forward(outs, getOutputsNames(net));
// Remove the bounding boxes with low confidence
postprocess(frame, outs);
finish = clock();
cout << "time is " << double(finish - start) / CLOCKS_PER_SEC << endl;
// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
//输出前向传播的时间
vector<double> layersTimes;
double freq = getTickFrequency() / 1000;
double t = net.getPerfProfile(layersTimes) / freq;
string label = format("Inference time for a frame : %.2f ms", t);
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
imshow("result", frame);
//保存图像
imwrite("result.jpg", frame);
waitKey(0);
return 0;
}
/**
* @brief Remove the bounding boxes with low confidence using non-maxima suppression 基于非极大性抑制去除边框
*
* @param frame 视频图像
* @param outs 输出层结果
*/
void postprocess(Mat& frame, const vector<Mat>& outs)
{
//输出类
vector<int> classIds;
//置信度
vector<float> confidences;
vector<Rect> boxes;
//遍历所有的输出层
for (size_t i = 0; i < outs.size(); ++i)
{
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class
// with the highest score for the box.
//扫描所有来自网络的边界框输出,只保留具有高置信度分数的边界框。将框的类标签指定为框得分最高的类。
//读取框
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;
// Get the value and location of the maximum score 获取置信度和位置参数
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
//如果大于置信度阈值
if (confidence > confThreshold)
{
//获取坐标
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 = cen�
落痕的寒假
- 粉丝: 2039
- 资源: 24
最新资源
- IMG_20241115_051050812.jpg
- 基于javaweb的网上拍卖系统,采用Spring + SpringMvc+Mysql + Hibernate+ JSP技术
- polygon-mumbai
- Chrome代理 switchyOmega
- GVC-全球价值链参与地位指数,基于ICIO表,(Wang等 2017a)计算方法
- 易语言ADS指纹浏览器管理工具
- 易语言奇易模块5.3.6
- cad定制家具平面图工具-(FG)门板覆盖柜体
- asp.net 原生js代码及HTML实现多文件分片上传功能(自定义上传文件大小、文件上传类型)
- whl@pip install pyaudio ERROR: Failed building wheel for pyaudio
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈