aboutsummaryrefslogtreecommitdiff
path: root/samples/ObjectDetection/test/ImageUtilsTest.cpp
blob: 4490cffda96573dba4d058e2c0387c8cf6b3d066 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include <catch.hpp>
#include <opencv2/opencv.hpp>
#include "ImageUtils.hpp"
#include "Types.hpp"

std::vector<std::tuple<int, int>> GetBoundingBoxPoints(std::vector<od::DetectedObject>& decodedResults,
                                                       cv::Mat imageMat)
{
    std::vector<std::tuple<int, int>> bboxes;
    for(const od::DetectedObject& object : decodedResults)
    {
        const od::BoundingBox& bbox = object.GetBoundingBox();

        if (bbox.GetX() + bbox.GetWidth() > imageMat.cols)
        {
            for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y});
            }

            for (int x = bbox.GetX(); x < imageMat.cols; ++x)
            {
                bboxes.emplace_back(std::tuple<int, int>{x, bbox.GetY() + bbox.GetHeight() - 1});
            }

            for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{imageMat.cols - 1, y});
            }
        }
        else if (bbox.GetY() + bbox.GetHeight() > imageMat.rows)
        {
            for (int y = bbox.GetY(); y < imageMat.rows; ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y});
            }

            for (int x = bbox.GetX(); x < bbox.GetX() + bbox.GetWidth(); ++x)
            {
                bboxes.emplace_back(std::tuple<int, int>{x, imageMat.rows - 1});
            }

            for (int y = bbox.GetY(); y < imageMat.rows; ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{bbox.GetX() + bbox.GetWidth() - 1, y});
            }
        }
        else
        {
            for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y});
            }

            for (int x = bbox.GetX(); x < bbox.GetX() + bbox.GetWidth(); ++x)
            {
                bboxes.emplace_back(std::tuple<int, int>{x, bbox.GetY() + bbox.GetHeight() - 1});
            }

            for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y)
            {
                bboxes.emplace_back(std::tuple<int, int>{bbox.GetX() + bbox.GetWidth() - 1, y});
            }
        }
    }
    return bboxes;
}

static std::string GetResourceFilePath(std::string filename)
{
    std::string testResources = TEST_RESOURCE_DIR;
    if (0 == testResources.size())
    {
        throw "Invalid test resources directory provided";
    }
    else
    {
        if(testResources.back() != '/')
        {
            return testResources + "/" + filename;
        }
        else
        {
            return testResources + filename;
        }
    }
}

TEST_CASE("Test Adding Inference output to frame")
{
    //todo: re-write test to use static detections

    std::string testResources = TEST_RESOURCE_DIR;
    REQUIRE(testResources != "");
    std::vector<std::tuple<std::string, common::BBoxColor>> labels;

    common::BBoxColor c
    {
        .colorCode = std::make_tuple (0, 0, 255)
    };

    auto bboxInfo = std::make_tuple ("person", c);
    od::BoundingBox bbox(10, 10, 50, 50);
    od::DetectedObject detection(0, "person", bbox, 0.75);

    labels.push_back(bboxInfo);

    od::DetectedObjects detections;
    cv::Mat frame = cv::imread(GetResourceFilePath("basketball1.png"), cv::IMREAD_COLOR);
    detections.push_back(detection);

    AddInferenceOutputToFrame(detections, frame, labels);

    std::vector<std::tuple<int, int>> bboxes = GetBoundingBoxPoints(detections, frame);

    // Check that every point is the expected color
    for(std::tuple<int, int> tuple : bboxes)
    {
        cv::Point p(std::get<0>(tuple), std::get<1>(tuple));
        CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[0]) == 0);
        CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[1]) == 0);
        CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[2]) == 255);
    }
}