2017-12-01 06:14:40 +08:00
|
|
|
// Brief Sample of using OpenCV dnn module in real time with device capture, video and image.
|
|
|
|
// VIDEO DEMO: https://www.youtube.com/watch?v=NHtRlndE2cg
|
|
|
|
|
2017-09-25 04:34:08 +08:00
|
|
|
#include <opencv2/dnn.hpp>
|
|
|
|
#include <opencv2/dnn/shape_utils.hpp>
|
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2017-12-01 06:14:40 +08:00
|
|
|
|
2017-09-25 04:34:08 +08:00
|
|
|
using namespace std;
|
2017-12-01 06:14:40 +08:00
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::dnn;
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-12-01 06:14:40 +08:00
|
|
|
static const char* about =
|
|
|
|
"This sample uses You only look once (YOLO)-Detector (https://arxiv.org/abs/1612.08242) to detect objects on camera/video/image.\n"
|
|
|
|
"Models can be downloaded here: https://pjreddie.com/darknet/yolo/\n"
|
|
|
|
"Default network is 416x416.\n"
|
|
|
|
"Class names can be downloaded here: https://github.com/pjreddie/darknet/tree/master/data\n";
|
|
|
|
|
|
|
|
static const char* params =
|
|
|
|
"{ help | false | print usage }"
|
|
|
|
"{ cfg | | model configuration }"
|
|
|
|
"{ model | | model weights }"
|
|
|
|
"{ camera_device | 0 | camera device number}"
|
|
|
|
"{ source | | video or image for detection}"
|
2018-01-09 03:30:28 +08:00
|
|
|
"{ save | | file name output}"
|
|
|
|
"{ fps | 3 | frame per second }"
|
2018-01-02 22:26:29 +08:00
|
|
|
"{ style | box | box or line style draw }"
|
2017-12-01 06:14:40 +08:00
|
|
|
"{ min_confidence | 0.24 | min confidence }"
|
|
|
|
"{ class_names | | File with class names, [PATH-TO-DARKNET]/data/coco.names }";
|
2017-09-25 04:34:08 +08:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
CommandLineParser parser(argc, argv, params);
|
2017-09-25 04:34:08 +08:00
|
|
|
|
|
|
|
if (parser.get<bool>("help"))
|
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
cout << about << endl;
|
2017-09-25 04:34:08 +08:00
|
|
|
parser.printMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
String modelConfiguration = parser.get<String>("cfg");
|
|
|
|
String modelBinary = parser.get<String>("model");
|
2017-09-25 04:34:08 +08:00
|
|
|
|
|
|
|
//! [Initialize network]
|
|
|
|
dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
|
|
|
|
//! [Initialize network]
|
|
|
|
|
|
|
|
if (net.empty())
|
|
|
|
{
|
|
|
|
cerr << "Can't load network by using the following files: " << endl;
|
|
|
|
cerr << "cfg-file: " << modelConfiguration << endl;
|
|
|
|
cerr << "weights-file: " << modelBinary << endl;
|
|
|
|
cerr << "Models can be downloaded here:" << endl;
|
|
|
|
cerr << "https://pjreddie.com/darknet/yolo/" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
VideoCapture cap;
|
2018-01-09 03:30:28 +08:00
|
|
|
VideoWriter writer;
|
|
|
|
int codec = CV_FOURCC('M', 'J', 'P', 'G');
|
|
|
|
double fps = parser.get<float>("fps");
|
2017-12-01 06:14:40 +08:00
|
|
|
if (parser.get<String>("source").empty())
|
2017-10-26 04:02:35 +08:00
|
|
|
{
|
|
|
|
int cameraDevice = parser.get<int>("camera_device");
|
|
|
|
cap = VideoCapture(cameraDevice);
|
|
|
|
if(!cap.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Couldn't find camera: " << cameraDevice << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-01 06:14:40 +08:00
|
|
|
cap.open(parser.get<String>("source"));
|
2017-10-26 04:02:35 +08:00
|
|
|
if(!cap.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Couldn't open image or video: " << parser.get<String>("video") << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:30:28 +08:00
|
|
|
if(!parser.get<String>("save").empty())
|
|
|
|
{
|
|
|
|
writer.open(parser.get<String>("save"), codec, fps, Size((int)cap.get(CAP_PROP_FRAME_WIDTH),(int)cap.get(CAP_PROP_FRAME_HEIGHT)), 1);
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:26:29 +08:00
|
|
|
vector<String> classNamesVec;
|
2017-10-26 04:02:35 +08:00
|
|
|
ifstream classNamesFile(parser.get<String>("class_names").c_str());
|
|
|
|
if (classNamesFile.is_open())
|
|
|
|
{
|
|
|
|
string className = "";
|
2017-12-01 06:14:40 +08:00
|
|
|
while (std::getline(classNamesFile, className))
|
2017-10-26 04:02:35 +08:00
|
|
|
classNamesVec.push_back(className);
|
|
|
|
}
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2018-01-02 22:26:29 +08:00
|
|
|
String object_roi_style = parser.get<String>("style");
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
Mat frame;
|
|
|
|
cap >> frame; // get a new frame from camera/video or read image
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (frame.empty())
|
|
|
|
{
|
|
|
|
waitKey();
|
|
|
|
break;
|
|
|
|
}
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (frame.channels() == 4)
|
|
|
|
cvtColor(frame, frame, COLOR_BGRA2BGR);
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Prepare blob]
|
2017-12-07 14:42:29 +08:00
|
|
|
Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false); //Convert Mat to batch of images
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Prepare blob]
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Set input blob]
|
|
|
|
net.setInput(inputBlob, "data"); //set the network input
|
|
|
|
//! [Set input blob]
|
|
|
|
|
|
|
|
//! [Make forward pass]
|
|
|
|
Mat detectionMat = net.forward("detection_out"); //compute output
|
2017-12-01 06:14:40 +08:00
|
|
|
//! [Make forward pass]
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-12-01 06:14:40 +08:00
|
|
|
vector<double> layersTimings;
|
2018-01-02 22:26:29 +08:00
|
|
|
double tick_freq = getTickFrequency();
|
|
|
|
double time_ms = net.getPerfProfile(layersTimings) / tick_freq * 1000;
|
|
|
|
putText(frame, format("FPS: %.2f ; time: %.2f ms", 1000.f / time_ms, time_ms),
|
|
|
|
Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
float confidenceThreshold = parser.get<float>("min_confidence");
|
|
|
|
for (int i = 0; i < detectionMat.rows; i++)
|
2017-09-25 04:34:08 +08:00
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
const int probability_index = 5;
|
|
|
|
const int probability_size = detectionMat.cols - probability_index;
|
|
|
|
float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);
|
|
|
|
|
|
|
|
size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
|
|
|
|
float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);
|
|
|
|
|
|
|
|
if (confidence > confidenceThreshold)
|
|
|
|
{
|
2018-01-02 22:26:29 +08:00
|
|
|
float x_center = detectionMat.at<float>(i, 0) * frame.cols;
|
|
|
|
float y_center = detectionMat.at<float>(i, 1) * frame.rows;
|
|
|
|
float width = detectionMat.at<float>(i, 2) * frame.cols;
|
|
|
|
float height = detectionMat.at<float>(i, 3) * frame.rows;
|
|
|
|
Point p1(cvRound(x_center - width / 2), cvRound(y_center - height / 2));
|
|
|
|
Point p2(cvRound(x_center + width / 2), cvRound(y_center + height / 2));
|
|
|
|
Rect object(p1, p2);
|
|
|
|
|
|
|
|
Scalar object_roi_color(0, 255, 0);
|
|
|
|
|
|
|
|
if (object_roi_style == "box")
|
2017-10-26 04:02:35 +08:00
|
|
|
{
|
2018-01-02 22:26:29 +08:00
|
|
|
rectangle(frame, object, object_roi_color);
|
2017-10-26 04:02:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-02 22:26:29 +08:00
|
|
|
Point p_center(cvRound(x_center), cvRound(y_center));
|
|
|
|
line(frame, object.tl(), p_center, object_roi_color, 1);
|
2017-10-26 04:02:35 +08:00
|
|
|
}
|
2018-01-02 22:26:29 +08:00
|
|
|
|
|
|
|
String className = objectClass < classNamesVec.size() ? classNamesVec[objectClass] : cv::format("unknown(%d)", objectClass);
|
|
|
|
String label = format("%s: %.2f", className.c_str(), confidence);
|
|
|
|
int baseLine = 0;
|
|
|
|
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
|
|
|
|
rectangle(frame, Rect(p1, Size(labelSize.width, labelSize.height + baseLine)),
|
|
|
|
object_roi_color, CV_FILLED);
|
|
|
|
putText(frame, label, p1 + Point(0, labelSize.height),
|
|
|
|
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
|
2017-10-26 04:02:35 +08:00
|
|
|
}
|
2017-09-25 04:34:08 +08:00
|
|
|
}
|
2018-01-09 03:30:28 +08:00
|
|
|
if(writer.isOpened())
|
|
|
|
{
|
|
|
|
writer.write(frame);
|
|
|
|
}
|
2017-09-25 04:34:08 +08:00
|
|
|
|
2017-12-01 06:14:40 +08:00
|
|
|
imshow("YOLO: Detections", frame);
|
2017-10-26 04:02:35 +08:00
|
|
|
if (waitKey(1) >= 0) break;
|
|
|
|
}
|
2017-09-25 04:34:08 +08:00
|
|
|
|
|
|
|
return 0;
|
2017-10-26 04:02:35 +08:00
|
|
|
} // main
|