Commit 740ca1d2 authored by Anna Petrovicheva's avatar Anna Petrovicheva

Finalized the sample

parent 9b7ffbce
......@@ -9,54 +9,42 @@ using namespace cv::dnn;
#include <cstdlib>
using namespace std;
const size_t width = 300;
const size_t height = 300;
//static void colorizeSegmentation(dnn::Blob& score,
// const vector<cv::Vec3b>& colors,
// cv::Mat& segm)
//{
// const int rows = score.rows();
// const int cols = score.cols();
// const int chns = score.channels();
// cv::Mat maxCl(rows, cols, CV_8UC1);
// cv::Mat maxVal(rows, cols, CV_32FC1);
// for (int ch = 0; ch < chns; ch++)
// {
// for (int row = 0; row < rows; row++)
// {
// const float* ptrScore = score.ptrf(0, ch, row);
// uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
// float* ptrMaxVal = maxVal.ptr<float>(row);
// for (int col = 0; col < cols; col++)
// {
// if (ptrScore[col] > ptrMaxVal[col])
// {
// ptrMaxVal[col] = ptrScore[col];
// ptrMaxCl[col] = ch;
// }
// }
// }
// }
// segm.create(rows, cols, CV_8UC3);
// for (int row = 0; row < rows; row++)
// {
// const uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
// cv::Vec3b* ptrSegm = segm.ptr<cv::Vec3b>(row);
// for (int col = 0; col < cols; col++)
// {
// ptrSegm[col] = colors[ptrMaxCl[col]];
// }
// }
//}
const char* about = "This sample uses Single-Shot Detector to detect objects "
"from camera\n"; // TODO: link
Mat getMean(const size_t& height, const size_t& width)
{
Mat mean;
const int meanValues[3] = {104, 117, 123};
vector<Mat> meanChannels;
for(size_t i = 0; i < 3; i++)
{
Mat channel(height, width, CV_32F, Scalar(meanValues[i]));
meanChannels.push_back(channel);
}
cv::merge(meanChannels, mean);
return mean;
}
void preprocess(Mat& frame)
{
frame.convertTo(frame, CV_32FC3);
resize(frame, frame, Size(width, height)); //SSD accepts 300x300 RGB-images
Mat mean = getMean(width, height);
cv::subtract(frame, mean, frame);
}
const char* about = "This sample uses Single-Shot Detector "
"(https://arxiv.org/abs/1512.02325)"
"to detect objects on image\n"; // TODO: link
const char* params
= "{ help | help | false | print usage }"
"{ proto | model prototxt file | | model configuration }"
"{ model | caffemodel file | | model weights }";
= "{ help | help | false | print usage }"
"{ proto | model prototxt file | | model configuration }"
"{ model | caffemodel file | | model weights }"
"{ image | image file | | image for detection }";
int main(int argc, char** argv)
{
......@@ -102,53 +90,50 @@ int main(int argc, char** argv)
importer.release(); //We don't need importer anymore
//! [Initialize network]
VideoCapture camera;
if (!camera.open(0))
{
cout << "Unable to open camera stream" << endl;
return 0;
}
cv::Mat frame = cv::imread(parser.get<string>("image"), -1);
size_t i = 0;
for (;; )
//! [Prepare blob]
preprocess(frame);
dnn::Blob inputBlob = dnn::Blob(frame); //Convert Mat to dnn::Blob image
//! [Prepare blob]
//! [Set input blob]
net.setBlob(".data", inputBlob); //set the network input
//! [Set input blob]
//! [Make forward pass]
net.forward(); //compute output
//! [Make forward pass]
//! [Gather output]
dnn::Blob detection = net.getBlob("detection_out");
Mat detectionMat(detection.rows(), detection.cols(), CV_32F, detection.ptrf());
for(size_t i = 0; i < detectionMat.rows; i++)
{
Mat frame;
camera >> frame;
if (frame.empty())
break;
//! [Prepare blob]
resize(frame, frame, Size(300, 300)); //SSD accepts 300x300 RGB-images
dnn::Blob inputBlob = dnn::Blob(frame); //Convert Mat to dnn::Blob image
//! [Prepare blob]
std::ostringstream stream;
stream << "folder/" << i << ".jpg";
imwrite(stream.str(), frame);
//! [Set input blob]
net.setBlob(".data", inputBlob); //set the network input
//! [Set input blob]
//! [Make forward pass]
net.forward(); //compute output
//! [Make forward pass]
// //! [Gather output]
// dnn::Blob detection = net.getBlob("detection_out");
// // cv::Mat colorize;
// // colorizeSegmentation(score, colors, colorize);
// // cv::Mat show;
// // cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show);
// // cv::imshow("show", show);
// // cv::waitKey(0);
// // return 0;
// imshow("frame", frame);
// if (waitKey(1) == 27)
// break; // stop capturing by pressing ESC
std::cout << "Class: " << detectionMat.at<float>(i, 1) << std::endl;
std::cout << "Confidence: " << detectionMat.at<float>(i, 2) << std::endl;
std::cout << " " << detectionMat.at<float>(i, 3) * width;
std::cout << " " << detectionMat.at<float>(i, 4) * height;
std::cout << " " << detectionMat.at<float>(i, 5) * width;
std::cout << " " << detectionMat.at<float>(i, 6) * height;
float xLeftBottom = detectionMat.at<float>(i, 3) * width;
float yLeftBottom = detectionMat.at<float>(i, 4) * height;
float xRightTop = detectionMat.at<float>(i, 5) * width;
float yRightTop = detectionMat.at<float>(i, 6) * height;
Rect object(xLeftBottom, yLeftBottom,
xRightTop - xLeftBottom,
yRightTop - yLeftBottom);
rectangle(frame, object, Scalar(0, 255, 0));
}
camera.release();
imshow("detections", frame);
waitKey();
return 0;
} // main
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