Skip to content
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
36 changes: 36 additions & 0 deletions include/classificator-Nikita-Khloptsev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#pragma once
#include <iostream>
#include <fstream>
#include <string>

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

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

class Classificator
{
public:
vector<string> classesNames;
virtual Mat Classify(Mat image) = 0;
};
class DnnClassificator : public Classificator {
private:
Net net;
string model;
string config;
string labels;
double scale;
float inWidth;
float inHeight;
Scalar mean;
bool swapRB;

public:
DnnClassificator(string model, string config, string labels, double scale, float inWidth, float inHeight, Scalar mean, bool swapRB);
Mat Classify(Mat image);
};
12 changes: 12 additions & 0 deletions include/detectedobject-Nikita-Khloptsev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <string>

struct DetectedObject
{
int Left;
int Right;
int Top;
int Bottom;
float confidence;
int classID;
};
34 changes: 34 additions & 0 deletions include/detector-Nikita-Khloptsev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include <iostream>
#include <string>

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

#include "detectedobject.h"

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

class Detector{
public:
virtual vector<DetectedObject> Detect(Mat image) = 0;
};

class DnnDetector : public Detector{
private:
string model;
string config;
string label;
int width;
int height;
double scale;
Scalar mean;
bool swapRB;
Net net;
public:
DnnDetector(string model, string config, string label, int width, int height, double scale, Scalar mean, bool swapRB);
vector<DetectedObject> Detect (Mat frame);
};
33 changes: 33 additions & 0 deletions samples/practice1-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <string>

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include "filter.h"
#import "filter.cpp"

using namespace cv;
using namespace std;


int main()
{
// Load image
GrayFilter gr;
int w, h;
cv::Mat src, out1, out2;
src = imread("/home/augustinmay/Downloads/pic.jpg");
// Filter image
cout<<"Enter new size(width, height): ";
cin>>w>>h;
ResizeFilter res(w,h);
out1 = res.ProcessImage(src);
out2 = gr.ProcessImage(src);
// Show image
cv::imshow("source image", src);
cv::imshow("filtered image", out2);
cv::imshow("resized image", out1);
waitKey(0);
return 0;
}
51 changes: 51 additions & 0 deletions samples/practice2-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include "classificator.h"
#include "classificator.cpp"

using namespace cv;
using namespace std;


int main(){
// Load image and init parameters
string labels_1000[1000];
ifstream inp("/home/augustinmay/CLionProjects/practice-2/image_net.txt");
int count=0;
string str;
while (std::getline(inp, str)){
labels_1000[count] = str;
count++;
}
Mat image = imread("/home/augustinmay/Downloads/space-shuttle.jpg");
string model = "/home/augustinmay/Downloads/squeezenet1_1_caffemodel.caffemodel";
string config = "/home/augustinmay/Downloads/squeezenet1_1_prototxt.prototxt";
string labels = "/home/augustinmay/CV-SUMMER-CAMP/data/squeezenet1.1.labels";;
double scale=1;
int width = 224;
int heigth = 224;
Scalar mean = {104, 117, 123};
bool swap = false;
//Image classification
Classificator* dnnClassificator = new DnnClassificator(model, config, labels, scale, width, heigth, mean, swap);
Mat prob = dnnClassificator->Classify(image);
Point classIdPoint;
double confidence;
minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
int classId = classIdPoint.x;
string text1 = "label: " + labels_1000[classId], text2 = "with confidence: " +to_string(confidence*100)+ " %";
putText(image, text1, Point(5, 25), FONT_HERSHEY_DUPLEX, 0.7, Scalar(255, 255, 255), 5);
putText(image, text1, Point(5, 25), FONT_HERSHEY_DUPLEX, 0.7, Scalar(0, 0, 0), 2);
putText(image, text2, Point(5, 50), FONT_HERSHEY_DUPLEX, 0.7, Scalar(255, 255, 255), 5);
putText(image, text2, Point(5, 50), FONT_HERSHEY_DUPLEX, 0.7, Scalar(0, 0, 0), 2);
//Show result
imshow("My image: ", image);
waitKey(0);
return 0;
}
55 changes: 55 additions & 0 deletions samples/practice3-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <string>
#include <iostream>
#include <fstream>

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

