MLECO-2874: Move NMS out of the OD use_case

* Add ImageUtils
* Move image related code from UseCaseCommonUtils to ImageUtils
* Move NMS related code to ImageUtils
* Delete test specific ImageUtils and use new ImageUtils

Signed-off-by: Richard Burton <richard.burton@arm.com>
Change-Id: Icbf5dd9c6a941b0126ecdf69a0c9d9969f22729f
diff --git a/source/use_case/ad/src/UseCaseHandler.cc b/source/use_case/ad/src/UseCaseHandler.cc
index 420e6d4..853ab08 100644
--- a/source/use_case/ad/src/UseCaseHandler.cc
+++ b/source/use_case/ad/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 #include "hal.h"
 #include "AdMelSpectrogram.hpp"
 #include "AudioUtils.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "AdPostProcessing.hpp"
 #include "log_macros.h"
diff --git a/source/use_case/asr/src/UseCaseHandler.cc b/source/use_case/asr/src/UseCaseHandler.cc
index afcb6e4..7bce2c6 100644
--- a/source/use_case/asr/src/UseCaseHandler.cc
+++ b/source/use_case/asr/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 #include "hal.h"
 #include "Wav2LetterMfcc.hpp"
 #include "AudioUtils.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "AsrResult.hpp"
 #include "Wav2LetterPreprocess.hpp"
diff --git a/source/use_case/img_class/src/UseCaseHandler.cc b/source/use_case/img_class/src/UseCaseHandler.cc
index fafc6b9..1f1d78b 100644
--- a/source/use_case/img_class/src/UseCaseHandler.cc
+++ b/source/use_case/img_class/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,6 +19,7 @@
 #include "Classifier.hpp"
 #include "InputFiles.hpp"
 #include "MobileNetModel.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "hal.h"
 #include "log_macros.h"
@@ -136,7 +137,7 @@
             arm::app::DumpTensor(outputTensor);
 #endif /* VERIFY_TEST_OUTPUT */
 
-            if (!image::PresentInferenceResult(platform, results)) {
+            if (!PresentInferenceResult(platform, results)) {
                 return false;
             }
 
diff --git a/source/use_case/kws/src/UseCaseHandler.cc b/source/use_case/kws/src/UseCaseHandler.cc
index c2d2ea4..8dd7724 100644
--- a/source/use_case/kws/src/UseCaseHandler.cc
+++ b/source/use_case/kws/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 #include "hal.h"
 #include "MicroNetKwsMfcc.hpp"
 #include "AudioUtils.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "KwsResult.hpp"
 #include "log_macros.h"
diff --git a/source/use_case/kws_asr/src/UseCaseHandler.cc b/source/use_case/kws_asr/src/UseCaseHandler.cc
index bfc1d25..d598de6 100644
--- a/source/use_case/kws_asr/src/UseCaseHandler.cc
+++ b/source/use_case/kws_asr/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,6 +19,7 @@
 #include "hal.h"
 #include "InputFiles.hpp"
 #include "AudioUtils.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "MicroNetKwsModel.hpp"
 #include "MicroNetKwsMfcc.hpp"
diff --git a/source/use_case/noise_reduction/src/UseCaseHandler.cc b/source/use_case/noise_reduction/src/UseCaseHandler.cc
index 0c5984c..792b460 100644
--- a/source/use_case/noise_reduction/src/UseCaseHandler.cc
+++ b/source/use_case/noise_reduction/src/UseCaseHandler.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@
 #include "UseCaseHandler.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "AudioUtils.hpp"
+#include "ImageUtils.hpp"
 #include "InputFiles.hpp"
 #include "RNNoiseModel.hpp"
 #include "RNNoiseProcess.hpp"
diff --git a/source/use_case/object_detection/include/DetectorPostProcessing.hpp b/source/use_case/object_detection/include/DetectorPostProcessing.hpp
index 3e9c819..5393f89 100644
--- a/source/use_case/object_detection/include/DetectorPostProcessing.hpp
+++ b/source/use_case/object_detection/include/DetectorPostProcessing.hpp
@@ -18,6 +18,7 @@
 #define DETECTOR_POST_PROCESSING_HPP
 
 #include "UseCaseCommonUtils.hpp"
+#include "ImageUtils.hpp"
 #include "DetectionResult.hpp"
 #include "YoloFastestModel.hpp"
 
@@ -45,20 +46,6 @@
         int topN;
     };
 
