$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
content_in_doc.cc
1 // Copyright (C) 2010, 2011, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 
28 #include <libgen.h>
29 #include <fstream>
30 #include <iostream>
31 
32 #include <mln/core/image/image2d.hh>
33 
34 #include <mln/io/pbm/save.hh>
35 #include <mln/io/magick/load.hh>
36 
37 #include <scribo/toolchain/content_in_doc.hh>
38 #include <scribo/toolchain/text_in_doc_preprocess.hh>
39 
40 #include <scribo/core/document.hh>
41 
42 #include <scribo/debug/usage.hh>
43 #include <scribo/debug/logger.hh>
44 
45 #include <scribo/preprocessing/crop_without_localization.hh>
46 #include <scribo/preprocessing/crop.hh>
47 
48 #include <scribo/io/xml/save.hh>
49 #include <scribo/io/img/save.hh>
50 
51 #include <scribo/debug/option_parser.hh>
52 
53 
54 static const scribo::debug::arg_data arg_desc[] =
55 {
56  { "input.*", "An image." },
57  { "out.xml", "Result of the document analysis" },
58  {0, 0}
59 };
60 
61 
62 // --enable/disable-<name>
63 static const scribo::debug::toggle_data toggle_desc[] =
64 {
65  // name, description, default value
66  { "denoising", "Performs a denoising. (default: enabled)", true },
67  { "find-delims", "Find text alignements and whitespaces "
68  "to improve layout detection. (default: enabled)", true },
69  { "find-seps", "Find separators in document (default: enabled)", true },
70  { "ocr", "Performs character recognition (default: enabled)", true },
71  { "deskew", "Deskew image (default: disabled)", false},
72  {0, 0, false}
73 };
74 
75 
76 // --<name> <args>
77 static const scribo::debug::opt_data opt_desc[] =
78 {
79  // name, description, arguments, check args function, number of args, default arg
80  { "crop", "Crop input image before processing it.",
81  "<pmin_row> <pmin_col> <pmax_row> <pmax_col>", 0, 4, 0 },
82  { "debug-prefix", "Enable debug image outputs. Prefix image name with that "
83  "given prefix.", "<prefix>", 0, 1, 0 },
84  { "ocr-lang", "Set the language to be recognized by the OCR (Tesseract). "
85  "Depending on your system, you can choose between eng (default), "
86  "fra, deu, ita, nld, por, spa, vie",
87  "<lang>", scribo::debug::check_ocr_lang, 1, "eng" },
88  { "verbose", "Enable verbose mode", 0, 0, 0, 0 },
89  { "xml-format", "Choose betwen page, page-ext and full (default: page-ext).", "<format>",
90  scribo::debug::check_xml_format, 1, "page-ext" },
91  { "more-xml-format", "Provide an additional xml output. Format can"
92  " be chosen between page, page-ext and full (default: page-ext).", "<format>",
93  scribo::debug::check_xml_format, 1, "none" },
94  { "more-xml-file", "Filename of the additional xml output.", "<filename>",
95  0, 1, "/dev/null" },
96  { "debug-regions", "Save a debug image with all the regions.", "<filename>",
97  0, 1, "/dev/null" },
98  {0, 0, 0, 0, 0, 0}
99 };
100 
101 
102 int main(int argc, char* argv[])
103 {
104  using namespace scribo;
105  using namespace mln;
106 
107  scribo::debug::option_parser options(arg_desc, toggle_desc, opt_desc);
108 
109  if (!options.parse(argc, argv))
110  return 1;
111 
112  // Enable debug output.
113  if (options.is_set("debug-prefix"))
114  {
115  scribo::debug::logger().set_filename_prefix(options.opt_value("debug-prefix").c_str());
116  scribo::debug::logger().set_level(scribo::debug::All);
117  }
118 
119  bool verbose = options.is_set("verbose");
120 
121  mln_trace("main");
122 
124  image2d<value::rgb8> input;
125  mln::io::magick::load(input, options.arg("input.*"));
126 
127  bool enable_deskew = options.is_enabled("deskew");
128  // Preprocess document
130  input_preproc = toolchain::text_in_doc_preprocess(input, false, 0,
131  0.34, enable_deskew, verbose);
132 
133  // Optional Cropping
134  point2d crop_shift = literal::origin;
135  if (options.is_set("crop"))
136  {
137  std::vector<const char *> values = options.opt_values("crop");
139  minr = atoi(values[0]),
140  minc = atoi(values[1]),
141  maxr = atoi(values[2]),
142  maxc = atoi(values[3]);
143 
144  if (verbose)
145  std::cout << "> Image cropped from (" << minr << "," << minc << ")"
146  << " to (" << maxr << "," << maxc << ")" << std::endl;
147 
148  box2d roi = mln::make::box2d(minr, minc, maxr, maxc);
149  input_preproc = preprocessing::crop_without_localization(input_preproc, roi);
150  crop_shift = point2d(minr, minc);
151 
152  scribo::debug::logger().log_image(scribo::debug::Results, input_preproc,
153  "input_preproc_cropped.pbm");
154  }
155 
156  bool denoise = options.is_enabled("denoising");
157  std::string language = options.opt_value("ocr-lang");
158  bool find_line_seps = options.is_enabled("find-seps");
159  bool find_whitespace_seps = options.is_enabled("find-delims");
160  bool enable_ocr = options.is_enabled("ocr");
161 
162  if (verbose)
163  std::cout << "Running with the following options :"
164  << " ocr_language = " << language
165  << " | find_lines_seps = " << find_line_seps
166  << " | find_whitespace_seps = " << find_whitespace_seps
167  << " | debug = " << scribo::debug::logger().is_enabled()
168  << std::endl;
169 
170  // Run document toolchain.
171 
172  // Text
173  if (verbose)
174  std::cout << "Analysing document..." << std::endl;
176  doc = scribo::toolchain::content_in_doc(input, input_preproc, denoise,
177  find_line_seps, find_whitespace_seps,
178  enable_ocr, language, verbose);
179 
180  // Saving results
181  if (verbose)
182  std::cout << "Saving results..." << std::endl;
183 
184  // Default XML output
185  if (options.opt_value("xml-format") == "page-ext")
186  scribo::io::xml::save(doc, options.arg("out.xml"), scribo::io::xml::PageExtended);
187  if (options.opt_value("xml-format") == "page")
188  scribo::io::xml::save(doc, options.arg("out.xml"), scribo::io::xml::Page);
189  if (options.opt_value("xml-format") == "full")
190  scribo::io::xml::save(doc, options.arg("out.xml"), scribo::io::xml::Full);
191 
192  // Additional XML output
193  if (options.opt_value("more-xml-format") == "page-ext")
194  scribo::io::xml::save(doc, options.opt_value("more-xml-file"), scribo::io::xml::PageExtended);
195  if (options.opt_value("more-xml-format") == "page")
196  scribo::io::xml::save(doc, options.opt_value("more-xml-file"), scribo::io::xml::Page);
197  if (options.opt_value("more-xml-format") == "full")
198  scribo::io::xml::save(doc, options.opt_value("more-xml-file"), scribo::io::xml::Full);
199 
201  scribo::io::img::save(doc, mln::debug::filename("regions.png"), scribo::io::img::DebugWoImage);
202  if (options.opt_value("debug-regions") != "/dev/null")
203  scribo::io::img::save(doc, options.opt_value("debug-regions"), scribo::io::img::DebugWithImage);
204 
205 }