20 #include <boost/algorithm/string.hpp> 26 : classification_result(nullptr),
27 detection_result(nullptr)
29 if (!cnn_type.compare(
"tinyyolo_v1") || !cnn_type.compare(
"mobilenetssd"))
50 int img_width,
int img_height)
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;
61 for (
int i = 0; i < grid_height; i++)
63 for (
int j = 0; j < grid_width; j++)
65 for (
int k = 0; k < bbox_num; k++)
67 int index = i * grid_width * bbox_num + j * bbox_num + k;
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));
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);
88 0 : (y_center - 0.5 * obj_in_bbox.
bbox.
height);
89 objs_in_bboxes->push_back(obj_in_bbox);
102 for (
auto item : *objs_in_bboxes)
109 int img_width,
int img_height)
111 constexpr
int num_in_group = 7;
112 int num_detection = result.at(0);
115 for (
int i = 0; i < num_detection; i++)
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;
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)
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);
151 for (
auto item : *objs_in_bboxes)
179 constexpr
float iou_threshold = 0.5;
185 std::sort(objs_in_bboxes->begin(), objs_in_bboxes->end(), cmp);
187 for (
auto iter1 = objs_in_bboxes->begin(); iter1 != objs_in_bboxes->end(); iter1++)
189 if (iter1->item.probability == 0)
193 for (
auto iter2 = iter1 + 1; iter2 != objs_in_bboxes->end(); iter2++)
195 if (
IOU(*iter1, *iter2) > iou_threshold)
197 iter2->item.probability = 0;
202 for (
auto iter = objs_in_bboxes->begin(); iter != objs_in_bboxes->end(); )
204 if (iter->item.probability == 0)
206 iter = objs_in_bboxes->erase(iter);
223 int inter_w = xmax - xmin;
224 int inter_h = ymax - ymin;
226 if (inter_w > 0 && inter_h > 0)
228 inter_area = inter_w * inter_h;
std::shared_ptr< DetectionResult > DetectionResultPtr
void NMS(ItemInBBoxArrayPtr objs_in_bboxes)
void setDetectionResult(ItemInBBox item)
void setClassificationResult(Item item)
DetectionResultPtr getDetectionResult()
std::shared_ptr< ItemInBBoxArray > ItemInBBoxArrayPtr
void parseSSDResult(const std::vector< float > &result, const std::vector< std::string > categories, int img_width, int img_height)
DetectionResultPtr detection_result
Result(const std::string &cnn_type)
ClassificationResultPtr classification_result
std::shared_ptr< ClassificationResult > ClassificationResultPtr
void parseYoloResult(const std::vector< float > &result, const std::vector< std::string > categories, int img_width, int img_height)
ClassificationResultPtr getClassificationResult()
float IOU(ItemInBBox box1, ItemInBBox box2)