-
-    struct Box {
-        float x;
-        float y;
-        float w;
-        float h;
-    };
-
-    struct Detection {
-        Box bbox;
-        std::vector<float> prob;
-        float objectness;
-    };
-
     /**
      * @brief   Helper class to manage tensor post-processing for "object_detection"
      *          output.
@@ -99,18 +86,11 @@
         int   m_topN;       /* TopN */
 
         /**
-         * @brief       Calculate the Sigmoid function of the give value.
-         * @param[in]   x   Value.
-         * @return      Sigmoid value of the input.
-         **/
-        float Sigmoid(float x);
-
-        /**
          * @brief       Insert the given Detection in the list.
          * @param[in]   detections   List of detections.
          * @param[in]   det          Detection to be inserted.
          **/
-        void InsertTopNDetections(std::forward_list<Detection>& detections, Detection& det);
+        void InsertTopNDetections(std::forward_list<image::Detection>& detections, image::Detection& det);
 
         /**
          * @brief        Given a Network calculate the detection boxes.
@@ -124,49 +104,7 @@
                              int imageWidth,
                              int imageHeight,
                              float threshold,
-                             std::forward_list<Detection>& detections);
-
-        /**
-         * @brief       Calculate the 1D overlap.
-         * @param[in]   x1Center   First center point.
-         * @param[in]   width1     First width.
-         * @param[in]   x2Center   Second center point.
-         * @param[in]   width2     Second width.
-         * @return      The overlap between the two lines.
-         **/
-        float Calculate1DOverlap(float x1Center, float width1, float x2Center, float width2);
-
-        /**
-         * @brief       Calculate the intersection between the two given boxes.
-         * @param[in]   box1   First box.
-         * @param[in]   box2   Second box.
-         * @return      The intersection value.
-         **/
-        float CalculateBoxIntersect(Box& box1, Box& box2);
-
-        /**
-         * @brief       Calculate the union between the two given boxes.
-         * @param[in]   box1   First box.
-         * @param[in]   box2   Second box.
-         * @return      The two given boxes union value.
-         **/
-        float CalculateBoxUnion(Box& box1, Box& box2);
-        /**
-         * @brief       Calculate the intersection over union between the two given boxes.
-         * @param[in]   box1   First box.
-         * @param[in]   box2   Second box.
-         * @return      The intersection over union value.
-         **/
-        float CalculateBoxIOU(Box& box1, Box& box2);
-
-        /**
-         * @brief       Calculate the Non-Maxima suppression on the given detection boxes.
-         * @param[in]   detections    Detection boxes.
-         * @param[in]   classes       Number of classes.
-         * @param[in]   iouThreshold  Intersection over union threshold.
-         * @return      true or false based on execution success.
-         **/
-        void  CalculateNMS(std::forward_list<Detection>& detections, int classes, float iouThreshold);
+                             std::forward_list<image::Detection>& detections);
 
         /**
          * @brief       Draw on the given image a bounding box starting at (boxX, boxY).
diff --git a/source/use_case/object_detection/src/DetectorPostProcessing.cc b/source/use_case/object_detection/src/DetectorPostProcessing.cc
index edfb137..e97e6b3 100644
--- a/source/use_case/object_detection/src/DetectorPostProcessing.cc
+++ b/source/use_case/object_detection/src/DetectorPostProcessing.cc
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include "DetectorPostProcessing.hpp"
+#include "PlatformMath.hpp"
 
 #include <algorithm>
 #include <cmath>
@@ -75,7 +76,7 @@
     int originalImageWidth = originalImageSize;
     int originalImageHeight = originalImageSize;
 
-    std::forward_list<Detection> detections;
+    std::forward_list<image::Detection> detections;
     GetNetworkBoxes(net, originalImageWidth, originalImageHeight, m_threshold, detections);
 
     /* Do nms */
