|
| 1 | +/* |
| 2 | + * This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License as published by the |
| 6 | + * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | + * option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | + * more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "zm_catch2.h" |
| 19 | + |
| 20 | +#include <cstdio> |
| 21 | +#include <fstream> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "zm_object_classes.h" |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +// Write a .names file beside a notional model so loadFromFile() finds it. |
| 29 | +std::string WriteNames(const std::string &stem, const std::string &contents) { |
| 30 | + const std::string names = stem + ".names"; |
| 31 | + std::ofstream out(names); |
| 32 | + out << contents; |
| 33 | + out.close(); |
| 34 | + return stem + ".nb"; // what loadFromFile is handed |
| 35 | +} |
| 36 | + |
| 37 | +} // namespace |
| 38 | + |
| 39 | +TEST_CASE("ObjectClasses::boxColorForName maps the COCO groupings", "[objectclasses]") { |
| 40 | + // These are the groupings the old index-based mapping encoded. Resolving them |
| 41 | + // by name has to give exactly the same answers for COCO. |
| 42 | + SECTION("person is blue") { |
| 43 | + REQUIRE(ObjectClasses::boxColorForName("person") == kRGBBlue); |
| 44 | + } |
| 45 | + |
| 46 | + SECTION("vehicles are green") { |
| 47 | + for (const char *name : {"bicycle", "car", "motorcycle", "airplane", |
| 48 | + "bus", "train", "truck", "boat"}) { |
| 49 | + REQUIRE(ObjectClasses::boxColorForName(name) == kRGBGreen); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + SECTION("animals are orange") { |
| 54 | + for (const char *name : {"bird", "cat", "dog", "horse", "sheep", |
| 55 | + "cow", "elephant", "bear", "zebra", "giraffe"}) { |
| 56 | + REQUIRE(ObjectClasses::boxColorForName(name) == kRGBOrange); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + SECTION("anything else is red") { |
| 61 | + REQUIRE(ObjectClasses::boxColorForName("backpack") == kRGBRed); |
| 62 | + REQUIRE(ObjectClasses::boxColorForName("gun") == kRGBRed); |
| 63 | + REQUIRE(ObjectClasses::boxColorForName("") == kRGBRed); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +TEST_CASE("ObjectClasses::boxColorFor agrees with the index mapping for COCO", |
| 68 | + "[objectclasses]") { |
| 69 | + // A default-constructed instance carries COCO, so the new name-based lookup |
| 70 | + // must reproduce the legacy index-based one across every class. This is the |
| 71 | + // regression guard: the refactor may not change existing behaviour. |
| 72 | + ObjectClasses coco; |
| 73 | + for (size_t i = 0; i < coco.size(); i++) { |
| 74 | + const int id = static_cast<int>(i); |
| 75 | + REQUIRE(coco.boxColorFor(id) == ObjectClasses::getDetectionBoxColor(id)); |
| 76 | + REQUIRE(std::string(coco.colorStringFor(id)) == |
| 77 | + std::string(ObjectClasses::getDetectionColorString(id))); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +TEST_CASE("ObjectClasses::boxColorFor does not give a custom model COCO's colours", |
| 82 | + "[objectclasses]") { |
| 83 | + /* The bug this fixes: a firearm model's class 0 is "gun", but the index-based |
| 84 | + * mapping only knows COCO, where 0 is "person" - so a weapon was drawn in the |
| 85 | + * blue reserved for people. |
| 86 | + */ |
| 87 | + const std::string model = WriteNames("/tmp/zm_objclasses_firearm", "gun\nrifle\nknife\n"); |
| 88 | + |
| 89 | + ObjectClasses classes; |
| 90 | + REQUIRE(classes.loadFromFile(model)); |
| 91 | + REQUIRE(classes.size() == 3); |
| 92 | + REQUIRE(classes.getClassName(0) == "gun"); |
| 93 | + |
| 94 | + // The legacy index lookup still claims blue for class 0 ... |
| 95 | + REQUIRE(ObjectClasses::getDetectionBoxColor(0) == kRGBBlue); |
| 96 | + // ... but resolving through this model's own names does not. |
| 97 | + REQUIRE(classes.boxColorFor(0) == kRGBRed); |
| 98 | + REQUIRE(classes.boxColorFor(1) == kRGBRed); |
| 99 | + REQUIRE(classes.boxColorFor(2) == kRGBRed); |
| 100 | + REQUIRE(std::string(classes.colorStringFor(0)) == "red"); |
| 101 | + |
| 102 | + std::remove("/tmp/zm_objclasses_firearm.names"); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_CASE("ObjectClasses::boxColorFor still honours names a custom model shares with COCO", |
| 106 | + "[objectclasses]") { |
| 107 | + // A custom model that reorders COCO-ish names must colour by meaning, not by |
| 108 | + // position: "person" is blue wherever it lands. |
| 109 | + const std::string model = WriteNames("/tmp/zm_objclasses_mixed", "gun\nperson\ncar\n"); |
| 110 | + |
| 111 | + ObjectClasses classes; |
| 112 | + REQUIRE(classes.loadFromFile(model)); |
| 113 | + REQUIRE(classes.boxColorFor(0) == kRGBRed); // gun |
| 114 | + REQUIRE(classes.boxColorFor(1) == kRGBBlue); // person, at index 1 not 0 |
| 115 | + REQUIRE(classes.boxColorFor(2) == kRGBGreen); // car, at index 2 not 2-of-COCO |
| 116 | + |
| 117 | + std::remove("/tmp/zm_objclasses_mixed.names"); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_CASE("ObjectClasses::loadFromFile falls back to COCO when no .names exists", |
| 121 | + "[objectclasses]") { |
| 122 | + ObjectClasses classes; |
| 123 | + REQUIRE_FALSE(classes.loadFromFile("/tmp/zm_objclasses_definitely_absent.nb")); |
| 124 | + REQUIRE(classes.size() == 80); |
| 125 | + REQUIRE(classes.getClassName(0) == "person"); |
| 126 | + REQUIRE(classes.boxColorFor(0) == kRGBBlue); |
| 127 | +} |
0 commit comments