result.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 #include <string>
19 #include <vector>
20 #include <boost/algorithm/string.hpp>
22 
23 namespace movidius_ncs_lib
24 {
25 Result::Result(const std::string& cnn_type)
26  : classification_result(nullptr),
27  detection_result(nullptr)
28 {
29  if (!cnn_type.compare("tinyyolo_v1") || !cnn_type.compare("mobilenetssd"))
30  {
31  detection_result = std::make_shared<DetectionResult>();
32  }
33  else
34  {
35  classification_result = std::make_shared<ClassificationResult>();
36  }
37 }
38 
40 {
41  return classification_result;
42 }
43 
45 {
46  return detection_result;
47 }
48 
49 void Result::parseYoloResult(const std::vector<float>& result, const std::vector<std::string> categories,
50  int img_width, int img_height)
51 {
52  constexpr int grid_width = 7;
53  constexpr int grid_height = 7;
54  constexpr int bbox_num = 2;
55  constexpr float prob_threshold = 0.2;
56  constexpr int bbox_conf_num = grid_width * grid_height * bbox_num;
57  int class_num = categories.size();
58  int prob_num = grid_width * grid_height * class_num;
59  ItemInBBoxArrayPtr objs_in_bboxes = std::make_shared<ItemInBBoxArray>();
60 
61  for (int i = 0; i < grid_height; i++)
62  {
63  for (int j = 0; j < grid_width; j++)
64  {
65  for (int k = 0; k < bbox_num; k++)
66  {
67  int index = i * grid_width * bbox_num + j * bbox_num + k;
68  ItemInBBox obj_in_bbox;
69 
70  std::vector<float> probs(result.begin() + index / 2 * class_num,
71  result.begin() + index / 2 * class_num + class_num);
72  float scale = result[prob_num + index];
73  std::vector<float>::iterator max_iter = std::max_element(std::begin(probs), std::end(probs));
74  obj_in_bbox.item.probability = *max_iter * scale;
75 
76  if (obj_in_bbox.item.probability > prob_threshold)
77  {
78  obj_in_bbox.item.category = categories[std::distance(std::begin(probs), max_iter)];
79  int x_center = ((result[prob_num + bbox_conf_num + index * 4] + j) / 7.0) * img_width;
80  int y_center = ((result[prob_num + bbox_conf_num + index * 4 + 1] + i) / 7.0) * img_height;
81  obj_in_bbox.bbox.width = (result[prob_num + bbox_conf_num + index * 4 + 2])
82  * (result[prob_num + bbox_conf_num + index * 4 + 2]) * img_width;
83  obj_in_bbox.bbox.height = (result[prob_num + bbox_conf_num + index * 4 + 3])
84  * (result[prob_num + bbox_conf_num + index * 4 + 3]) * img_height;
85  obj_in_bbox.bbox.x = (x_center - 0.5 * obj_in_bbox.bbox.width) < 0?
86  0 : (x_center - 0.5 * obj_in_bbox.bbox.width);
87  obj_in_bbox.bbox.y = (y_center - 0.5 * obj_in_bbox.bbox.height) < 0?
88  0 : (y_center - 0.5 * obj_in_bbox.bbox.height);
89  objs_in_bboxes->push_back(obj_in_bbox);
90  }
91  }
92  }
93  }
94 
95  NMS(objs_in_bboxes);
96 
97  if (!detection_result->items_in_boxes.empty())
98  {
99  detection_result->items_in_boxes.clear();
100  }
101 
102  for (auto item : *objs_in_bboxes)
103  {
104  detection_result->items_in_boxes.push_back(item);
105  }
106 }
107 
108 void Result::parseSSDResult(const std::vector<float>& result, const std::vector<std::string> categories,
109  int img_width, int img_height)
110 {
111  constexpr int num_in_group = 7;
112  int num_detection = result.at(0);
113  ItemInBBoxArrayPtr objs_in_bboxes = std::make_shared<ItemInBBoxArray>();
114 
115  for (int i = 0; i < num_detection; i++)
116  {
117  int category_id = result[(i + 1) * num_in_group + 1];
118  float probability = result[(i + 1) * num_in_group + 2];
119  int xmin = result[(i + 1) * num_in_group + 3] * img_width;
120  int ymin = result[(i + 1) * num_in_group + 4] * img_height;
121  int xmax = result[(i + 1) * num_in_group + 5] * img_width;
122  int ymax = result[(i + 1) * num_in_group + 6] * img_height;
123 
124  ItemInBBox obj_in_bbox;
125  if (std::isnan(category_id) || std::isnan(probability) || std::isnan(xmin)
126  || xmin < 0 || xmin > img_width || std::isnan(ymin) || ymin < 0 || ymin > img_height
127  || std::isnan(xmax) || xmax < 0 || xmax > img_width || std::isnan(ymax)
128  || ymax < 0 || ymax > img_height)
129  {
130  continue;
131  }
132  else
133  {
134  obj_in_bbox.item.category = categories.at(category_id);
135  obj_in_bbox.item.probability = probability;
136  obj_in_bbox.bbox.width = xmax - xmin;
137  obj_in_bbox.bbox.height = ymax - ymin;
138  obj_in_bbox.bbox.x = xmin;
139  obj_in_bbox.bbox.y = ymin;
140  objs_in_bboxes->push_back(obj_in_bbox);
141  }
142  }
143 
144  NMS(objs_in_bboxes);
145 
146  if (!detection_result->items_in_boxes.empty())
147  {
148  detection_result->items_in_boxes.clear();
149  }
150 
151  for (auto item : *objs_in_bboxes)
152  {
153  detection_result->items_in_boxes.push_back(item);
154  }
155 }
156 
158 {
159  classification_result->items.push_back(item);
160 }
161 
163 {
164  classification_result->time_taken = time;
165 }
166 
168 {
169  detection_result->items_in_boxes.push_back(item);
170 }
171 
173 {
174  detection_result->time_taken = time;
175 }
176 
177 void Result::NMS(ItemInBBoxArrayPtr objs_in_bboxes)
178 {
179  constexpr float iou_threshold = 0.5;
180 
181  auto cmp = [](const ItemInBBox &a, const ItemInBBox &b)
182  {
183  return a.item.probability > b.item.probability;
184  };
185  std::sort(objs_in_bboxes->begin(), objs_in_bboxes->end(), cmp);
186 
187  for (auto iter1 = objs_in_bboxes->begin(); iter1 != objs_in_bboxes->end(); iter1++)
188  {
189  if (iter1->item.probability == 0)
190  {
191  continue;
192  }
193  for (auto iter2 = iter1 + 1; iter2 != objs_in_bboxes->end(); iter2++)
194  {
195  if (IOU(*iter1, *iter2) > iou_threshold)
196  {
197  iter2->item.probability = 0;
198  }
199  }
200  }
201 
202  for (auto iter = objs_in_bboxes->begin(); iter != objs_in_bboxes->end(); )
203  {
204  if (iter->item.probability == 0)
205  {
206  iter = objs_in_bboxes->erase(iter);
207  }
208  else
209  {
210  iter++;
211  }
212  }
213 }
214 
216 {
217  int xmax = (box1.bbox.x + box1.bbox.width < box2.bbox.x + box2.bbox.width)?
218  box1.bbox.x + box1.bbox.width : box2.bbox.x + box2.bbox.width;
219  int xmin = (box1.bbox.x > box2.bbox.x)? box1.bbox.x : box2.bbox.x;
220  int ymax = (box1.bbox.y + box1.bbox.height < box2.bbox.y + box2.bbox.height)?
221  box1.bbox.y + box1.bbox.height : box2.bbox.y + box2.bbox.height;
222  int ymin = (box1.bbox.y > box2.bbox.y)? box1.bbox.y : box2.bbox.y;
223  int inter_w = xmax - xmin;
224  int inter_h = ymax - ymin;
225  int inter_area = 0;
226  if (inter_w > 0 && inter_h > 0)
227  {
228  inter_area = inter_w * inter_h;
229  }
230  return inter_area * 1.0 / (box1.bbox.width * box1.bbox.height + box2.bbox.width * box2.bbox.height - inter_area);
231 }
232 } // namespace movidius_ncs_lib
std::shared_ptr< DetectionResult > DetectionResultPtr
Definition: result.h:64
void NMS(ItemInBBoxArrayPtr objs_in_bboxes)
Definition: result.cpp:177
void setDetectionResult(ItemInBBox item)
Definition: result.cpp:167
void setClassificationResult(Item item)
Definition: result.cpp:157
DetectionResultPtr getDetectionResult()
Definition: result.cpp:44
std::shared_ptr< ItemInBBoxArray > ItemInBBoxArrayPtr
Definition: result.h:49
void parseSSDResult(const std::vector< float > &result, const std::vector< std::string > categories, int img_width, int img_height)
Definition: result.cpp:108
DetectionResultPtr detection_result
Definition: result.h:90
Result(const std::string &cnn_type)
Definition: result.cpp:25
std::string category
Definition: result.h:28
ClassificationResultPtr classification_result
Definition: result.h:89
std::shared_ptr< ClassificationResult > ClassificationResultPtr
Definition: result.h:63
void parseYoloResult(const std::vector< float > &result, const std::vector< std::string > categories, int img_width, int img_height)
Definition: result.cpp:49
ClassificationResultPtr getClassificationResult()
Definition: result.cpp:39
float IOU(ItemInBBox box1, ItemInBBox box2)
Definition: result.cpp:215


movidius_ncs_lib
Author(s): Xiaojun Huang
autogenerated on Mon Jun 10 2019 14:11:23