2017-06-26 18:35:51 +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 width = 300;
|
|
|
|
const size_t height = 300;
|
|
|
|
|
2017-06-26 21:22:50 +08:00
|
|
|
static Mat getMean(const size_t& imageHeight, const size_t& imageWidth)
|
2017-06-26 18:35:51 +08:00
|
|
|
{
|
|
|
|
Mat mean;
|
|
|
|
|
|
|
|
const int meanValues[3] = {104, 117, 123};
|
|
|
|
vector<Mat> meanChannels;
|
2017-06-26 21:22:50 +08:00
|
|
|
for(int i = 0; i < 3; i++)
|
2017-06-26 18:35:51 +08:00
|
|
|
{
|
2017-06-26 21:22:50 +08:00
|
|
|
Mat channel((int)imageHeight, (int)imageWidth, CV_32F, Scalar(meanValues[i]));
|
2017-06-26 18:35:51 +08:00
|
|
|
meanChannels.push_back(channel);
|
|
|
|
}
|
|
|
|
cv::merge(meanChannels, mean);
|
|
|
|
return mean;
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:22:50 +08:00
|
|
|
static Mat preprocess(const Mat& frame)
|
2017-06-26 18:35:51 +08:00
|
|
|
{
|
|
|
|
Mat preprocessed;
|
|
|
|
frame.convertTo(preprocessed, CV_32F);
|
|
|
|
resize(preprocessed, preprocessed, Size(width, height)); //SSD accepts 300x300 RGB-images
|
|
|
|
|
|
|
|
Mat mean = getMean(width, height);
|
|
|
|
cv::subtract(preprocessed, mean, preprocessed);
|
|
|
|
|
|
|
|
return preprocessed;
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
const char* classNames[] = {"background",
|
|
|
|
"aeroplane", "bicycle", "bird", "boat",
|
|
|
|
"bottle", "bus", "car", "cat", "chair",
|
|
|
|
"cow", "diningtable", "dog", "horse",
|
|
|
|
"motorbike", "person", "pottedplant",
|
|
|
|
"sheep", "sofa", "train", "tvmonitor"};
|
|
|
|
|
2017-06-26 18:35:51 +08:00
|
|
|
const char* about = "This sample uses Single-Shot Detector "
|
2017-10-26 04:02:35 +08:00
|
|
|
"(https://arxiv.org/abs/1512.02325) "
|
|
|
|
"to detect objects on camera/video/image.\n"
|
|
|
|
".caffemodel model's file is available here: "
|
|
|
|
"https://github.com/weiliu89/caffe/tree/ssd#models\n"
|
|
|
|
"Default network is 300x300 and 20-classes VOC.\n";
|
2017-06-26 18:35:51 +08:00
|
|
|
|
|
|
|
const char* params
|
|
|
|
= "{ help | false | print usage }"
|
|
|
|
"{ proto | | model configuration }"
|
|
|
|
"{ model | | model weights }"
|
2017-10-26 04:02:35 +08:00
|
|
|
"{ camera_device | 0 | camera device number}"
|
|
|
|
"{ video | | video or image for detection}"
|
2017-06-26 18:35:51 +08:00
|
|
|
"{ min_confidence | 0.5 | min confidence }";
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
cv::CommandLineParser parser(argc, argv, params);
|
|
|
|
|
|
|
|
if (parser.get<bool>("help"))
|
|
|
|
{
|
2017-10-26 04:02:35 +08:00
|
|
|
cout << about << endl;
|
2017-06-26 18:35:51 +08:00
|
|
|
parser.printMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String modelConfiguration = parser.get<string>("proto");
|
|
|
|
String modelBinary = parser.get<string>("model");
|
|
|
|
|
2017-08-03 22:43:52 +08:00
|
|
|
//! [Initialize network]
|
|
|
|
dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
|
|
|
|
//! [Initialize network]
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-08-03 22:43:52 +08:00
|
|
|
if (net.empty())
|
2017-06-26 18:35:51 +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/weiliu89/caffe/tree/ssd#models" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
VideoCapture cap;
|
|
|
|
if (parser.get<String>("video").empty())
|
|
|
|
{
|
|
|
|
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"));
|
|
|
|
if(!cap.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Couldn't open image or video: " << parser.get<String>("video") << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
cv::Mat frame;
|
|
|
|
cap >> frame; // get a new frame from camera/video or read image
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (frame.empty())
|
|
|
|
{
|
|
|
|
waitKey();
|
|
|
|
break;
|
|
|
|
}
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
if (frame.channels() == 4)
|
|
|
|
cvtColor(frame, frame, COLOR_BGRA2BGR);
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Prepare blob]
|
|
|
|
Mat preprocessedFrame = preprocess(frame);
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
Mat inputBlob = blobFromImage(preprocessedFrame, 1.0f, Size(), Scalar(), false); //Convert Mat to batch of images
|
|
|
|
//! [Prepare blob]
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Set input blob]
|
|
|
|
net.setInput(inputBlob, "data"); //set the network input
|
|
|
|
//! [Set input blob]
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
//! [Make forward pass]
|
|
|
|
Mat detection = net.forward("detection_out"); //compute output
|
|
|
|
//! [Make forward pass]
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
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), 0, 0.5, Scalar(0,0,255));
|
2017-06-26 18:35:51 +08:00
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
|
2017-06-26 18:35:51 +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++)
|
|
|
|
{
|
|
|
|
float confidence = detectionMat.at<float>(i, 2);
|
|
|
|
|
|
|
|
if(confidence > confidenceThreshold)
|
|
|
|
{
|
|
|
|
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
|
|
|
|
|
|
|
|
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
|
|
|
|
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
|
|
|
|
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
|
|
|
|
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
|
|
|
|
|
|
|
|
ss.str("");
|
|
|
|
ss << confidence;
|
|
|
|
String conf(ss.str());
|
|
|
|
|
|
|
|
Rect object(xLeftBottom, yLeftBottom,
|
|
|
|
xRightTop - xLeftBottom,
|
|
|
|
yRightTop - yLeftBottom);
|
|
|
|
|
|
|
|
rectangle(frame, object, Scalar(0, 255, 0));
|
|
|
|
String label = String(classNames[objectClass]) + ": " + conf;
|
|
|
|
int baseLine = 0;
|
|
|
|
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
|
|
|
|
rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
|
|
|
|
Size(labelSize.width, labelSize.height + baseLine)),
|
|
|
|
Scalar(255, 255, 255), CV_FILLED);
|
|
|
|
putText(frame, label, Point(xLeftBottom, yLeftBottom),
|
|
|
|
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
|
|
|
|
}
|
2017-06-26 18:35:51 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 04:02:35 +08:00
|
|
|
imshow("detections", frame);
|
|
|
|
if (waitKey(1) >= 0) break;
|
|
|
|
}
|
2017-06-26 18:35:51 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // main
|