forked from skaringa/emeocv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·401 lines (335 loc) · 11.1 KB
/
main.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* main.cpp
*
*/
#include <string>
#include <list>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/SimpleLayout.hh>
#include <log4cpp/Priority.hh>
#include "Config.h"
#include "CSVDatabase.h"
#include "Directory.h"
#include "ImageProcessor.h"
#include "KNearestOcr.h"
#include "Plausi.h"
#include "RRDatabase.h"
static int delay = 1000;
#ifndef VERSION
#define VERSION "0.9.7 tweaked by WOF2"
#endif
static void testOcr(ImageInput* pImageInput) {
log4cpp::Category::getRoot().info("testOcr");
Config config;
config.loadConfig();
ImageProcessor proc(config);
proc.debugWindow();
proc.debugDigits();
Plausi plausi(config);
KNearestOcr ocr(config);
if (! ocr.loadTrainingData()) {
std::cout << "Failed to load OCR training data\n";
return;
}
std::cout << "OCR training data loaded.\n";
std::cout << "<q> to quit.\n";
while (pImageInput->nextImage()) {
proc.setInput(pImageInput->getImage());
proc.process();
if (proc.getOutput().size() != config.getDigitCount()) {
plausi.registerNotRecognized();
}else{
std::string result = ocr.recognize(proc.getOutput());
std::cout << result;
if (plausi.check(result, pImageInput->getTime())) {
std::cout << " " << std::fixed << std::setprecision(1) << plausi.getCheckedValue() << std::endl;
} else {
std::cout << " -------";
DirectoryInput* dirInput = dynamic_cast<DirectoryInput*>(pImageInput);
if (dirInput != nullptr) {
std::cout << " " << dirInput->getCurrentFilename();;
}
std::cout << std::endl;
}
}
int key = cv::waitKey(delay) & 255;
if (key == 'q') {
std::cout << "Quit\n";
break;
}
}
std::cout << plausi.getStats() << std::endl;
}
static void learnOcr(ImageInput* pImageInput) {
log4cpp::Category::getRoot().info("learnOcr");
Config config;
config.loadConfig();
ImageProcessor proc(config);
proc.debugWindow();
KNearestOcr ocr(config);
ocr.loadTrainingData();
std::cout << "Entering OCR training mode!\n";
std::cout << "<0>..<9> to answer digit, <space> to ignore digit, <s> to save and quit, <q> to quit without saving.\n";
int key = 0;
while (pImageInput->nextImage()) {
proc.setInput(pImageInput->getImage());
proc.process();
key = ocr.learn(proc.getOutput());
std::cout << std::endl;
if (key == 'q' || key == 's') {
std::cout << "Quit\n";
break;
}
}
if (key != 'q' && ocr.hasTrainingData()) {
std::cout << "Saving training data\n";
ocr.saveTrainingData();
}
}
static void printStatistics() {
Config config;
config.loadConfig();
KNearestOcr ocr(config);
if (! ocr.loadTrainingData()) {
std::cout << "Failed to load OCR training data. Use option -l to train the program\n";
return;
}
std::cout << "OCR stats:\n"<<ocr.getStatitics();
}
static void adjustCamera(ImageInput* pImageInput) {
log4cpp::Category::getRoot().info("adjustCamera");
Config config;
config.loadConfig();
ImageProcessor proc(config);
proc.debugWindow();
proc.debugDigits();
//proc.debugEdges();
//proc.debugSkew();
std::cout << "Adjust camera.\n";
std::cout << "<r>, <p> to select raw or processed image, <s> to save config and quit, <q> to quit without saving.\n";
bool processImage = true;
int key = 0;
while (pImageInput->nextImage()) {
std::cout << "Reloading config" << std::fixed << std::endl;
config.loadConfig();
proc.reloadConfig(config);
proc.setInput(pImageInput->getImage());
if (processImage) {
try{
proc.process();
}
catch (const cv::Exception& e) {
std::cout << "Exception while processing image. Skipping to next " << std::fixed<< std::endl;
}
} else {
proc.showImage();
}
key = cv::waitKey(delay) & 255;
if (key == 'q' || key == 's') {
std::cout << "Quit\n";
break;
} else if (key == 'r') {
processImage = false;
} else if (key == 'p') {
processImage = true;
}
}
if (key != 'q') {
std::cout << "Saving config\n";
config.saveConfig();
};
}
static void capture(ImageInput* pImageInput) {
log4cpp::Category::getRoot().info("capture");
std::cout << "Capturing images into directory.\n";
std::cout << "<Ctrl-C> to quit.\n";
while (pImageInput->nextImage()) {
usleep(delay*1000L);
}
}
static void writeData(ImageInput* pImageInput) {
log4cpp::Category::getRoot().info("writeData");
Config config;
config.loadConfig();
ImageProcessor proc(config);
Plausi plausi(config);
//RRDatabase rrd("emeter.rrd");
CSVDatabase csv("emeter.csv");
struct stat st;
KNearestOcr ocr(config);
if (! ocr.loadTrainingData()) {
std::cout << "Failed to load OCR training data\n";
return;
}
std::cout << "OCR training data loaded.\n";
std::cout << "<Ctrl-C> to quit.\n";
while (pImageInput->nextImage()) {
proc.setInput(pImageInput->getImage());
try{
proc.process();
}
catch (const cv::Exception& e) {
std::cout << "Exception while processing image. Skipping to next " << std::fixed<< std::endl;
continue;
}
if (proc.getOutput().size() != config.getDigitCount()) {
plausi.registerNotRecognized();
}else{
std::string result = ocr.recognize(proc.getOutput());
std::cout << "Result for plausi process: "<< result;;
DirectoryInput* dirInput = dynamic_cast<DirectoryInput*>(pImageInput);
if (dirInput != nullptr) {
std::cout << " " << dirInput->getCurrentFilename();;
}
std::cout << "\n";
if (plausi.check(result, pImageInput->getTime())) {
char buff[20];
time_t now = plausi.getCheckedTime();
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
std::cout << "Saving record to CSV :"<<buff<<", value: "<<plausi.getCheckedValue()<<".\n";
csv.update( plausi.getCheckedTime(), plausi.getCheckedValue());
}
}
if (0 == stat("imgdebug", &st) && S_ISDIR(st.st_mode)) {
// write debug image
pImageInput->setOutputDir("imgdebug");
pImageInput->saveImage();
pImageInput->setOutputDir("");
}
usleep(delay*1000L);
}
std::cout << plausi.getStats() << std::endl;
}
static void usage(const char* progname) {
std::cout << "Program to read and recognize the counter of an electricity meter with OpenCV.\n";
std::cout << "Homepage: https://github.com/wof2/emeocv\n";
std::cout << "Version: " << VERSION << std::endl;
std::cout << "Usage: " << progname << " [-i <dir>|-c <cam>|k] [-l|-t|-a|-w|-o <dir>] [-s <delay>] [-v <level>\n";
std::cout << "\nImage input:\n";
std::cout << " -i <image directory> : read image files (png) from directory.\n";
std::cout << " -c <camera number> : read images from camera.\n";
std::cout << " -k : read images from camera. Use CLI command provided in config.yml\n";
std::cout << "\nOperation:\n";
std::cout << " -a : adjust camera.\n";
std::cout << " -o <directory> : capture images into directory.\n";
std::cout << " -l : learn OCR.\n";
std::cout << " -t : test OCR.\n";
std::cout << " -p : print OCR training set statistics.\n";
std::cout << " -w : write OCR data to RR database. This is the normal working mode.\n";
std::cout << "\nOptions:\n";
std::cout << " -s <n> : Sleep n milliseconds after processing of each image (default=1000).\n";
std::cout << " -v <l> : Log level. One of DEBUG, INFO, ERROR (default).\n";
}
static void configureLogging(const std::string & priority = "INFO", bool toConsole = false) {
log4cpp::Appender *fileAppender = new log4cpp::FileAppender("default", "emeocv.log");
log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
layout->setConversionPattern("%d{%d.%m.%Y %H:%M:%S} %p: %m%n");
fileAppender->setLayout(layout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::getPriorityValue(priority));
root.addAppender(fileAppender);
if (toConsole) {
log4cpp::Appender *consoleAppender = new log4cpp::OstreamAppender("console", &std::cout);
consoleAppender->setLayout(new log4cpp::SimpleLayout());
root.addAppender(consoleAppender);
}
}
int main(int argc, char **argv) {
int opt;
ImageInput* pImageInput = 0;
int inputCount = 0;
std::string outputDir;
std::string logLevel = "ERROR";
char cmd = 0;
int cmdCount = 0;
Config config;
while ((opt = getopt(argc, argv, "i:kc:ltaws:o:v:hp")) != -1) {
switch (opt) {
case 'i':
pImageInput = new DirectoryInput(Directory(optarg, ".png"));
inputCount++;
break;
case 'c':
pImageInput = new CameraInput(atoi(optarg));
inputCount++;
break;
case 'k':
config.loadConfig();
pImageInput = new CLIImageInput(config.getCliCaptureCommand(), config.getCliCaptureTemporaryPath());
inputCount++;
break;
case 'p':
printStatistics();
return(EXIT_SUCCESS);
break;
case 'l':
case 't':
case 'a':
case 'w':
cmd = opt;
cmdCount++;
break;
case 'o':
cmd = opt;
cmdCount++;
outputDir = optarg;
break;
case 's':
delay = atoi(optarg);
break;
case 'v':
logLevel = optarg;
break;
case 'h':
default:
usage(argv[0]);
return(EXIT_FAILURE);
break;
}
}
if (inputCount != 1) {
std::cerr << "*** You should specify exactly one camera or input directory!\n\n";
usage(argv[0]);
return(EXIT_FAILURE);
}
if (cmdCount != 1) {
std::cerr << "*** You should specify exactly one operation!\n\n";
usage(argv[0]);
return(EXIT_FAILURE);
}
configureLogging(logLevel, cmd == 'a');
switch (cmd) {
case 'o':
pImageInput->setOutputDir(outputDir);
capture(pImageInput);
break;
case 'l':
learnOcr(pImageInput);
break;
case 't':
testOcr(pImageInput);
break;
case 'a':
adjustCamera(pImageInput);
break;
case 'w':
writeData(pImageInput);
break;
}
delete pImageInput;
return EXIT_SUCCESS;
}