-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSideEarDetection.cpp
More file actions
140 lines (114 loc) · 4.8 KB
/
SideEarDetection.cpp
File metadata and controls
140 lines (114 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <dlib/svm_threaded.h>
#include <dlib/string.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_processing.h>
#include <dlib/data_io.h>
#include <dlib/cmd_line_parser.h>
#include <dlib/image_transforms.h>
#include <iostream>
#include <fstream>
#include "SideEarDetection.h"
std::vector<cv::Rect> detectEar(cv::Mat img, bool sampleflag, bool fliptag){
std::vector<cv::Rect> output_ear_cnt;
const unsigned long upsample_amount =2;
const unsigned long image_batch =1;
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<24> > image_scanner_type_small;
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<6> > image_scanner_type_large;
std::ifstream fin_ear("SideEar.svm", std::ios::binary);
if (fliptag) {
std::cout<<"Flip Image..."<<std::endl;
fin_ear.close();
fin_ear.clear();
fin_ear.open("SideEarFlip.svm", std::ios::binary);
}
if (!fin_ear)
{
std::cout << "Can't find a trained object detector file SideEar.svm. " << std::endl;
std::cout << "You need to train one using the -t option." << std::endl;
std::cout << "\nTry the -h option for more information." << std::endl;
}
dlib::object_detector<image_scanner_type_large> large_ear_detector;
dlib::object_detector<image_scanner_type_small> small_ear_detector;
if(sampleflag==true) dlib::deserialize(small_ear_detector, fin_ear);
else dlib::deserialize(large_ear_detector, fin_ear);
dlib::array2d<dlib::bgr_pixel> cimg;
dlib::assign_image(cimg, dlib::cv_image<dlib::bgr_pixel>(img));
if (sampleflag == true){
for (unsigned long i = 0; i < upsample_amount; ++i){
std::cout<<"Upsample image"<<std::endl;
dlib::pyramid_up(cimg);
}
}
std::vector<dlib::rectangle> rects;
std::vector<dlib::rectangle> ear_rects;
if (sampleflag==true){
ear_rects = small_ear_detector(cimg);
std::cout << "Number of Ear detections: "<< ear_rects.size() << std::endl;
}
else{
ear_rects = large_ear_detector(cimg);
std::cout << "Number of Ear detections: "<< ear_rects.size() << std::endl;
}
for (int i =0; i< ear_rects.size();i++){
auto d = ear_rects.at(i);
cv::Rect c(d.left(), d.top(), d.width(), d.height());
std::cout << d.left() << " "<< d.top()<< " "<< d.width() << " "<< d.height() << std::endl;
output_ear_cnt.push_back(c);
}
std::cout << "Hit enter to compute body size.";
std::cin.get();
return output_ear_cnt;
}
std::vector<cv::Rect> detectNose(cv::Mat img, bool sampleflag, bool fliptag){
std::vector<cv::Rect> output_nose_cnt;
const unsigned long upsample_amount =2;
const unsigned long image_batch =1;
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<24> > image_scanner_type_small;
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<6> > image_scanner_type_large;
std::ifstream fin_nose("SideEyNose.svm", std::ios::binary);
if (fliptag) {
std::cout<<"Flip Image..."<<std::endl;
fin_nose.close();
fin_nose.clear();
fin_nose.open("SideEyNoseFlip.svm", std::ios::binary);
}
if (!fin_nose)
{
std::cout << "Can't find a trained object detector file SideEyNose.svm. " << std::endl;
std::cout << "You need to train one using the -t option." << std::endl;
std::cout << "\nTry the -h option for more information." << std::endl;
}
dlib::object_detector<image_scanner_type_large> large_nose_detector;
dlib::object_detector<image_scanner_type_small> small_nose_detector;
if(sampleflag==true) dlib::deserialize(small_nose_detector, fin_nose);
else dlib::deserialize(large_nose_detector, fin_nose);
dlib::array2d<dlib::bgr_pixel> cimg;
dlib::assign_image(cimg, dlib::cv_image<dlib::bgr_pixel>(img));
if (sampleflag == true){
for (unsigned long i = 0; i < upsample_amount; ++i){
std::cout<<"Upsample image"<<std::endl;
dlib::pyramid_up(cimg);
}
}
std::vector<dlib::rectangle> rects;
if (sampleflag==true){
rects = small_nose_detector(cimg);
std::cout << "Number of Nose detections: "<< rects.size() << std::endl;
}
else{
rects = large_nose_detector(cimg);
std::cout << "Number of Nose detections: "<< rects.size() << std::endl;
}
for (int i =0; i< rects.size();i++){
auto d = rects.at(i);
cv::Rect c(d.left(), d.top(), d.width(), d.height());
std::cout << d.left() << " "<< d.top()<< " "<< d.width() << " "<< d.height() << std::endl;
output_nose_cnt.push_back(c);
}
std::cout << "Hit enter to compute body size.";
std::cin.get();
return output_nose_cnt;
}