CommonUtility.cpp 1.31 KB
Newer Older
yaoht's avatar
yaoht committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <CommonUtility.h>

namespace migraphxSamples
{

    bool CompareConfidence(const ResultOfDetection &L, const ResultOfDetection &R)
    {
        return L.confidence > R.confidence;
    }

    bool CompareArea(const ResultOfDetection &L, const ResultOfDetection &R)
    {
        return L.boundingBox.area() > R.boundingBox.area();
    }

    void NMS(std::vector<ResultOfDetection> &detections, float IOUThreshold)
    {
        // sort
        std::sort(detections.begin(), detections.end(), CompareConfidence);

        for (int i = 0; i < detections.size(); ++i)
        {
            if (detections[i].exist)
            {
                for (int j = i + 1; j < detections.size(); ++j)
                {
                    if (detections[j].exist)
                    {
                        // compute IOU
                        float intersectionArea = (detections[i].boundingBox & detections[j].boundingBox).area();
                        float intersectionRate = intersectionArea / (detections[i].boundingBox.area() + detections[j].boundingBox.area() - intersectionArea);

                        if (intersectionRate > IOUThreshold)
                        {
                            detections[j].exist = false;
                        }
                    }
                }
            }
        }
    }

}