summaryrefslogtreecommitdiffstats
path: root/modules/image/src/img_ocr.c
blob: 2eea9c9ac0770cdedfc61d9adab8d137a41a1735 (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
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
		/* INT_MAX */
	#include <limits.h>
		/* snprintf() & FILENAME_MAX */
	#include <stdio.h>

/* Packages ------------------------------------------------------------------*/
		/* OCR Tesseract */
	#include <tesseract/capi.h>

/* Project -------------------------------------------------------------------*/
		/* share_path */
	#include "about.h"

/* Module --------------------------------------------------------------------*/
		/* data & img_ocr_text & OCR_TEXT_MAX */
	#include "img_iface.h"

	#include "img_ocr.h"


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	img_ocr_read	(void *data);


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
void	img_ocr_act	(int action, void *data)
{
	switch (action) {
	case IMG_OCR_ACT_READ:
		img_ocr_read(data);
		break;
	}
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	img_ocr_read	(void *data)
{
	struct TessBaseAPI	*handle_ocr;

	/* Data */
	struct Img_Iface_Data_Read	*data_cast;
	data_cast	= (struct Img_Iface_Data_Read *)data;

	/* Language */
	int	lang;
	char	lang_str [20 + 1];
	lang	= data_cast->lang;
	switch (lang) {
	case IMG_IFACE_OCR_LANG_ENG:
		sprintf(lang_str, "eng");
		break;
	case IMG_IFACE_OCR_LANG_SPA:
		sprintf(lang_str, "spa");
		break;
	case IMG_IFACE_OCR_LANG_CAT:
		sprintf(lang_str, "cat");
		break;
	case IMG_IFACE_OCR_LANG_DIGITS:
		sprintf(lang_str, "digits");
		break;
	case IMG_IFACE_OCR_LANG_DIGITS_COMMA:
		sprintf(lang_str, "digits_comma");
		break;
	}

	/* Config file */
	int	conf;
	char	conf_str [FILENAME_MAX];
	conf	= data_cast->conf;
	switch (conf) {
	case IMG_IFACE_OCR_CONF_PRICE:
		sprintf(conf_str, "%s/%s", share_path, "price");
		break;
	}

	/* init OCR */
	handle_ocr	= TessBaseAPICreate();
	TessBaseAPIInit2(handle_ocr, NULL, lang_str,
#ifdef	OEM_LSTM_ONLY
						OEM_LSTM_ONLY);
#else
						OEM_DEFAULT);
#endif
//						OEM_TESSERACT_LSTM_COMBINED);
	if (conf) {
		/* Configure OCR (whitelist chars) */
		TessBaseAPIReadConfigFile(handle_ocr, conf_str);
	}

	/* scan image for text */
	TessBaseAPISetImage(handle_ocr, data_cast->img.data,
				data_cast->img.width, data_cast->img.height,
				data_cast->img.B_per_pix,
				data_cast->img.B_per_line);
	TessBaseAPIRecognize(handle_ocr, NULL);
	char	*txt;
	txt	= TessBaseAPIGetUTF8Text(handle_ocr);

	/* Copy text to global variable */
	snprintf(img_ocr_text, OCR_TEXT_MAX, "%s", txt);

	/* cleanup */
	TessDeleteText(txt);
	TessBaseAPIEnd(handle_ocr);
	TessBaseAPIDelete(handle_ocr);
}


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