27 #ifndef SCRIBO_TEXT_RECOGNITION_HH
28 # define SCRIBO_TEXT_RECOGNITION_HH
37 # if !defined HAVE_TESSERACT_2 && !defined HAVE_TESSERACT_3
38 # define HAVE_TESSERACT_2
45 # include <mln/core/image/dmorph/image_if.hh>
46 # include <mln/core/concept/neighborhood.hh>
47 # include <mln/core/site_set/box.hh>
49 # include <mln/util/array.hh>
50 # include <mln/data/fill.hh>
51 # include <mln/data/paste.hh>
52 # include <mln/data/paste_without_localization.hh>
53 # include <mln/pw/all.hh>
55 # include <mln/core/alias/w_window2d_int.hh>
56 # include <mln/make/w_window2d_int.hh>
58 # include <mln/border/resize.hh>
60 # include <scribo/core/macros.hh>
62 # include <scribo/text/clean_inplace.hh>
64 # include <scribo/core/line_set.hh>
67 # include <tesseract/baseapi.h>
69 # if defined HAVE_TESSERACT_3
70 # include <tesseract/resultiterator.h>
71 # endif // ! HAVE_TESSERACT_3
92 recognition(line_set<L>& lines,
const char *language);
105 template <
typename I>
108 const char *language,
109 const std::string& output_file = std::string());
113 # ifndef MLN_INCLUDE_ONLY
115 # ifdef HAVE_TESSERACT_2
118 template <
typename L>
120 recognition(line_set<L>& lines,
const char *language)
124 mln_trace(
"scribo::text::recognition");
139 setlocale(LC_NUMERIC,
"C");
142 TessBaseAPI::InitWithLanguage(NULL, NULL, language, NULL,
false, 0, NULL);
147 for_all_lines(i, lines)
149 if (! lines(i).is_textline())
152 mln_domain(I)
box = lines(i).
bbox();
162 const component_set<L>& comp_set = lines.
components();
167 lines(i).component_ids();
168 for_all_elements(e, lines(i).component_ids())
170 unsigned comp_id = comps(e);
172 | (
pw::value(lbl) == pw::cst(comp_id))).rw(),
187 mln_domain(I) lbox = text_ima.domain();
188 lbox.enlarge(lines(i).char_space() + 2);
189 I line_image(lbox, 0);
194 char* s = TessBaseAPI::TesseractRect(
195 (
unsigned char*) line_image.buffer(),
197 line_image.
ncols() * sizeof (
bool),
206 str = str.substr(0, str.length() - 2);
207 lines(i).update_text(str);
215 setlocale(LC_NUMERIC,
"");
219 template <
typename I>
222 const char *language,
223 const std::string& output_file)
227 mln_trace(
"scribo::text::recognition");
229 const I& line =
exact(line_);
230 mln_precondition(line.is_valid());
233 setlocale(LC_NUMERIC,
"C");
236 TessBaseAPI::InitWithLanguage(NULL, NULL, language, NULL,
false, 0, NULL);
239 if (!output_file.empty())
240 file.open(output_file.c_str());
242 mln_domain(I) box = line.domain();
252 border::
resize(text_ima, 0);
255 char* s = TessBaseAPI::TesseractRect(
256 (
unsigned char*) text_ima.buffer(),
258 text_ima.
ncols() * sizeof (
bool),
266 if (!output_file.empty())
269 str = str.substr(0, str.length() - 1);
270 file << line.domain().bbox().pmin().row()
272 << line.domain().bbox().pmin().col()
274 << line.domain().bbox().pmax().row()
276 << line.domain().bbox().pmax().col()
285 if (!output_file.empty())
289 setlocale(LC_NUMERIC,
"");
293 # else // HAVE_TESSERACT_3
296 template <
typename L>
298 recognition(line_set<L>& lines,
const char *language)
302 mln_trace(
"scribo::text::recognition");
305 setlocale(LC_NUMERIC,
"C");
308 tesseract::TessBaseAPI tess;
309 if (tess.Init(NULL, language, tesseract::OEM_DEFAULT) == -1)
311 std::cerr <<
"Error: cannot initialize tesseract!" << std::endl;
314 tess.SetPageSegMode(tesseract::PSM_SINGLE_LINE);
319 for_all_lines(i, lines)
321 if (! lines(i).is_textline())
324 mln_domain(I) box = lines(i).bbox();
336 const component_set<L>& comp_set = lines.components();
337 const L& lbl = comp_set.labeled_image();
341 lines(i).component_ids();
342 for_all_elements(e, lines(i).component_ids())
344 unsigned comp_id = comps(e);
346 | (
pw::value(lbl) == pw::cst(comp_id))).rw(),
355 (
unsigned char*) &text_ima(text_ima.domain().pmin()),
359 text_ima.ncols() *
sizeof (bool)
360 + 2 * text_ima.border());
362 char *s = tess.GetUTF8Text();
365 tesseract::ResultIterator *it = tess.GetIterator();
367 str = str.substr(0, str.length() - 2);
368 lines(i).update_text(str, it->Confidence(tesseract::RIL_TEXTLINE));
375 setlocale(LC_NUMERIC,
"");
379 template <
typename I>
382 const char *language,
383 const std::string& output_file)
387 mln_trace(
"scribo::text::recognition");
389 const I& line =
exact(line_);
390 mln_precondition(line.is_valid());
393 setlocale(LC_NUMERIC,
"C");
396 tesseract::TessBaseAPI tess;
397 if (tess.Init(NULL, language, tesseract::OEM_DEFAULT) == -1)
399 std::cerr <<
"Error: cannot initialize tesseract!" << std::endl;
404 if (!output_file.empty())
405 file.open(output_file.c_str());
408 char* s = tess.TesseractRect(
409 (
unsigned char*) &
line(line.domain().pmin()),
411 line.ncols() *
sizeof (bool) + line.border() * 2,
419 if (!output_file.empty())
422 str = str.substr(0, str.length() - 1);
423 file << line.domain().bbox().pmin().row()
425 << line.domain().bbox().pmin().col()
427 << line.domain().bbox().pmax().row()
429 << line.domain().bbox().pmax().col()
438 if (!output_file.empty())
442 setlocale(LC_NUMERIC,
"");
446 # endif // ! HAVE_TESSERACT_2
448 # endif // ! MLN_INCLUDE_ONLY
454 #endif // ! SCRIBO_TEXT_RECOGNITION_HH