Skip to content

Commit 02d9717

Browse files
committed
support SPRING_EDITION_BOX
1 parent fdd603e commit 02d9717

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed
Binary file not shown.

FasterRCNN_SE_Detection_Example/FasterRCNN_SE.h

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* FasterRCNN_SE.hpp
33
* FasterRCNN_SpringEdition
44
*
@@ -14,13 +14,38 @@
1414
#include<sstream>
1515
#include<Windows.h>
1616
#include<opencv2/opencv.hpp>
17-
class FasterRCNN {
17+
#ifndef SPRING_EDITION_BOX
18+
#define SPRING_EDITION_BOX
19+
/**
20+
* @brief 이 클래스는 cv::Rect를 확장한 것으로 클래스값과 스코어값이 추가되었습니다.
21+
* @author kimbomm
22+
* @date 2017-10-05
23+
*
24+
* @see https://github.com/springkim/FasterRCNN_SpringEdition
25+
* @see https://github.com/springkim/YOLOv2_SpringEdition
26+
*/
27+
class BoxSE : public cv::Rect {
1828
public:
19-
class Box : public cv::Rect{
20-
public:
21-
int m_class;
22-
float m_score;
23-
};
29+
int m_class = -1;
30+
float m_score = 0.0F;
31+
std::string m_class_name;
32+
BoxSE() {
33+
m_class_name = "Unknown";
34+
}
35+
BoxSE(int c, float s, int _x, int _y, int _w, int _h, std::string name = "")
36+
:m_class(c), m_score(s) {
37+
this->x = _x;
38+
this->y = _y;
39+
this->width = _w;
40+
this->height = _h;
41+
char* lb[5] = { "th","st","nd","rd","th" };
42+
if (name.length() == 0) {
43+
m_class_name = std::to_string(m_class) + lb[m_class < 4 ? m_class : 4] + " class";
44+
}
45+
}
46+
};
47+
#endif
48+
class FasterRCNN {
2449
private:
2550
std::string m_key_mutex = "";
2651
HANDLE m_mutex = INVALID_HANDLE_VALUE;
@@ -33,8 +58,9 @@ class FasterRCNN {
3358
//2(50) = detect
3459
//3(51) = receive
3560
//9(57) = terminate signal
61+
std::vector<std::string> m_class_map;
3662
public:
37-
void Create(std::string model_path,DWORD size=6000) {
63+
void Create(std::string model_path,std::string classfile,DWORD size=6000) {
3864
m_key_shmem = this->GetKey() + "_shmem";
3965
m_key_mutex = this->GetKey() + "_mutex";
4066
m_size = size;
@@ -60,6 +86,24 @@ class FasterRCNN {
6086
}
6187
::ReleaseMutex(m_mutex);
6288
}
89+
std::fstream fin;
90+
fin.open(classfile, std::ios::in);
91+
if (fin.is_open() == false) {
92+
::MessageBoxA(nullptr, "Can't read class map file", "Error", MB_OK);
93+
exit(1);
94+
}
95+
std::string line;
96+
while (!fin.eof()) {
97+
std::getline(fin, line);
98+
if (line.length() == 0) {
99+
break;
100+
}
101+
std::istringstream iss(line);
102+
std::string _class;
103+
iss >> _class;
104+
m_class_map.push_back(_class);
105+
}
106+
fin.close();
63107
}
64108
void Release() {
65109
bool pass = false;
@@ -82,7 +126,7 @@ class FasterRCNN {
82126
this->Release();
83127
}
84128
}
85-
std::vector<Box> Detect(std::string img_path, float threshold) {
129+
std::vector<BoxSE> Detect(std::string img_path, float threshold) {
86130
::ReleaseMutex(m_mutex);
87131
bool pass = false;
88132
while (pass == false) {
@@ -121,10 +165,11 @@ class FasterRCNN {
121165
result.push_back(atoi(str.c_str()));
122166
offset += str.length() + 1;
123167
}
124-
std::vector<Box> boxes;
168+
std::vector<BoxSE> boxes;
125169
for (int i = 0; i < result[0]; i++) {
126-
Box box;
170+
BoxSE box;
127171
box.m_class = result[i * 6 + 1];
172+
box.m_class_name = m_class_map[box.m_class];
128173
box.m_score = result[i * 6 + 2] / 10000.0F;
129174
box.x= result[i * 6 + 3];
130175
box.y = result[i * 6 + 4];
@@ -134,15 +179,15 @@ class FasterRCNN {
134179
boxes.push_back(box);
135180
}
136181
}
137-
auto IOU = [](Box& a, Box& b)->float {
182+
auto IOU = [](BoxSE& a, BoxSE& b)->float {
138183
float i = static_cast<float>((a & b).area());
139184
float u = a.area() + b.area() - i;
140185
return i / u;
141186
};
142187
//Sort by Greater
143-
std::sort(boxes.begin(), boxes.end(), [](Box& a, Box& b)->bool {return a.m_score > b.m_score; });
188+
std::sort(boxes.begin(), boxes.end(), [](BoxSE& a, BoxSE& b)->bool {return a.m_score > b.m_score; });
144189
std::vector<bool> select(boxes.size(), true);
145-
std::vector<Box> boxes2;
190+
std::vector<BoxSE> boxes2;
146191
for (int i = 0; i < boxes.size(); i++) {
147192
if (select[i] == true) {
148193
for (int j = i + 1; j < boxes.size(); j++) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
powershell "(New-Object System.Net.WebClient).DownloadFile('https://www.dropbox.com/s/v7wn51rkq3mro8g/faster_rcnn_eval_VGG16_e2e.model?dl=1','faster_rcnn_eval_VGG16_e2e.model')"
1+
powershell "(New-Object System.Net.WebClient).DownloadFile('https://www.dropbox.com/s/v7wn51rkq3mro8g/faster_rcnn_eval_VGG16_e2e.model?dl=1','faster_rcnn_eval_VGG16_e2e.model')"
2+
powershell "(New-Object System.Net.WebClient).DownloadFile('https://www.dropbox.com/s/eb3vykxbpym7tb1/class_map.txt?dl=1','class_map.txt')"

FasterRCNN_SE_Detection_Example/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66

77
int main() {
88
std::string model_path = "faster_rcnn_eval_VGG16_e2e.model";
9+
std::string class_map_path = "class_map.txt";
910
FasterRCNN detector;
1011

11-
detector.Create(model_path);
12+
detector.Create(model_path, class_map_path);
1213

1314
std::string img_file = "0067.jpg";
1415

15-
std::vector<FasterRCNN::Box> boxes = detector.Detect(img_file, 0.9F);
16+
std::vector<BoxSE> boxes = detector.Detect(img_file, 0.9F);
1617
cv::Mat img = cv::imread(img_file);
1718
for (auto&box : boxes) {
1819
std::cout << box.m_class << std::endl;
1920
std::cout << box.m_score << std::endl;
20-
std::cout << "[" << box.x << "," << box.y << "," << box.width << "," << box.height << "]" << std::endl;
21+
std::cout << box.m_class_name << "[" << box.x << "," << box.y << "," << box.width << "," << box.height << "]" << std::endl;
2122
cv::rectangle(img, box, cv::Scalar(0, 0, 255), 2);
2223
}
2324
cv::imshow("result", img);

0 commit comments

Comments
 (0)