CommonUtility.cpp 1.3 KB
Newer Older
Your Name's avatar
Your Name committed
1
2
3
4
5
#include <CommonUtility.h>

namespace migraphxSamples
{

liucong's avatar
liucong committed
6
bool CompareConfidence(const ResultOfDetection& L, const ResultOfDetection& R)
Your Name's avatar
Your Name committed
7
8
9
10
{
    return L.confidence > R.confidence;
}

liucong's avatar
liucong committed
11
bool CompareArea(const ResultOfDetection& L, const ResultOfDetection& R)
Your Name's avatar
Your Name committed
12
13
14
15
{
    return L.boundingBox.area() > R.boundingBox.area();
}

liucong's avatar
liucong committed
16
void NMS(std::vector<ResultOfDetection>& detections, float IOUThreshold)
Your Name's avatar
Your Name committed
17
18
19
20
{
    // sort
    std::sort(detections.begin(), detections.end(), CompareConfidence);

liucong's avatar
liucong committed
21
    for(int i = 0; i < detections.size(); ++i)
Your Name's avatar
Your Name committed
22
    {
liucong's avatar
liucong committed
23
        if(detections[i].exist)
Your Name's avatar
Your Name committed
24
        {
liucong's avatar
liucong committed
25
            for(int j = i + 1; j < detections.size(); ++j)
Your Name's avatar
Your Name committed
26
            {
liucong's avatar
liucong committed
27
                if(detections[j].exist)
Your Name's avatar
Your Name committed
28
29
                {
                    // compute IOU
liucong's avatar
liucong committed
30
31
32
33
34
                    float intersectionArea =
                        (detections[i].boundingBox & detections[j].boundingBox).area();
                    float intersectionRate =
                        intersectionArea / (detections[i].boundingBox.area() +
                                            detections[j].boundingBox.area() - intersectionArea);
Your Name's avatar
Your Name committed
35

liucong's avatar
liucong committed
36
                    if(intersectionRate > IOUThreshold)
Your Name's avatar
Your Name committed
37
38
39
40
41
42
43
44
45
                    {
                        detections[j].exist = false;
                    }
                }
            }
        }
    }
}

liucong's avatar
liucong committed
46
} // namespace migraphxSamples