Fix yolo decoding while resizer of letter box 48/284048/1
authorTae-Young Chung <ty83.chung@samsung.com>
Wed, 9 Nov 2022 06:17:57 +0000 (15:17 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Wed, 9 Nov 2022 06:17:57 +0000 (15:17 +0900)
Change-Id: If4de2eb113e4b58cb3d2bdc3c6ca19a82d949abf
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
mv_machine_learning/inference/src/Inference.cpp

index d5d3c68..7c4c465 100644 (file)
@@ -1096,13 +1096,35 @@ int Inference::GetObjectDetectionResults(ObjectDetectionResults *results)
                        results->indices.push_back(box.index);
                        results->names.push_back(mUserListName[box.index]);
                        results->confidences.push_back(box.score);
-                       results->locations.push_back(
-                                       cv::Rect(static_cast<int>((box.location.x - box.location.width * 0.5f) *
-                                                                                         static_cast<float>(mSourceSize.width)),
-                                                        static_cast<int>((box.location.y - box.location.height * 0.5f) *
-                                                                                         static_cast<float>(mSourceSize.height)),
-                                                        static_cast<int>(box.location.width * static_cast<float>(mSourceSize.width)),
-                                                        static_cast<int>(box.location.height * static_cast<float>(mSourceSize.height))));
+                       if (mMetadata.GetInputMeta().GetOption().begin()->second.resizer == Resizer::LETTERBOX) {
+                               double srcW = static_cast<double>(mSourceSize.width);
+                               double srcH = static_cast<double>(mSourceSize.height);
+                               double dstW = static_cast<double>(mMetadata.GetInputMeta().GetLayer().begin()->second.getWidth());
+                               double dstH = static_cast<double>(mMetadata.GetInputMeta().GetLayer().begin()->second.getHeight());
+                               double scale = std::min(1.0, std::min(dstW/srcW, dstH/srcH));
+                               double padSize[] = {(dstW - (scale*srcW))/2.0, (dstH - (scale*srcH))/2.0};
+
+                               results->locations.push_back(cv::Rect(
+                                       static_cast<int>(std::min(srcW, std::max(((box.location.x - box.location.width * 0.5f) * dstW - padSize[0])/scale, 0.0))),
+                                       static_cast<int>(std::min(srcH, std::max(((box.location.y - box.location.height * 0.5f) * dstH - padSize[1])/scale, 0.0))),
+                                       static_cast<int>((box.location.width  * dstW)/scale + padSize[0]),
+                                       static_cast<int>((box.location.height * dstH)/scale + padSize[1])
+                               ));
+                               results->locations.back().width = (results->locations.back().x + results->locations.back().width) > srcW ?
+                                                                                               srcW - results->locations.back().x :
+                                                                                               results->locations.back().width;
+                               results->locations.back().height = (results->locations.back().y + results->locations.back().height) > srcH ?
+                                                                                               srcH - results->locations.back().y :
+                                                                                               results->locations.back().height;
+                       } else {
+                               results->locations.push_back(
+                                               cv::Rect(static_cast<int>((box.location.x - box.location.width * 0.5f) *
+                                                                                               static_cast<float>(mSourceSize.width)),
+                                                               static_cast<int>((box.location.y - box.location.height * 0.5f) *
+                                                                                               static_cast<float>(mSourceSize.height)),
+                                                               static_cast<int>(box.location.width * static_cast<float>(mSourceSize.width)),
+                                                               static_cast<int>(box.location.height * static_cast<float>(mSourceSize.height))));
+                       }
                        results->number_of_objects++;
                }