Remove the bounding boxes with low confidence using non-maxima suppression
| 119 | |
| 120 | // Remove the bounding boxes with low confidence using non-maxima suppression |
| 121 | void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector<cv::Mat>& outs, size_t frameId) |
| 122 | { |
| 123 | std::vector<int> classIds; |
| 124 | std::vector<float> confidences; |
| 125 | std::vector<cv::Rect> boxes; |
| 126 | std::vector<int> objectIds; |
| 127 | |
| 128 | for (size_t i = 0; i < outs.size(); ++i) |
| 129 | { |
| 130 | // Scan through all the bounding boxes output from the network and keep only the |
| 131 | // ones with high confidence scores. Assign the box's class label as the class |
| 132 | // with the highest score for the box. |
| 133 | float* data = (float*)outs[i].data; |
| 134 | for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols) |
| 135 | { |
| 136 | cv::Mat scores = outs[i].row(j).colRange(5, outs[i].cols); |
| 137 | cv::Point classIdPoint; |
| 138 | double confidence; |
| 139 | // Get the value and location of the maximum score |
| 140 | cv::minMaxLoc(scores, 0, &confidence, 0, &classIdPoint); |
| 141 | if (confidence > confThreshold) |
| 142 | { |
| 143 | int centerX = (int)(data[0] * frameDims.width); |
| 144 | int centerY = (int)(data[1] * frameDims.height); |
| 145 | int width = (int)(data[2] * frameDims.width); |
| 146 | int height = (int)(data[3] * frameDims.height); |
| 147 | int left = centerX - width / 2; |
| 148 | int top = centerY - height / 2; |
| 149 | |
| 150 | classIds.push_back(classIdPoint.x); |
| 151 | confidences.push_back((float)confidence); |
| 152 | boxes.push_back(cv::Rect(left, top, width, height)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Perform non maximum suppression to eliminate redundant overlapping boxes with |
| 158 | // lower confidences |
| 159 | std::vector<int> indices; |
| 160 | cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); |
| 161 | |
| 162 | // Pass boxes to SORT algorithm |
| 163 | std::vector<cv::Rect> sortBoxes; |
| 164 | for(auto box : boxes) |
| 165 | sortBoxes.push_back(box); |
| 166 | sort.update(sortBoxes, frameId, sqrt(pow(frameDims.width,2) + pow(frameDims.height, 2)), confidences, classIds); |
| 167 | |
| 168 | // Clear data vectors |
| 169 | boxes.clear(); confidences.clear(); classIds.clear(); objectIds.clear(); |
| 170 | // Get SORT predicted boxes |
| 171 | for(auto TBox : sort.frameTrackingResult){ |
| 172 | if(TBox.frame == frameId){ |
| 173 | boxes.push_back(TBox.box); |
| 174 | confidences.push_back(TBox.confidence); |
| 175 | classIds.push_back(TBox.classId); |
| 176 | objectIds.push_back(TBox.id); |
| 177 | } |
| 178 | } |
nothing calls this directly
no test coverage detected