diff --git a/src/CVObjectDetection.cpp b/src/CVObjectDetection.cpp index 07d63e8d9..277a9cc40 100644 --- a/src/CVObjectDetection.cpp +++ b/src/CVObjectDetection.cpp @@ -166,14 +166,15 @@ void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector std::vector indices; cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); + // Pass boxes to SORT algorithm std::vector sortBoxes; for(auto box : boxes) sortBoxes.push_back(box); sort.update(sortBoxes, frameId, sqrt(pow(frameDims.width,2) + pow(frameDims.height, 2)), confidences, classIds); - - sortBoxes.clear(); boxes.clear(); confidences.clear(); classIds.clear(); - + // Clear data vectors + boxes.clear(); confidences.clear(); classIds.clear(); + // Get SORT predicted boxes for(auto TBox : sort.frameTrackingResult){ if(TBox.frame == frameId){ boxes.push_back(TBox.box); @@ -181,55 +182,58 @@ void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector classIds.push_back(TBox.classId); } } - - // for(int i = 0; i > rectAndClasses; - // for(int i=0; i bboxes; - // rectAndClasses[classIds[i]] = bboxes; - // } - - // rectAndClasses[classIds[i]].push_back(boxes[i]); - // } - - // for(std::map >::iterator it = rectAndClasses.begin(); it != rectAndClasses.end(); it++){ - // if(sort.find(it->first) == sort.end()){ - // SortTracker classTracker; - // sort[it->first] = classTracker; - // } - // sort[it->first].update(it->second, frameId, sqrt(pow(frameDims.width,2) + pow(frameDims.height, 2))); - // } - - // classIds.clear(); boxes.clear(); confidences.clear(); - // for(std::map::iterator it = sort.begin(); it != sort.end(); it++){ - // for(auto TBox : it->second.frameTrackingResult){ - // boxes.push_back(TBox.box); - // classIds.push_back(it->first); - // confidences.push_back(1); - // } - // } + // Remove boxes based on controids distance + for(uint i = 0; i= confidences[j]){ + boxes.erase(boxes.begin() + j); + classIds.erase(classIds.begin() + j); + confidences.erase(confidences.begin() + j); + break; + } + else{ + boxes.erase(boxes.begin() + i); + classIds.erase(classIds.begin() + i); + confidences.erase(confidences.begin() + i); + i = 0; + break; + } + } + } + } + } + // Remove boxes based in IOU score + for(uint i = 0; i= confidences[j]){ + boxes.erase(boxes.begin() + j); + classIds.erase(classIds.begin() + j); + confidences.erase(confidences.begin() + j); + break; + } + else{ + boxes.erase(boxes.begin() + i); + classIds.erase(classIds.begin() + i); + confidences.erase(confidences.begin() + i); + i = 0; + break; + } + } + } + } + } + // Normalize boxes coordinates std::vector> normalized_boxes; for(auto box : boxes){ cv::Rect_ normalized_box; @@ -243,25 +247,26 @@ void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector detectionsData[frameId] = CVDetectionData(classIds, confidences, normalized_boxes, frameId); } +// Compute IOU between 2 boxes bool CVObjectDetection::iou(cv::Rect pred_box, cv::Rect sort_box){ - // determine the (x, y)-coordinates of the intersection rectangle + // Determine the (x, y)-coordinates of the intersection rectangle int xA = std::max(pred_box.x, sort_box.x); int yA = std::max(pred_box.y, sort_box.y); int xB = std::min(pred_box.x + pred_box.width, sort_box.x + sort_box.width); int yB = std::min(pred_box.y + pred_box.height, sort_box.y + sort_box.height); - // compute the area of intersection rectangle + // Compute the area of intersection rectangle int interArea = std::max(0, xB - xA + 1) * std::max(0, yB - yA + 1); - // compute the area of both the prediction and ground-truth - // rectangles + + // Compute the area of both the prediction and ground-truth rectangles int boxAArea = (pred_box.width + 1) * (pred_box.height + 1); int boxBArea = (sort_box.width + 1) * (sort_box.height + 1); - // compute the intersection over union by taking the intersection - // area and dividing it by the sum of prediction + ground-truth - // areas - the interesection area + + // Compute the intersection over union by taking the intersection float iou = interArea / (float)(boxAArea + boxBArea - interArea); - if(iou > 0.75) + // If IOU is above this value the boxes are very close (probably a variation of the same bounding box) + if(iou > 0.5) return true; return false; } diff --git a/src/effects/ObjectDetection.cpp b/src/effects/ObjectDetection.cpp index 545db9450..00370be81 100644 --- a/src/effects/ObjectDetection.cpp +++ b/src/effects/ObjectDetection.cpp @@ -174,6 +174,9 @@ bool ObjectDetection::LoadObjDetectdData(std::string inputFilePath){ classNames.clear(); detectionsData.clear(); + // Seed to generate same random numbers + std::srand(1); + // Get all classes names and assign a color to them for(int i = 0; i < objMessage.classnames_size(); i++){ classNames.push_back(objMessage.classnames(i)); classesColor.push_back(cv::Scalar(std::rand()%205 + 50, std::rand()%205 + 50, std::rand()%205 + 50));