本文參考github的一個(gè)項(xiàng)目ObjectDetection-YOLO方篮,代碼都是參考上面的兽肤,作者環(huán)境是linux叉讥,稍加修改后捶障,在此給出一個(gè)在Win10底下可以運(yùn)行的簡(jiǎn)單的demo是鬼,先說一下運(yùn)行環(huán)境
- 需要下載yolov3.weights權(quán)重文件肤舞、yolov3.cfg網(wǎng)絡(luò)構(gòu)建文件、coco.names均蜜、xxx.jpg李剖、xxx.mp4文件以及其他的object_detection_yolo.cpp、object_detection_yolo.py等文件囤耳。
github下載鏈接
網(wǎng)盤下載鏈接(推薦) 密碼:gfg1 - 配好opencv環(huán)境篙顺,網(wǎng)上教程很多不再贅述。
- 運(yùn)行環(huán)境 vs2015+opencv4.0+win10
代碼如下
#include <fstream>
#include <sstream>
#include <iostream>
#include <io.h>
#include<opencv2/core/types_c.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
float confThreshold = 0.5; // Confidence threshold
float nmsThreshold = 0.4; // Non-maximum suppression threshold
int inpWidth = 416; // Width of network's input image
int inpHeight = 416; // Height of network's input image
vector<string> classes;
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& out);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
int main(int argc, char** argv)
{
String name_file = "coco.names";
String model_def = "yolov3.cfg";
String model_weights = "yolov3.weights";
string imgname = "person";
string img_path = imgname.append(".jpg");
//read names
ifstream ifs(name_file.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
//inti model
Net net = readNetFromDarknet(model_def, model_weights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
//讀圖片
#ifdef Image
//read image and forward
Mat frame, blob;
if ((_access(img_path.c_str(), 0)) == -1)
{
cerr << "file:" << img_path.c_str() << "not exist" << endl;
return -1;
}
frame = imread(img_path);
// Create a 4D blob from a frame.
blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
// vector<Mat> mat_blob;
// imagesFromBlob(blob, mat_blob);
//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);
// 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));
imwrite(imgname.append("_label").append(".jpg"), frame);
imshow("res", frame);
waitKey(0);
#endif
//讀視頻
#ifndef Image
Mat frame, blob;
VideoCapture cap;
VideoWriter video;
string outputFile = "yolo_out_cpp.avi";
string video_path = "run.mp4";
int k = 0;
cap.open(video_path);
if (!cap.isOpened())//如果視頻不能正常打開則返回
return 0;
static const string kWinName = "Deep learning object detection in OpenCV";
namedWindow(kWinName, WINDOW_NORMAL);
video.open(outputFile, VideoWriter::fourcc('M', 'J', 'P', 'G'), 28, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)));
// Process frames.
while (1)
{
// get frame from the video
cap >> frame;
// Stop the program if reached end of video
if (frame.empty()) {
cout << "Done processing !!!" << endl;
cout << "Output file is stored as " << outputFile << endl;
waitKey(3000);
break;
}
cout << "處理幀數(shù):" << k << endl;
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);
// 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));
// Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
video.write(detectedFrame);
imshow(kWinName, frame);
k++;
}
cap.release();
#endif // !Image
return 0;
}
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net)
{
static vector<String> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector<String> layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
return names;
}
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence
string label = format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
//Display the label at the top of the bounding box
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
}
// Remove the bounding boxes with low confidence using non-maxima suppression
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 = 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));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame);
}
}
總結(jié):
-
處理圖片簡(jiǎn)單一點(diǎn)充择,處理視頻折騰了好久德玫,一開始配好環(huán)境控制臺(tái)一直黑屏,后來知道其實(shí)就是速度慢點(diǎn)椎麦,示例視頻run.mp4大約5秒宰僧,幀數(shù)約為150張,最后在程序中加了一個(gè)處理幀數(shù)計(jì)數(shù)器K才能看出观挎,以下為運(yùn)行結(jié)果圖
-
圖片處理結(jié)果圖
- 小伙伴們有什么圖要試驗(yàn)的只要把路徑改一下應(yīng)該就能跑起來琴儿,有什么問題可以在評(píng)論區(qū)交流,
PS:簡(jiǎn)書好像不能傳視頻键兜,貼一張?zhí)幚砗玫囊曨l截圖
PS-2:已經(jīng)把視頻上傳至b站, 傳送門