summaryrefslogtreecommitdiffstats
path: root/src/image/ocr.c
blob: 4f6d3debe58ae09c00194226db7ece129a4c2046 (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
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "vision-artificial/image/ocr.h"

#include <limits.h>
#include <stddef.h>
#include <stdio.h>

#include <tesseract/capi.h>

#include "vision-artificial/about/about.h"
#include "vision-artificial/image/iface.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* structs / unions *****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/


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


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	img_ocr_act	(int action, const void *data)
{

	switch (action) {
	case IMG_OCR_ACT_READ:
		img_ocr_read(data);
		break;
	}
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static	void	img_ocr_read	(const void *data)
{
	const	struct Img_Iface_Data_Read	*data_cast;
	struct TessBaseAPI	*handle_ocr;
	int	lang;
	char	lang_str[FILENAME_MAX];
	int	conf;
	char	conf_str[FILENAME_MAX];
	char	*txt;

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

	/* Language */
	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 */
	conf	= data_cast->conf;
	switch (conf) {
	case IMG_IFACE_OCR_CONF_PRICE:
		if (snprintf(conf_str, FILENAME_MAX, "%s/%s",
						share_path,
						"price")  >=  FILENAME_MAX) {
			printf("Path is too large and has been truncated\n");
			printf("Price configuration was not possible!\n");
			conf	= IMG_IFACE_OCR_CONF_NONE;
		}
		break;
	}

	/* init OCR */
	handle_ocr	= TessBaseAPICreate();
#if defined(OEM_LSTM_ONLY)
	TessBaseAPIInit2(handle_ocr, NULL, lang_str, OEM_LSTM_ONLY);
#else
	TessBaseAPIInit2(handle_ocr, NULL, lang_str, OEM_DEFAULT);
#endif
/*	TessBaseAPIInit2(handle_ocr, NULL, lang_str, OEM_TESSERACT_LSTM_COMBINED);*/

	/* Configure OCR (whitelist chars) */
	if (conf)
		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);
	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 **********************************************************
 ******************************************************************************/