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 #3

Merged
merged 3 commits into from
Jul 8, 2019
Merged
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
3 changes: 2 additions & 1 deletion include/detectedobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct DetectedObject
int Right;
int Top;
int Bottom;
int uuid;
double score;
int classid;
std::string classname;
};
15 changes: 15 additions & 0 deletions include/detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ class Detector
public:
virtual vector<DetectedObject> Detect(Mat image) = 0 {}
};
class DnnDetector : public Detector
{
string modelPath;
string configPath;
string labelPath;
int width;
int height;
double scale;
Scalar mean;
bool swapRB;
Net net;
public:
DnnDetector(string _modelPath, string _configPath, string _labelPath, int _width, int _height, double _scale, Scalar _mean, bool _swapRB);
vector<DetectedObject> Detect (Mat frame);
};
78 changes: 78 additions & 0 deletions samples/practice3_YANA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <string>
#include <iostream>

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

#include "detector.h"

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 | | image to process }"
"{ w width | | image width for classification }"
"{ h heigth | | image heigth fro classification }"
"{ model_path | | path to model }"
"{ config_path | | path to model configuration }"
"{ label_path | | path to class labels }"
"{ scale | | }"
"{ mean | | vector of mean model values }"
"{ swap | | swap R and B channels. TRUE|FALSE }"
"{ 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;
}

// Load image and init parameters
String imgName(parser.get<String>("image"));
Mat image = imread(imgName);

String modelPath(parser.get<String>("model_path"));
String configPath(parser.get<String>("config_path"));
String labelPath(parser.get<String>("label_path"));
int width = parser.get<int>("width");
int heigth = parser.get<int>("heigth");
double scale = parser.get<double>("scale");
Scalar mean = parser.get<Scalar>("mean");
bool swap = parser.get<bool>("swap");

String label[21] = { "background", "aeroplane", "bicycle", "bird",
"boat","bottle", "bus", "car", "cat", "chair", "cow",
"diningtable", "dog", "horse", "motorbike", "person",
"pottedplant", "sheep", "sofa", "train", "tvmonitor" };

//Image detection
Detector* a = new DnnDetector(modelPath, configPath, labelPath, width, heigth, scale, mean, swap);
vector<DetectedObject> objects = a->Detect(image);

//Show result
for (int i = 0; i < objects.size(); i++)
{
Point point1(objects[i].Left, objects[i].Bottom);
Point point2(objects[i].Right, objects[i].Top);
rectangle(image, point1, point2, Scalar(0, 0, 255), 3);
string text = "Class: " + label[objects[i].classid] + " Confidence: " + std::to_string(objects[i].score);
putText(image, text, Point(objects[i].Left, objects[i].Bottom - 10), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 0));
}
namedWindow("Objects", WINDOW_NORMAL);
imshow("Objects", image);
waitKey(0);

return 0;
}
41 changes: 40 additions & 1 deletion src/detector.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
#include "detector.h"
#include "detector.h"

DnnDetector::DnnDetector(string _modelPath, string _configPath, string _labelPath, int _width, int _height, double _scale, Scalar _mean, bool _swapRB)
{
modelPath = _modelPath;
configPath = _configPath;
labelPath = _labelPath;
width = _width;
height = _height;
scale = _scale;
mean = _mean;
swapRB = _swapRB;
net = readNet(_modelPath, _configPath);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
};
vector<DetectedObject> DnnDetector::Detect(Mat frame)
{
Mat inputTensor;
blobFromImage(frame, inputTensor, scale, Size(width, height), mean, swapRB, false, CV_32F);
net.setInput(inputTensor);
Mat prob = net.forward();
prob = prob.reshape(1, 1);
int n = prob.cols / 7;
prob = prob.reshape(1, n);
vector<DetectedObject> objects;
int cols = frame.cols;
int rows = frame.rows;
for (int i = 0; i < n; i++) {
DetectedObject tmp;
tmp.classid = prob.at<float>(i, 1);
tmp.score = prob.at<float>(i, 2);
tmp.Left = prob.at<float>(i, 3) * cols;
tmp.Bottom = prob.at <float>(i, 4) * rows;
tmp.Right = prob.at<float>(i, 5) * cols;
tmp.Top = prob.at<float>(i, 6) * rows;
objects.push_back(tmp);
return objects;
}
};