summaryrefslogtreecommitdiffstats
path: root/modules/proc/src/proc_label.cpp
blob: 765856d66770eb050a6e326802a77dc7a4f225d3 (plain) (blame)
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
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
		/* snprintf() */
	#include <cstdio>
		/* strcmp() */
	#include <cstring>

/* Packages ------------------------------------------------------------------*/
		/* openCV */
	#include <opencv2/opencv.hpp>
		/* zbar::ZBAR_EAN13 */
	#include <zbar.h>

/* Project -------------------------------------------------------------------*/
		/* zb_codes */
	#include "img_iface.hpp"
		/* user_iface_log */
	#include "user_iface.hpp"

/* Module --------------------------------------------------------------------*/
	#include "proc_common.hpp"

	#include "proc_label.hpp"


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
/* Global --------------------------------------------------------------------*/

/* Static --------------------------------------------------------------------*/
static	std::vector <std::vector <cv::Point_ <int>>>	contours;
static	class cv::Mat					hierarchy;
static	class cv::RotatedRect				rect;


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	result_label		(int status);

static	int	label_find		(void);
static	void	label_align		(void);
static	int	find_cerdo		(void);
static	int	barcode_read		(void);
static	int	barcode_chk_prod	(void);
static	void	price_read		(void);
static	int	price_chk		(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
int	proc_label		(void)
{
	int	status;

	proc_save_mem(0);
	/* Find label (position and angle) */
	{
		/* Measure time */
		clock_start();

		status	= label_find();
		if (status) {
			result_label(status);
			return	status;
		}

		/* Measure time */
		clock_stop("Find label");
	}
	/* Align label and extract green component */
	{
		/* Measure time */
		clock_start();

		label_align();

		/* Measure time */
		clock_stop("Align label");
	}
	/* Find "Cerdo" in aligned image */
	{
		/* Measure time */
		clock_start();

		status	= find_cerdo();
		if (status) {
			result_label(status);
			return	status;
		}

		/* Measure time */
		clock_stop("Find cerdo (OCR)");
	}
	/* Read barcode in original image */
	{
		/* Measure time */
		clock_start();

		status	= barcode_read();
		if (status) {
			result_label(status);
			return	status;
		}

		/* Measure time */
		clock_stop("Read barcode (zbar)");
	}
	/* Check product code in barcode */
	{
		/* Measure time */
		clock_start();

		status	= barcode_chk_prod();
		if (status) {
			result_label(status);
			return	status;
		}

		/* Measure time */
		clock_stop("Chk product code");
	}
	/* Read price in aligned image (green component) */
	{
		/* Measure time */
		clock_start();

		price_read();

		/* Measure time */
		clock_stop("Read price (OCR)");
	}
	/* Check label price with barcode price */
	{
		/* Measure time */
		clock_start();

		status	= price_chk();
		if (status) {
			result_label(status);
			return	status;
		}

		/* Measure time */
		clock_stop("Check price");
	}

	status	= LABEL_OK;
	result_label(status);
	return	status;
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	result_label		(int status)
{
	/* Cleanup */

	/* Write result into log */
	char	result [LOG_LINE_LEN];
	switch (status) {
	case LABEL_OK:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  OK");
		break;
	case LABEL_NOK_LABEL:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK_LABEL");
		break;
	case LABEL_NOK_CERDO:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK_CERDO");
		break;
	case LABEL_NOK_BCODE:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK_BCODE");
		break;
	case LABEL_NOK_PRODUCT:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK_PRODUCT");
		break;
	case LABEL_NOK_PRICE:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK_PRICE");
		break;
	default:
		snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN,
						"Label:  NOK");
		break;
	}
	user_iface_log.lvl[user_iface_log.len]	= 0;
	(user_iface_log.len)++;
}

static	int	label_find		(void)
{
	int	status;

	proc_load_mem(0);

	proc_cmp(IMG_IFACE_CMP_BLUE);
	proc_smooth(IMGI_SMOOTH_MEDIAN, 7);
#if 0
	proc_adaptive_threshold(CV_ADAPTIVE_THRESH_MEAN_C,
					CV_THRESH_BINARY, 5);
#else
	proc_not();
#endif
	proc_smooth(IMGI_SMOOTH_MEAN, 21);
	proc_threshold(cv::THRESH_BINARY_INV, 2);
	proc_dilate_erode(100);
	proc_contours(&contours, &hierarchy);

	/* If no contour is found, error:  NOK_LABEL */
	if (!contours.size()) {
		status	= LABEL_NOK_LABEL;
		return	status;
	}

	proc_min_area_rect(&(contours[0]), &rect, true);

	/* If angle is < -45º, it is taking into acount the incorrect side */
	if (rect.angle < -45.0) {
		int	tmp;
		rect.angle		+= 90.0;
		tmp			= rect.size.width;
		rect.size.width		= rect.size.height;
		rect.size.height	= tmp;
	}

	status	= LABEL_OK;
	return	status;
}

static	void	label_align		(void)
{
	proc_load_mem(0);

	proc_rotate(&rect);
	proc_cmp(IMG_IFACE_CMP_GREEN);
	proc_save_mem(1);
}

static	int	find_cerdo		(void)
{
	int	status;

	proc_load_mem(1);

	int	x;
	int	y;
	int	w;
	int	h;
	x	= rect.center.x - (1.05 * rect.size.width / 2);
	if (x < 0) {
		x	= 0;
	}
	y	= rect.center.y - (1.47 * rect.size.height / 2);
	if (y < 0) {
		y	= 0;
	}
	w	= rect.size.width / 2;
	h	= rect.size.height * 0.20;
	proc_ROI(x, y, w, h);
	proc_threshold(cv::THRESH_BINARY, IMG_IFACE_THR_OTSU);
	proc_erode(1);
	proc_OCR(IMG_IFACE_OCR_LANG_ENG, IMG_IFACE_OCR_CONF_NONE);

	/* Compare Label text to "Cerdo". */
	bool	cerdo_nok;
	cerdo_nok	= strncmp(img_ocr_text, "Cerdo",
						strlen("Cerdo"));

	/* If string doesn't match, error:  NOK_CERDO */
	if (cerdo_nok) {
		status	= LABEL_NOK_CERDO;
		return	status;
	}

	status	= LABEL_OK;
	return	status;
}

static	int	barcode_read		(void)
{
	int	status;

	proc_load_mem(0);

	proc_cmp(IMG_IFACE_CMP_GREEN);
	proc_zbar(zbar::ZBAR_EAN13);

	/* Check that 1 and only 1 bcode is read. */
	if (zb_codes.n != 1) {
		status	= LABEL_NOK_BCODE;
		return	status;
	}

	status	= LABEL_OK;
	return	status;
}

static	int	barcode_chk_prod	(void)
{
	int	status;

	bool	prod_nok;
	prod_nok	= strncmp(zb_codes.arr[0].data, "2301703",
						strlen("2301703"));
	if (prod_nok) {
		status	= LABEL_NOK_PRODUCT;
		return	status;
	}

	status	= LABEL_OK;
	return	status;
}

static	void	price_read		(void)
{
	proc_load_mem(1);

	int	x;
	int	y;
	int	w;
	int	h;
	x	= rect.center.x + (0.33 * rect.size.width / 2);
	y	= rect.center.y + (0.64 * rect.size.height / 2);
	w	= rect.size.width * 0.225;
	h	= rect.size.height * 0.15;
	proc_ROI(x, y, w, h);
	proc_smooth(IMGI_SMOOTH_MEAN, 3);
	proc_threshold(cv::THRESH_BINARY, IMG_IFACE_THR_OTSU);
	proc_dilate_erode(1);
	proc_threshold(cv::THRESH_BINARY, 1);
	proc_OCR(IMG_IFACE_OCR_LANG_DIGITS, IMG_IFACE_OCR_CONF_PRICE);
}

static	int	price_chk		(void)
{
	int	status;

	char	price [80];

	/* Extract price from barcode */
	if (zb_codes.arr[0].data[8] != '0') {
		snprintf(price, 80, "%c%c.%c%c",
					zb_codes.arr[0].data[8],
					zb_codes.arr[0].data[9],
					zb_codes.arr[0].data[10],
					zb_codes.arr[0].data[11]);
	} else {
		snprintf(price, 80, "%c.%c%c",
					zb_codes.arr[0].data[9],
					zb_codes.arr[0].data[10],
					zb_codes.arr[0].data[11]);
	}

	/* Compare price from barcode and from text */
	bool	price_nok;
	price_nok	= strncmp(img_ocr_text, price, strlen(price));

	if (price_nok) {
		status	= LABEL_NOK_PRICE;
		return	status;
	}

	status	= LABEL_OK;
	return	status;
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/