Commit a42ca2ee authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #10483 from dkurt:fix_dnn_ssd_cpp_sample

parents bf80c941 d0580df3
...@@ -13,7 +13,6 @@ using namespace std; ...@@ -13,7 +13,6 @@ using namespace std;
const size_t inWidth = 300; const size_t inWidth = 300;
const size_t inHeight = 300; const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const float inScaleFactor = 0.007843f; const float inScaleFactor = 0.007843f;
const float meanVal = 127.5; const float meanVal = 127.5;
const char* classNames[] = {"background", const char* classNames[] = {"background",
...@@ -23,13 +22,6 @@ const char* classNames[] = {"background", ...@@ -23,13 +22,6 @@ const char* classNames[] = {"background",
"motorbike", "person", "pottedplant", "motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"}; "sheep", "sofa", "train", "tvmonitor"};
const char* 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";
const char* params const char* params
= "{ help | false | print usage }" = "{ help | false | print usage }"
"{ proto | MobileNetSSD_deploy.prototxt | model configuration }" "{ proto | MobileNetSSD_deploy.prototxt | model configuration }"
...@@ -44,16 +36,22 @@ const char* params ...@@ -44,16 +36,22 @@ const char* params
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
CommandLineParser parser(argc, argv, params); CommandLineParser parser(argc, argv, params);
parser.about("This sample uses MobileNet Single-Shot Detector "
if (parser.get<bool>("help")) "(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)
{ {
cout << about << endl;
parser.printMessage(); parser.printMessage();
return 0; return 0;
} }
String modelConfiguration = parser.get<string>("proto"); String modelConfiguration = parser.get<string>("proto");
String modelBinary = parser.get<string>("model"); String modelBinary = parser.get<string>("model");
CV_Assert(!modelConfiguration.empty() && !modelBinary.empty());
//! [Initialize network] //! [Initialize network]
dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary); dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
...@@ -75,7 +73,7 @@ int main(int argc, char** argv) ...@@ -75,7 +73,7 @@ int main(int argc, char** argv)
} }
VideoCapture cap; VideoCapture cap;
if (parser.get<String>("video").empty()) if (!parser.has("video"))
{ {
int cameraDevice = parser.get<int>("camera_device"); int cameraDevice = parser.get<int>("camera_device");
cap = VideoCapture(cameraDevice); cap = VideoCapture(cameraDevice);
...@@ -95,32 +93,16 @@ int main(int argc, char** argv) ...@@ -95,32 +93,16 @@ int main(int argc, char** argv)
} }
} }
Size inVideoSize; //Acquire input size
inVideoSize = Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH), //Acquire input size Size inVideoSize((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
(int) cap.get(CV_CAP_PROP_FRAME_HEIGHT)); (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));
Size cropSize;
if (inVideoSize.width / (float)inVideoSize.height > WHRatio)
{
cropSize = Size(static_cast<int>(inVideoSize.height * WHRatio),
inVideoSize.height);
}
else
{
cropSize = Size(inVideoSize.width,
static_cast<int>(inVideoSize.width / WHRatio));
}
Rect crop(Point((inVideoSize.width - cropSize.width) / 2,
(inVideoSize.height - cropSize.height) / 2),
cropSize);
double fps = cap.get(CV_CAP_PROP_FPS); double fps = cap.get(CV_CAP_PROP_FPS);
int fourcc = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC)); int fourcc = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
VideoWriter outputVideo; VideoWriter outputVideo;
outputVideo.open(parser.get<String>("out") , outputVideo.open(parser.get<String>("out") ,
(fourcc != 0 ? fourcc : VideoWriter::fourcc('M','J','P','G')), (fourcc != 0 ? fourcc : VideoWriter::fourcc('M','J','P','G')),
(fps != 0 ? fps : 10.0), cropSize, true); (fps != 0 ? fps : 10.0), inVideoSize, true);
for(;;) for(;;)
{ {
...@@ -138,15 +120,17 @@ int main(int argc, char** argv) ...@@ -138,15 +120,17 @@ int main(int argc, char** argv)
//! [Prepare blob] //! [Prepare blob]
Mat inputBlob = blobFromImage(frame, inScaleFactor, Mat inputBlob = blobFromImage(frame, inScaleFactor,
Size(inWidth, inHeight), meanVal, false); //Convert Mat to batch of images Size(inWidth, inHeight),
Scalar(meanVal, meanVal, meanVal),
false, false); //Convert Mat to batch of images
//! [Prepare blob] //! [Prepare blob]
//! [Set input blob] //! [Set input blob]
net.setInput(inputBlob, "data"); //set the network input net.setInput(inputBlob); //set the network input
//! [Set input blob] //! [Set input blob]
//! [Make forward pass] //! [Make forward pass]
Mat detection = net.forward("detection_out"); //compute output Mat detection = net.forward(); //compute output
//! [Make forward pass] //! [Make forward pass]
vector<double> layersTimings; vector<double> layersTimings;
...@@ -155,13 +139,10 @@ int main(int argc, char** argv) ...@@ -155,13 +139,10 @@ int main(int argc, char** argv)
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
frame = frame(crop);
ostringstream ss;
if (!outputVideo.isOpened()) if (!outputVideo.isOpened())
{ {
ss << "FPS: " << 1000/time << " ; time: " << time << " ms"; putText(frame, format("FPS: %.2f ; time: %.2f ms", 1000.f/time, time),
putText(frame, ss.str(), Point(20,20), 0, 0.5, Scalar(0,0,255)); Point(20,20), 0, 0.5, Scalar(0,0,255));
} }
else else
cout << "Inference time, ms: " << time << endl; cout << "Inference time, ms: " << time << endl;
...@@ -175,27 +156,20 @@ int main(int argc, char** argv) ...@@ -175,27 +156,20 @@ int main(int argc, char** argv)
{ {
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1)); size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols); int left = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows); int top = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols); int right = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows); int bottom = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
ss.str("");
ss << confidence;
String conf(ss.str());
Rect object((int)xLeftBottom, (int)yLeftBottom,
(int)(xRightTop - xLeftBottom),
(int)(yRightTop - yLeftBottom));
rectangle(frame, object, Scalar(0, 255, 0)); rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
String label = String(classNames[objectClass]) + ": " + conf; String label = format("%s: %.2f", classNames[objectClass], confidence);
int baseLine = 0; int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height), top = max(top, labelSize.height);
Size(labelSize.width, labelSize.height + baseLine)), rectangle(frame, Point(left, top - labelSize.height),
Point(left + labelSize.width, top + baseLine),
Scalar(255, 255, 255), CV_FILLED); Scalar(255, 255, 255), CV_FILLED);
putText(frame, label, Point(xLeftBottom, yLeftBottom), putText(frame, label, Point(left, top),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0)); FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment