2017-07-15 01:47:56 +08:00
|
|
|
#include <opencv2/dnn.hpp>
|
|
|
|
#include <opencv2/dnn/shape_utils.hpp>
|
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::dnn;
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
const size_t inWidth = 300;
|
|
|
|
const size_t inHeight = 300;
|
|
|
|
const float inScaleFactor = 0.007843f;
|
|
|
|
const float meanVal = 127.5;
|
|
|
|
const char* classNames[] = {"background",
|
|
|
|
"aeroplane", "bicycle", "bird", "boat",
|
|
|
|
"bottle", "bus", "car", "cat", "chair",
|
|
|
|
"cow", "diningtable", "dog", "horse",
|
|
|
|
"motorbike", "person", "pottedplant",
|
|
|
|
"sheep", "sofa", "train", "tvmonitor"};
|
|
|
|
|
|
|
|
const char* params
|
|
|
|
= "{ help | false | print usage }"
|
2017-08-09 00:53:07 +08:00
|
|
|
"{ proto | MobileNetSSD_deploy.prototxt | model configuration }"
|
|
|
|
"{ model | MobileNetSSD_deploy.caffemodel | model weights }"
|
2017-10-26 04:02:35 +08:00
|
|
|
"{ camera_device | 0 | camera device number }"
|
|
|
|
"{ video | | video or image for detection}"
|
2017-07-15 01:47:56 +08:00
|
|
|
"{ out | | path to output video file}"
|
2017-11-21 22:28:42 +08:00
|
|
|
"{ min_confidence | 0.2 | min confidence }"
|
|
|
|
"{ opencl | false | enable OpenCL }"
|
|
|
|
;
|
2017-07-15 01:47:56 +08:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
CommandLineParser parser(argc, argv, params);
|
2018-01-02 04:01:23 +08:00
|
|
|
parser.about("This sample uses MobileNet Single-Shot Detector "
|
|
|
|
"(https://arxiv.org/abs/1704.04861) "
|
|
|
|
"to detect objects on camera/video/image.\n"
|
|
|
|
".caffemodel model's file is available here: "
|
|
|
|
"https://github.com/chuanqi305/MobileNet-SSD\n"
|
|
|
|
"Default network is 300x300 and 20-classes VOC.\n");
|
|
|
|
|
|
|
|
if (parser.get<bool>("help") || argc == 1)
|
2017-07-15 01:47:56 +08:00
|
|
|
{
|
|
|
|
parser.printMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String modelConfiguration = parser.get<string>("proto");
|
|
|
|
String modelBinary = parser.get<string>("model");
|
2018-01-02 04:01:23 +08:00
|
|
|
CV_Assert(!modelConfiguration.empty() && !modelBinary.empty());
|
2017-07-15 01:47:56 +08:00
|
|
|
|
|
|
|
//! [Initialize network]
|
|
|
|
dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
|
|
|
|
//! [Initialize network]
|
|
|
|
|
2017-11-21 22:28:42 +08:00
|
|
|
if (parser.get<bool>("opencl"))
|
|
|
|
{
|
|
|
|
net.setPreferableTarget(DNN_TARGET_OPENCL);
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (net.empty())
|
2017-07-15 01:47:56 +08:00
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
cerr << "Can't load network by using the following files: " << endl;
|
|
|
|
cerr << "prototxt: " << modelConfiguration << endl;
|
|
|
|
cerr << "caffemodel: " << modelBinary << endl;
|
|
|
|
cerr << "Models can be downloaded here:" << endl;
|
|
|
|
cerr << "https://github.com/chuanqi305/MobileNet-SSD" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoCapture cap;
|
2018-01-02 04:01:23 +08:00
|
|
|
if (!parser.has("video"))
|
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
|
|
|
|
{
|
|
|
|
cap.open(parser.get<String>("video"));
|
2017-07-15 01:47:56 +08:00
|
|
|
if(!cap.isOpened())
|
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
cout << "Couldn't open image or video: " << parser.get<String>("video") << endl;
|
2017-07-15 01:47:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 04:01:23 +08:00
|
|
|
//Acquire input size
|
|
|
|
Size inVideoSize((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
|
|
|
|
(int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));
|
2017-07-15 01:47:56 +08:00
|
|
|
|
2017-12-06 21:16:37 +08:00
|
|
|
double fps = cap.get(CV_CAP_PROP_FPS);
|
|
|
|
int fourcc = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
|
2017-07-15 01:47:56 +08:00
|
|
|
VideoWriter outputVideo;
|
|
|
|
outputVideo.open(parser.get<String>("out") ,
|
2017-12-06 21:16:37 +08:00
|
|
|
(fourcc != 0 ? fourcc : VideoWriter::fourcc('M','J','P','G')),
|
2018-01-02 04:01:23 +08:00
|
|
|
(fps != 0 ? fps : 10.0), inVideoSize, true);
|
2017-07-15 01:47:56 +08:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
Mat frame;
|
2017-10-26 04:02:35 +08:00
|
|
|
cap >> frame; // get a new frame from camera/video or read image
|
|
|
|
|
|
|
|
if (frame.empty())
|
|
|
|
{
|
|
|
|
waitKey();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.channels() == 4)
|
|
|
|
cvtColor(frame, frame, COLOR_BGRA2BGR);
|
2017-07-15 01:47:56 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Prepare blob]
|
2017-07-15 01:47:56 +08:00
|
|
|
Mat inputBlob = blobFromImage(frame, inScaleFactor,
|
2018-01-02 04:01:23 +08:00
|
|
|
Size(inWidth, inHeight),
|
|
|
|
Scalar(meanVal, meanVal, meanVal),
|
|
|
|
false, false); //Convert Mat to batch of images
|
2017-07-15 01:47:56 +08:00
|
|
|
//! [Prepare blob]
|
|
|
|
|
|
|
|
//! [Set input blob]
|
2018-01-02 04:01:23 +08:00
|
|
|
net.setInput(inputBlob); //set the network input
|
2017-07-15 01:47:56 +08:00
|
|
|
//! [Set input blob]
|
|
|
|
|
|
|
|
//! [Make forward pass]
|
2018-01-02 04:01:23 +08:00
|
|
|
Mat detection = net.forward(); //compute output
|
2017-07-15 01:47:56 +08:00
|
|
|
//! [Make forward pass]
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
vector<double> layersTimings;
|
2017-08-02 22:27:58 +08:00
|
|
|
double freq = getTickFrequency() / 1000;
|
|
|
|
double time = net.getPerfProfile(layersTimings) / freq;
|
|
|
|
|
2017-07-15 01:47:56 +08:00
|
|
|
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (!outputVideo.isOpened())
|
|
|
|
{
|
2018-01-02 04:01:23 +08:00
|
|
|
putText(frame, format("FPS: %.2f ; time: %.2f ms", 1000.f/time, time),
|
|
|
|
Point(20,20), 0, 0.5, Scalar(0,0,255));
|
2017-10-26 04:02:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cout << "Inference time, ms: " << time << endl;
|
|
|
|
|
2017-07-15 01:47:56 +08:00
|
|
|
float confidenceThreshold = parser.get<float>("min_confidence");
|
|
|
|
for(int i = 0; i < detectionMat.rows; i++)
|
|
|
|
{
|
|
|
|
float confidence = detectionMat.at<float>(i, 2);
|
|
|
|
|
|
|
|
if(confidence > confidenceThreshold)
|
|
|
|
{
|
|
|
|
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
|
|
|
|
|
2018-01-02 04:01:23 +08:00
|
|
|
int left = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
|
|
|
|
int top = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
|
|
|
|
int right = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
|
|
|
|
int bottom = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
|
2017-07-15 01:47:56 +08:00
|
|
|
|
2018-01-02 04:01:23 +08:00
|
|
|
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
|
|
|
|
String label = format("%s: %.2f", classNames[objectClass], confidence);
|
2017-07-15 01:47:56 +08:00
|
|
|
int baseLine = 0;
|
|
|
|
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
|
2018-01-02 04:01:23 +08:00
|
|
|
top = max(top, labelSize.height);
|
|
|
|
rectangle(frame, Point(left, top - labelSize.height),
|
|
|
|
Point(left + labelSize.width, top + baseLine),
|
2017-07-15 01:47:56 +08:00
|
|
|
Scalar(255, 255, 255), CV_FILLED);
|
2018-01-02 04:01:23 +08:00
|
|
|
putText(frame, label, Point(left, top),
|
2017-07-15 01:47:56 +08:00
|
|
|
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputVideo.isOpened())
|
|
|
|
outputVideo << frame;
|
|
|
|
|
|
|
|
imshow("detections", frame);
|
|
|
|
if (waitKey(1) >= 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // main
|