1- /*
1+ /*
22* FasterRCNN_SE.hpp
33* FasterRCNN_SpringEdition
44*
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 {
1828public:
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 {
2449private:
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;
3662public:
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++) {
0 commit comments