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

Finalized the sample

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