Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

practice 3 Troynyakov Vladimir #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions include/detectedobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

struct DetectedObject
{
int Left;
int Right;
int Top;
int Bottom;
int uuid;
std::string classname;
int classid;
std::string className;
float score;
float left;
float bottom;
float right;
float top;


DetectedObject(int c, std::string cn, float s, float l, float b, float r, float t) : classid(c), className(cn), score(s), left(l), bottom(b), right(r), top(t)
{
}
};
19 changes: 19 additions & 0 deletions include/detector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <string>
#include <fstream>

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
Expand All @@ -16,4 +17,22 @@ class Detector
{
public:
virtual vector<DetectedObject> Detect(Mat image) = 0 {}
virtual string DecodeLabel(int n) = 0 {}
};

class DnnDetector : public Detector {
private:
string path_to_model;
string path_to_config;
string path_to_labels;
int input_width = 300;
int input_height = 300;
double scale = 0.007843;
Scalar mean = Scalar(127.5, 127.5, 127.5);;
bool swapRB = false;
Net net;
public:
DnnDetector(string path_to_model, string path_to_config, string path_to_labels);
vector<DetectedObject> Detect(Mat image);
string DecodeLabel(int n);
};
59 changes: 59 additions & 0 deletions samples/practice3_TroynyakovVladimir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <string>
#include <iostream>
#include "detector.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace std;
using namespace cv;
using namespace cv::dnn;

const char* cmdAbout =
"This is an empty application that can be treated as a template for your "
"own doing-something-cool applications.";

const char* cmdOptions =
"{ i image | <none> | image to process }"
"{ m model_path | | path to model }"
"{ c config_path | | path to model configuration }"
"{ l label_path | | path to class labels }"
"{ h ? help usage | | print help message }";


int main(int argc, const char** argv) {
// Parse command line arguments.
CommandLineParser parser(argc, argv, cmdOptions);
parser.about(cmdAbout);

// If help option is given, print help message and exit.
if (parser.get<bool>("help")) {
parser.printMessage();
return 0;
}

string imgName(parser.get<string>("image"));
string model_path(parser.get<string>("model_path"));
string config_path(parser.get<string>("config_path"));
string label_path(parser.get<string>("label_path"));
Mat image = imread(imgName);

Detector* detector = new DnnDetector(model_path, config_path, label_path);
vector<DetectedObject> vec = detector->Detect(image);
for (int i = 0; i < vec.size(); i++) {
Point leftbottom(vec[i].left, vec[i].bottom);
Point righttop(vec[i].right, vec[i].top);
Rect rect(leftbottom, righttop);
rectangle(image, rect, Scalar(0, 255, 0), 2);
cout << "Class: " << vec[i].className << endl;
cout << "Confidence: " << to_string(vec[i].score) << endl;
cout << leftbottom << " " << righttop << endl;
//cout << vec[i].left << " " << vec[i].right << " " << vec[i].bottom << " " << vec[i].top;

}
imshow("Detected", image);
waitKey(0);
delete detector;
return 0;
}
49 changes: 48 additions & 1 deletion src/detector.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
#include "detector.h"
#include "detector.h"
DnnDetector::DnnDetector(string path_to_model,
string path_to_config,
string path_to_labels) {
this->path_to_model = path_to_model;
this->path_to_config = path_to_config;
this->path_to_labels = path_to_labels;

Net net = readNet(this->path_to_model, this->path_to_config);
int backendId = DNN_BACKEND_OPENCV;
int targetId = DNN_TARGET_CPU;
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
this->net = net;
}


vector<DetectedObject> DnnDetector::Detect(Mat image) {
Mat inputTensor;
blobFromImage(image, inputTensor, scale, Size(input_width, input_height), mean, swapRB, false);
net.setInput(inputTensor);
Mat output = net.forward().reshape(1, 1);
output = output.reshape(1, output.cols / 7);
vector<DetectedObject> vecobj;
for (int i = 0; i < output.rows; i++)
{
vecobj.push_back(DetectedObject((int)output.at<float>(i, 1),
this->DecodeLabel((int)output.at<float>(i, 1) - 1),
output.at<float>(i, 2),
output.at<float>(i, 3)*image.cols,
output.at<float>(i, 4)*image.rows,
output.at<float>(i, 5)*image.cols,
output.at<float>(i, 6)*image.rows));
}
return vecobj;

}


string DnnDetector::DecodeLabel(int n) {
ifstream file_labels(path_to_labels);
vector<string> classesNames;
string line;
while (getline(file_labels, line)) {
classesNames.push_back(line);
}
return classesNames[n];
}