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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* unit tests for MeterOCR.cpp
* Author: Matthias Behr, 2015
*/
#include <json-c/json.h>
#include <protocols/MeterOCR.hpp>
#include <test_config.hpp>
#include "gtest/gtest.h"
class MeterOCR_Test {
public:
static void test_calcImpulses();
static void test_roundBasedOnSmallerDigits();
};
TEST(MeterOCR, basic2_needle_autofix) {
std::list<Option> options;
options.emplace_back("file", ocrTestImage("img2.png"));
options.push_back(
Option("rotate", -2.0)); // rotate by -2deg (counterclockwise)// 661, 539, 585/680
struct json_object *jso = json_tokener_parse("[{\"type\": \"needle\", \"boundingboxes\":[\
{\"identifier\": \"water cons\", \"scaler\":-1,\"digit\":false, \"circle\": {\"cx\": 689, \"cy\": 449, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-2,\"digit\":false, \"circle\": {\"cx\": 661, \"cy\": 539, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-3,\"digit\":false, \"circle\": {\"cx\": 574, \"cy\": 576, \"cr\": 24}, \"confidence_id\": \"conf_circle\"},\
{\"identifier\": \"water cons\", \"scaler\":-4,\"digit\":true, \"circle\": {\"cx\": 488, \"cy\": 542, \"cr\": 24}}\
]}]"); // should detect 0,3767
options.push_back(Option("recognizer", jso));
json_object_put(jso);
jso = json_tokener_parse("\
{\"range\": 20, \"x\": 465, \"y\":395}\
");
options.push_back(Option("autofix", jso));
json_object_put(jso);
MeterOCR m(options);
ASSERT_EQ(SUCCESS, m.open());
std::vector<Reading> rds;
rds.resize(2);
EXPECT_EQ(2, m.read(rds, 2));
double value = rds[0].value();
EXPECT_TRUE(std::abs(0.3767 - value) < 0.00001);
value = rds[1].value();
EXPECT_GT(value, 80.0); // expect conf. >80
ASSERT_EQ(0, m.close());
}
TEST(MeterOCR, basic2_needle_autofix_impulses) {
std::list<Option> options;
options.emplace_back("file", ocrTestImage("img2_blue.png"));
options.push_back(Option("generate_debug_image", false));
options.push_back(Option("impulses", 10000));
options.push_back(
Option("rotate", -2.0)); // rotate by -2deg (counterclockwise)// 661, 539, 585/680
struct json_object *jso =
json_tokener_parse("[{\"type\": \"needle\",\"kernelColorString\":\"-2 -2 2 0 0 0 0 0 0\",\
\"boundingboxes\":[\
{\"identifier\": \"water cons\", \"scaler\":-1,\"digit\":false, \"circle\": {\"cx\": 689, \"cy\": 449, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-2,\"digit\":false, \"circle\": {\"cx\": 661, \"cy\": 539, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-3,\"digit\":false, \"circle\": {\"cx\": 574, \"cy\": 576, \"cr\": 24}, \"confidence_id\": \"conf_circle\"},\
{\"identifier\": \"water cons\", \"scaler\":-4,\"digit\":true, \"circle\": {\"cx\": 488, \"cy\": 542, \"cr\": 24}}\
],\
}]"); // should detect 0,3767
options.push_back(Option("recognizer", jso));
json_object_put(jso);
jso = json_tokener_parse("\
{\"range\": 20, \"x\": 465, \"y\":395}\
");
options.push_back(Option("autofix", jso));
json_object_put(jso);
MeterOCR m(options);
ASSERT_EQ(SUCCESS, m.open());
std::vector<Reading> rds;
rds.resize(2);
EXPECT_EQ(0, m.read(rds, 2));
m.set_forced_file_changed();
// expect 0 readings on first read
ASSERT_EQ(2, m.read(rds, 2));
// expect 2 on 2nd read
double value = rds[0].value(); // but with same pic no impulses. // TODO add interface to change
// file between reads
EXPECT_TRUE(std::abs(value) < 0.00001);
value = rds[1].value();
EXPECT_GT(value, 80.0); // expect conf. >80
ASSERT_EQ(0, m.close());
}
TEST(MeterOCR, basic2_needle_autofix_impulses_debug_image) {
std::list<Option> options;
options.emplace_back("file", ocrTestImage("img2.png"));
options.push_back(Option("generate_debug_image", true));
options.push_back(Option("impulses", 10000));
options.push_back(
Option("rotate", -2.0)); // rotate by -2deg (counterclockwise)// 661, 539, 585/680
struct json_object *jso = json_tokener_parse("[{\"type\": \"needle\", \"boundingboxes\":[\
{\"identifier\": \"water cons\", \"scaler\":-1,\"digit\":false, \"circle\": {\"cx\": 689, \"cy\": 449, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-2,\"digit\":false, \"circle\": {\"cx\": 661, \"cy\": 539, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-3,\"digit\":false, \"circle\": {\"cx\": 574, \"cy\": 576, \"cr\": 24}, \"confidence_id\": \"conf_circle\"},\
{\"identifier\": \"water cons\", \"scaler\":-4,\"digit\":true, \"circle\": {\"cx\": 488, \"cy\": 542, \"cr\": 24}}\
]}]"); // should detect 0,3767
options.push_back(Option("recognizer", jso));
json_object_put(jso);
jso = json_tokener_parse("\
{\"range\": 20, \"x\": 465, \"y\":395}\
");
options.push_back(Option("autofix", jso));
json_object_put(jso);
MeterOCR m(options);
ASSERT_EQ(SUCCESS, m.open());
std::vector<Reading> rds;
rds.resize(2);
EXPECT_EQ(0, m.read(rds, 2));
m.set_forced_file_changed();
// expect 0 readings on first read
EXPECT_EQ(2, m.read(rds, 2));
// expect 2 on 2nd read
double value = rds[0].value(); // but with same pic no impulses. // TODO add interface to change
// file between reads
EXPECT_TRUE(std::abs(value) < 0.00001);
value = rds[1].value();
EXPECT_GT(value, 80.0); // expect conf. >80
ASSERT_EQ(0, m.close());
}
void MeterOCR_Test::test_calcImpulses() {
std::list<Option> options;
options.emplace_back("file", ocrTestImage("img2.png"));
options.push_back(Option("impulses", 10));
struct json_object *jso = json_tokener_parse("[{\"type\": \"needle\", \"boundingboxes\":[\
{\"identifier\": \"water cons\", \"scaler\":-1,\"digit\":false, \"circle\": {\"cx\": 689, \"cy\": 449, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-2,\"digit\":false, \"circle\": {\"cx\": 661, \"cy\": 539, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-3,\"digit\":false, \"circle\": {\"cx\": 574, \"cy\": 576, \"cr\": 24}, \"confidence_id\": \"conf_circle\"},\
{\"identifier\": \"water cons\", \"scaler\":-4,\"digit\":true, \"circle\": {\"cx\": 488, \"cy\": 542, \"cr\": 24}}\
]}]"); // should detect 0,3767
options.push_back(Option("recognizer", jso));
MeterOCR m(options);
// let's test calcImpulses:
ASSERT_EQ(0, m.calcImpulses(0.1, 0.1));
ASSERT_EQ(0, m.calcImpulses(-0.1, -0.1));
ASSERT_EQ(0, m.calcImpulses(1001.2, 1001.2));
// max impulses:
ASSERT_EQ(5, m.calcImpulses(5.5, 5.0));
ASSERT_EQ(-5, m.calcImpulses(5.0, 5.49999));
// wrap detection:
ASSERT_EQ(4, m.calcImpulses(
0.2, 0.8)); // without wrap det. it would be negative (new value < old value)
// multiple wraps:
ASSERT_EQ(4, m.calcImpulses(0.2, 10.8));
ASSERT_EQ(-4, m.calcImpulses(10.8, 0.2));
// now with impulses <10 (i.e. no wrap detection)
m._impulses = 5;
ASSERT_EQ(5, m.calcImpulses(6.0, 5.0));
ASSERT_EQ(-5, m.calcImpulses(5.0, 6.0));
ASSERT_EQ(-3, m.calcImpulses(0.2, 0.8)); // with wrap it's +5
}
TEST(MeterOCR, calcImpulses) { MeterOCR_Test::test_calcImpulses(); }
void MeterOCR_Test::test_roundBasedOnSmallerDigits() {
struct json_object *jso = json_tokener_parse("\
{\"boundingboxes\": [{\"identifier\":\"1\", \"circle\":{\"cx\":20, \"cy\":20, \"cr\":10}}]\
}");
MeterOCR::RecognizerNeedle r(jso);
json_object_put(jso);
int conf = 100;
// no rounding
ASSERT_EQ(1, r.roundBasedOnSmallerDigits(1, 1.0, 0.0, conf));
EXPECT_EQ(100, conf);
// round up:
ASSERT_EQ(5, r.roundBasedOnSmallerDigits(4, 4.99, 0.0, conf));
EXPECT_EQ(90, conf);
// round down:
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(5, 5.00, 0.99, conf));
EXPECT_EQ(75, conf);
// round down:
conf += 15;
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(5, 5.4, 0.91, conf));
EXPECT_EQ(75, conf);
// round up wrap:
ASSERT_EQ(0, r.roundBasedOnSmallerDigits(9, 9.99, 0.0, conf));
EXPECT_EQ(65, conf);
// round down wrap:
ASSERT_EQ(9, r.roundBasedOnSmallerDigits(0, 0.00, 0.99, conf));
EXPECT_EQ(50, conf);
conf += 15;
ASSERT_EQ(9, r.roundBasedOnSmallerDigits(0, 0.4, 0.91, conf));
EXPECT_EQ(50, conf);
// stay:
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(4, 4.5, 0.5, conf));
EXPECT_EQ(50, conf);
ASSERT_EQ(0, r.roundBasedOnSmallerDigits(0, 0.4, 0.90, conf));
EXPECT_EQ(50, conf);
// stay:
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(4, 4.5, 0.1, conf));
EXPECT_EQ(50, conf);
// stay:
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(4, 4.5, 0.9, conf));
EXPECT_EQ(50, conf);
// stay:
ASSERT_EQ(4, r.roundBasedOnSmallerDigits(4, 4.9, 0.9, conf));
EXPECT_EQ(50, conf);
}
TEST(MeterOCR, roundBasedOnSmallerDigits) { MeterOCR_Test::test_roundBasedOnSmallerDigits(); }
TEST(MeterOCR, debouncing) {
ASSERT_EQ(8, debounce(9, 8.49));
ASSERT_EQ(9, debounce(9, 8.51));
ASSERT_EQ(9, debounce(9, 8.99));
ASSERT_EQ(9, debounce(9, 9.00));
ASSERT_EQ(9, debounce(9, 9.49));
ASSERT_EQ(9, debounce(9, 9.50));
ASSERT_EQ(9, debounce(9, 9.51));
ASSERT_EQ(9, debounce(9, 9.99));
// ASSERT_EQ(9, debounce(9, 10.0)); // should never happen
ASSERT_EQ(9, debounce(9, 0.49));
ASSERT_EQ(0, debounce(9, 0.51));
ASSERT_EQ(9, debounce(0, 9.49));
ASSERT_EQ(0, debounce(0, 9.51));
for (int i = 0; i < 9; ++i) {
ASSERT_EQ(i, debounce(i, (double)i + 0.5));
ASSERT_EQ(i, debounce(i, (double)i + 1.0));
ASSERT_EQ(i + 1, debounce(i, (double)i + 1.501));
ASSERT_EQ(i + 1, debounce(i + 1, (double)(i + 1) - 0.4999));
ASSERT_EQ(i, debounce(i + 1, (double)(i + 1) - 0.51));
}
};
TEST(MeterOCR,
basic2_needle_v4l) // TODO remove this test as it depends on a test machine with v4l2 dev
{
std::list<Option> options;
options.push_back(Option("v4l2_dev", (char *)"/dev/video0"));
options.push_back(
Option("rotate", -2.0)); // rotate by -2deg (counterclockwise)// 661, 539, 585/680
struct json_object *jso = json_tokener_parse("[{\"type\": \"needle\", \"boundingboxes\":[\
{\"identifier\": \"water cons\", \"scaler\":-1,\"digit\":false, \"circle\": {\"cx\": 689, \"cy\": 449, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-2,\"digit\":false, \"circle\": {\"cx\": 661, \"cy\": 539, \"cr\": 24}},\
{\"identifier\": \"water cons\", \"scaler\":-3,\"digit\":false, \"circle\": {\"cx\": 574, \"cy\": 576, \"cr\": 24}, \"confidence_id\": \"conf_circle\"},\
{\"identifier\": \"water cons\", \"scaler\":-4,\"digit\":true, \"circle\": {\"cx\": 488, \"cy\": 542, \"cr\": 24}}\
]}]"); // should detect 0,3767
options.push_back(Option("recognizer", jso));
json_object_put(jso);
jso = json_tokener_parse("\
{\"range\": 20, \"x\": 465, \"y\":395}\
");
options.push_back(Option("autofix", jso));
json_object_put(jso);
MeterOCR m(options);
if (SUCCESS ==
m.open()) { // we don't use assert_eq here as not all build machines have a camera!
// ASSERT_EQ(SUCCESS, m.open());
std::vector<Reading> rds;
rds.resize(2);
EXPECT_EQ(0, m.read(rds, 2));
EXPECT_EQ(0, m.read(rds, 2));
EXPECT_EQ(0, m.read(rds, 2));
EXPECT_EQ(0, m.read(rds, 2));
/*
double value = rds[0].value();
EXPECT_TRUE(std::abs(0.3767-value)<0.00001);
value = rds[1].value();
EXPECT_GT(value, 80.0); // expect conf. >80
*/
ASSERT_EQ(0, m.close());
}
}
|