aboutsummaryrefslogtreecommitdiff
path: root/samples/ObjectDetection/src/ImageUtils.cpp
blob: 9a3ed17b635773f47c57cf1b47f5e0a5df41228d (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
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ImageUtils.hpp"
#include "BoundingBox.hpp"
#include "Types.hpp"

#include <armnn/Logging.hpp>

static cv::Scalar GetScalarColorCode(std::tuple<int, int, int> color)
{
    return cv::Scalar(std::get<0>(color), std::get<1>(color), std::get<2>(color));
}

void AddInferenceOutputToFrame(od::DetectedObjects& decodedResults, cv::Mat& inputFrame,
                               std::vector<std::tuple<std::string, od::BBoxColor>>& labels)
{
    for(const od::DetectedObject& object : decodedResults)
    {
        int confidence = static_cast<int>(object.GetScore() * 100);
        int baseline = 0;
        std::string textStr;
        std::tuple<int, int, int> colorCode(255, 0, 0); //red

        if (labels.size() > object.GetId())
        {
            auto label = labels[object.GetId()];
            textStr = std::get<0>(label) + " - " + std::to_string(confidence) + "%";
            colorCode = std::get<1>(label).colorCode;
        }
        else
        {
            textStr = std::to_string(object.GetId()) + " - " + std::to_string(confidence) + "%";
        }

        cv::Size textSize = getTextSize(textStr, cv::FONT_HERSHEY_DUPLEX, 1.0, 1, &baseline);

        const od::BoundingBox& bbox = object.GetBoundingBox();

        if (bbox.GetX() + bbox.GetWidth() > inputFrame.cols)
        {
            cv::Rect r(bbox.GetX(), bbox.GetY(), inputFrame.cols - bbox.GetX(), bbox.GetHeight());

            cv::rectangle(inputFrame, r, GetScalarColorCode(colorCode), 2, 8, 0);
        }
        else if (bbox.GetY() + bbox.GetHeight() > inputFrame.rows)
        {
            cv::Rect r(bbox.GetX(), bbox.GetY(), bbox.GetWidth(), inputFrame.rows - bbox.GetY());

            cv::rectangle(inputFrame, r, GetScalarColorCode(colorCode), 2, 8, 0);
        }
        else
        {
            cv::Rect r(bbox.GetX(), bbox.GetY(), bbox.GetWidth(), bbox.GetHeight());

            cv::rectangle(inputFrame, r, GetScalarColorCode(colorCode), 2, 8, 0);
        }

        int textBoxY = std::max(0 ,bbox.GetY() - textSize.height);

        cv::Rect text(bbox.GetX(), textBoxY, textSize.width, textSize.height);

        cv::rectangle(inputFrame, text, GetScalarColorCode(colorCode), -1);

        cv::Scalar color;

        if(std::get<0>(colorCode) + std::get<1>(colorCode) + std::get<2>(colorCode) > 127)
        {
            color = cv::Scalar::all(0);
        }
        else
        {
            color = cv::Scalar::all(255);
        }

        cv::putText(inputFrame,
                    textStr ,
                    cv::Point(bbox.GetX(), textBoxY + textSize.height -(textSize.height)/3),
                    cv::FONT_HERSHEY_DUPLEX,
                    0.5,
                    color,
                    1);
    }
}


void ResizeFrame(const cv::Mat& frame, cv::Mat& dest, const od::Size& aspectRatio)
{
    if(&dest != &frame)
    {
        double longEdgeInput = std::max(frame.rows, frame.cols);
        double longEdgeOutput = std::max(aspectRatio.m_Width, aspectRatio.m_Height);
        const double resizeFactor = longEdgeOutput/longEdgeInput;
        cv::resize(frame, dest, cv::Size(0, 0), resizeFactor, resizeFactor, DefaultResizeFlag);
    }
    else
    {
        const std::string warningMessage{"Resize was not performed because resized frame references the source frame."};
        ARMNN_LOG(warning) << warningMessage;
    }
}

/** Pad a frame with zeros (add rows and columns to the end) */
void PadFrame(const cv::Mat& src, cv::Mat& dest, const int bottom, const int right)
{
    if(&dest != &src)
    {
        cv::copyMakeBorder(src, dest, 0, bottom, 0, right, cv::BORDER_CONSTANT);
    }
    else
    {
        const std::string warningMessage
        {
            "Pad was not performed because destination frame references the source frame."
        };
        ARMNN_LOG(warning) << warningMessage;
    }
}

void ResizeWithPad(const cv::Mat& frame, cv::Mat& dest, cv::Mat& cache, const od::Size& destSize)
{
    ResizeFrame(frame, cache, destSize);
    PadFrame(cache, dest,destSize.m_Height - cache.rows,destSize.m_Width - cache.cols);
}