@@ -124,15 +125,11 @@
     }
 }
 
-float DetectorPostprocessing::Sigmoid(float x)
-{
-    return 1.f/(1.f + exp(-x));
-}
 
-void DetectorPostprocessing::InsertTopNDetections(std::forward_list<Detection>& detections, Detection& det)
+void DetectorPostprocessing::InsertTopNDetections(std::forward_list<image::Detection>& detections, image::Detection& det)
 {
-    std::forward_list<Detection>::iterator it;
-    std::forward_list<Detection>::iterator last_it;
+    std::forward_list<image::Detection>::iterator it;
+    std::forward_list<image::Detection>::iterator last_it;
     for ( it = detections.begin(); it != detections.end(); ++it ) {
         if(it->objectness > det.objectness)
             break;
@@ -144,11 +141,11 @@
     }
 }
 
-void DetectorPostprocessing::GetNetworkBoxes(Network& net, int imageWidth, int imageHeight, float threshold, std::forward_list<Detection>& detections)
+void DetectorPostprocessing::GetNetworkBoxes(Network& net, int imageWidth, int imageHeight, float threshold, std::forward_list<image::Detection>& detections)
 {
     int numClasses = net.numClasses;
     int num = 0;
-    auto det_objectness_comparator = [](Detection& pa, Detection& pb) {
+    auto det_objectness_comparator = [](image::Detection& pa, image::Detection& pb) {
         return pa.objectness < pb.objectness;
     };
     for (size_t i = 0; i < net.branches.size(); ++i) {
@@ -162,10 +159,10 @@
 
                     /* Objectness score */
                     int bbox_obj_offset = h * width * channel + w * channel + anc * (numClasses + 5) + 4;
-                    float objectness = Sigmoid(((float)net.branches[i].modelOutput[bbox_obj_offset] - net.branches[i].zeroPoint) * net.branches[i].scale);
+                    float objectness = math::MathUtils::SigmoidF32(((float)net.branches[i].modelOutput[bbox_obj_offset] - net.branches[i].zeroPoint) * net.branches[i].scale);
 
                     if(objectness > threshold) {
-                        Detection det;
+                        image::Detection det;
                         det.objectness = objectness;
                         /* Get bbox prediction data for each anchor, each feature point */
                         int bbox_x_offset = bbox_obj_offset -4;
@@ -183,8 +180,8 @@
                         float bbox_x, bbox_y;
 
                         /* Eliminate grid sensitivity trick involved in YOLOv4 */
-                        bbox_x = Sigmoid(det.bbox.x);
-                        bbox_y = Sigmoid(det.bbox.y);
+                        bbox_x = math::MathUtils::SigmoidF32(det.bbox.x);
+                        bbox_y = math::MathUtils::SigmoidF32(det.bbox.y);
                         det.bbox.x = (bbox_x + w) / width;
                         det.bbox.y = (bbox_y + h) / height;
 
@@ -192,7 +189,7 @@
                         det.bbox.h = exp(det.bbox.h) * net.branches[i].anchor[anc*2+1] / net.inputHeight;
 
                         for (int s = 0; s < numClasses; s++) {
-                            float sig = Sigmoid(((float)net.branches[i].modelOutput[bbox_scores_offset + s] - net.branches[i].zeroPoint) * net.branches[i].scale)*objectness;
+                            float sig = math::MathUtils::SigmoidF32(((float)net.branches[i].modelOutput[bbox_scores_offset + s] - net.branches[i].zeroPoint) * net.branches[i].scale)*objectness;
                             det.prob.emplace_back((sig > threshold) ? sig : 0);
                         }
 
@@ -221,81 +218,6 @@
         num -=1;
 }
 
-float DetectorPostprocessing::Calculate1DOverlap(float x1Center, float width1, float x2Center, float width2)
-{
-    float left_1 = x1Center - width1/2;
-    float left_2 = x2Center - width2/2;
-    float leftest = left_1 > left_2 ? left_1 : left_2;
-
-    float right_1 = x1Center + width1/2;
-    float right_2 = x2Center + width2/2;
-    float rightest = right_1 < right_2 ? right_1 : right_2;
-
-    return rightest - leftest;
-}
-
-float DetectorPostprocessing::CalculateBoxIntersect(Box& box1, Box& box2)
-{
-    float width = Calculate1DOverlap(box1.x, box1.w, box2.x, box2.w);
-    if (width < 0) {
-        return 0;
-    }
-    float height = Calculate1DOverlap(box1.y, box1.h, box2.y, box2.h);
-    if (height < 0) {
-        return 0;
-    }
-
-    float total_area = width*height;
-    return total_area;
-}
-
-float DetectorPostprocessing::CalculateBoxUnion(Box& box1, Box& box2)
-{
-    float boxes_intersection = CalculateBoxIntersect(box1, box2);
-    float boxes_union = box1.w * box1.h + box2.w * box2.h - boxes_intersection;
-    return boxes_union;
-}
-
-
-float DetectorPostprocessing::CalculateBoxIOU(Box& box1, Box& box2)
-{
-    float boxes_intersection = CalculateBoxIntersect(box1, box2);
-    if (boxes_intersection == 0) {
-        return 0;
-    }
-
-    float boxes_union = CalculateBoxUnion(box1, box2);
-    if (boxes_union == 0) {
-        return 0;
-    }
-
-    return boxes_intersection / boxes_union;
-}
-
-void DetectorPostprocessing::CalculateNMS(std::forward_list<Detection>& detections, int classes, float iouThreshold)
-{
-    int idxClass{0};
-    auto CompareProbs = [idxClass](Detection& prob1, Detection& prob2) {
-        return prob1.prob[idxClass] > prob2.prob[idxClass];
-    };
-
-    for (idxClass = 0; idxClass < classes; ++idxClass) {
-        detections.sort(CompareProbs);
-
-        for (std::forward_list<Detection>::iterator it=detections.begin(); it != detections.end(); ++it) {
-            if (it->prob[idxClass] == 0) continue;
-            for (std::forward_list<Detection>::iterator itc=std::next(it, 1); itc != detections.end(); ++itc) {
-                if (itc->prob[idxClass] == 0) {
-                    continue;
-                }
-                if (CalculateBoxIOU(it->bbox, itc->bbox) > iouThreshold) {
-                    itc->prob[idxClass] = 0;
-                }
-            }
-        }
-    }
-}
-
 void DetectorPostprocessing::DrawBoxOnImage(uint8_t* imgIn, int imWidth, int imHeight, int boxX,int boxY, int boxWidth, int boxHeight)
 {
     auto CheckAndFixOffset = [](int im_width,int im_height,int& offset) {
diff --git a/source/use_case/vww/src/UseCaseHandler.cc b/source/use_case/vww/src/UseCaseHandler.cc
index 01011e2..a47f191 100644
--- a/source/use_case/vww/src/UseCaseHandler.cc
+++ b/source/use_case/vww/src/UseCaseHandler.cc
@@ -18,6 +18,7 @@
 #include "VisualWakeWordModel.hpp"
 #include "Classifier.hpp"
 #include "InputFiles.hpp"
+#include "ImageUtils.hpp"
 #include "UseCaseCommonUtils.hpp"
 #include "hal.h"
 #include "log_macros.h"
@@ -144,7 +145,7 @@
             arm::app::DumpTensor(outputTensor);
 #endif /* VERIFY_TEST_OUTPUT */
 
-            if (!image::PresentInferenceResult(platform, results)) {
+            if (!PresentInferenceResult(platform, results)) {
                 return false;
             }