#include "detector.h"
#include "detector.cpp"

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

int main() {
// Parse command line arguments.

// Load image and init parameters
Mat image = imread("/home/augustinmay/Downloads/plane.jpg");
String model="/home/augustinmay/Downloads/mobilenet-ssd/mobilenet-ssd.caffemodel";
String config="/home/augustinmay/Downloads/mobilenet-ssd/mobilenet-ssd.prototxt";
String label="/home/augustinmay/Downloads/mobilenet-ssd/object_detection_classes.txt";
int width = 300;
int heigth = 300;
double scale = 1.0 / 127.5;
Scalar mean = Scalar(127.5, 127.5, 127.5);
bool swapRB = false;
string labels[20];
ifstream inp(label);
int count=0;
string str;
while (std::getline(inp, str)){
labels[count] = str;
count++;
}
//Image detection
Detector *det = new DnnDetector(model, config, label, width, heigth, scale, mean, swapRB);
vector<DetectedObject> objects = det->Detect(image);
//Show result
for (int i = 0; i < objects.size(); i++){
if((objects[i].classID-1)==-1){
break;
}
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 text1 = "label: " + labels[objects[i].classID-1]+" with confidence: " +to_string(objects[i].confidence*100)+ " %";
putText(image, text1, point1+Point(0, -10), FONT_HERSHEY_DUPLEX, 0.7, Scalar(255, 255, 255), 5);
putText(image, text1, point1+Point(0, -10), FONT_HERSHEY_DUPLEX, 0.7, Scalar(0, 0, 0), 2);
}
imshow("Objects", image);
waitKey(0);
return 0;
}
23 changes: 23 additions & 0 deletions src/classificator-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "classificator.h"

DnnClassificator::DnnClassificator(string model, string config, string labels, double scale, float inWidth, float inHeight, Scalar mean, bool swapRB) {
this->model = model;
this->config = config;
this->labels = labels;
this->scale = scale;
this->inWidth = inWidth;
this->inHeight = inHeight;
this->mean = mean;
this->swapRB = swapRB;
net = readNet(model, config);
net.setPreferableBackend(0);
net.setPreferableTarget(0);
}
Mat DnnClassificator::Classify(Mat image) {
Size check_size = Size(inWidth, inHeight);
Mat Tensor;
blobFromImage(image, Tensor, scale, check_size, mean, swapRB, false, CV_32F);
net.setInput(Tensor);
Mat prob = net.forward();
return prob;
}
37 changes: 37 additions & 0 deletions src/detector-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "detector.h"

DnnDetector::DnnDetector(string model, string config, string label, int width, int height, double scale, Scalar mean, bool swapRB){
this->model = model;
this->config = config;
this->label = label;
this->width = width;
this->height = height;
this->scale = scale;
this->mean = mean;
this->swapRB = swapRB;
net = readNet(model, config);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
};
vector<DetectedObject> DnnDetector::Detect(Mat frame){
Mat Tensor;
blobFromImage(frame, Tensor, scale, Size(width, height), mean, swapRB, false, CV_32F);
net.setInput(Tensor);
Mat prob = net.forward().reshape(1, 1);
int obj_num = prob.cols / 7;
prob = prob.reshape(1, obj_num);
vector<DetectedObject> objects;
int cols = frame.cols;
int rows = frame.rows;
for (int i = 0; i < obj_num; i++){
DetectedObject temp;
temp.classID = prob.at<float>(i, 1);
temp.confidence = prob.at<float>(i, 2);
temp.Left = prob.at<float>(i, 3) * cols;
temp.Bottom = prob.at <float>(i, 4) * rows;
temp.Right = prob.at<float>(i, 5) * cols;
temp.Top = prob.at<float>(i, 6) * rows;
objects.push_back(temp);
}
return objects;
};
20 changes: 20 additions & 0 deletions src/filter-Nikita-Khloptsev.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "filter.h"

Mat GrayFilter::ProcessImage(Mat image) {
Mat grayed;
cvtColor(image, grayed, COLOR_BGR2GRAY);
return grayed;

}

ResizeFilter::ResizeFilter(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
}

Mat ResizeFilter::ProcessImage(Mat image) {
cv::Size new_size(width, height);
Mat changed;
resize(image, changed, new_size);
return changed;
}