Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include <catch.hpp> |
| 6 | #include <opencv2/opencv.hpp> |
| 7 | #include "ImageUtils.hpp" |
| 8 | #include "Types.hpp" |
| 9 | |
| 10 | std::vector<std::tuple<int, int>> GetBoundingBoxPoints(std::vector<od::DetectedObject>& decodedResults, |
| 11 | cv::Mat imageMat) |
| 12 | { |
| 13 | std::vector<std::tuple<int, int>> bboxes; |
| 14 | for(const od::DetectedObject& object : decodedResults) |
| 15 | { |
| 16 | const od::BoundingBox& bbox = object.GetBoundingBox(); |
| 17 | |
| 18 | if (bbox.GetX() + bbox.GetWidth() > imageMat.cols) |
| 19 | { |
| 20 | for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y) |
| 21 | { |
| 22 | bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y}); |
| 23 | } |
| 24 | |
| 25 | for (int x = bbox.GetX(); x < imageMat.cols; ++x) |
| 26 | { |
| 27 | bboxes.emplace_back(std::tuple<int, int>{x, bbox.GetY() + bbox.GetHeight() - 1}); |
| 28 | } |
| 29 | |
| 30 | for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y) |
| 31 | { |
| 32 | bboxes.emplace_back(std::tuple<int, int>{imageMat.cols - 1, y}); |
| 33 | } |
| 34 | } |
| 35 | else if (bbox.GetY() + bbox.GetHeight() > imageMat.rows) |
| 36 | { |
| 37 | for (int y = bbox.GetY(); y < imageMat.rows; ++y) |
| 38 | { |
| 39 | bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y}); |
| 40 | } |
| 41 | |
| 42 | for (int x = bbox.GetX(); x < bbox.GetX() + bbox.GetWidth(); ++x) |
| 43 | { |
| 44 | bboxes.emplace_back(std::tuple<int, int>{x, imageMat.rows - 1}); |
| 45 | } |
| 46 | |
| 47 | for (int y = bbox.GetY(); y < imageMat.rows; ++y) |
| 48 | { |
| 49 | bboxes.emplace_back(std::tuple<int, int>{bbox.GetX() + bbox.GetWidth() - 1, y}); |
| 50 | } |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y) |
| 55 | { |
| 56 | bboxes.emplace_back(std::tuple<int, int>{bbox.GetX(), y}); |
| 57 | } |
| 58 | |
| 59 | for (int x = bbox.GetX(); x < bbox.GetX() + bbox.GetWidth(); ++x) |
| 60 | { |
| 61 | bboxes.emplace_back(std::tuple<int, int>{x, bbox.GetY() + bbox.GetHeight() - 1}); |
| 62 | } |
| 63 | |
| 64 | for (int y = bbox.GetY(); y < bbox.GetY() + bbox.GetHeight(); ++y) |
| 65 | { |
| 66 | bboxes.emplace_back(std::tuple<int, int>{bbox.GetX() + bbox.GetWidth() - 1, y}); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return bboxes; |
| 71 | } |
| 72 | |
| 73 | static std::string GetResourceFilePath(std::string filename) |
| 74 | { |
| 75 | std::string testResources = TEST_RESOURCE_DIR; |
| 76 | if (0 == testResources.size()) |
| 77 | { |
| 78 | throw "Invalid test resources directory provided"; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | if(testResources.back() != '/') |
| 83 | { |
| 84 | return testResources + "/" + filename; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | return testResources + filename; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | TEST_CASE("Test Adding Inference output to frame") |
| 94 | { |
| 95 | //todo: re-write test to use static detections |
| 96 | |
| 97 | std::string testResources = TEST_RESOURCE_DIR; |
| 98 | REQUIRE(testResources != ""); |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 99 | std::vector<std::tuple<std::string, common::BBoxColor>> labels; |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 100 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 101 | common::BBoxColor c |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 102 | { |
| 103 | .colorCode = std::make_tuple (0, 0, 255) |
| 104 | }; |
| 105 | |
| 106 | auto bboxInfo = std::make_tuple ("person", c); |
| 107 | od::BoundingBox bbox(10, 10, 50, 50); |
| 108 | od::DetectedObject detection(0, "person", bbox, 0.75); |
| 109 | |
| 110 | labels.push_back(bboxInfo); |
| 111 | |
| 112 | od::DetectedObjects detections; |
| 113 | cv::Mat frame = cv::imread(GetResourceFilePath("basketball1.png"), cv::IMREAD_COLOR); |
| 114 | detections.push_back(detection); |
| 115 | |
| 116 | AddInferenceOutputToFrame(detections, frame, labels); |
| 117 | |
| 118 | std::vector<std::tuple<int, int>> bboxes = GetBoundingBoxPoints(detections, frame); |
| 119 | |
| 120 | // Check that every point is the expected color |
| 121 | for(std::tuple<int, int> tuple : bboxes) |
| 122 | { |
| 123 | cv::Point p(std::get<0>(tuple), std::get<1>(tuple)); |
| 124 | CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[0]) == 0); |
| 125 | CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[1]) == 0); |
| 126 | CHECK(static_cast<int>(frame.at<cv::Vec3b>(p)[2]) == 255); |
| 127 | } |
| 128 | } |