runner.cc 6.35 KB
Newer Older
1
2
// Copyright (C) 2010, 2011, 2012 EPITA Research and Development
// Laboratory (LRDE)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.

#include "runner.hh"

#include <mln/core/image/image2d.hh>
#include <mln/value/rgb8.hh>
#include <mln/io/magick/load.hh>

24
25
#include "demodir.hh"

26
27
28
29
30
31
#include "process.hh"
#include "preprocess.hh"
#include "config.hh"
#include "defs.hh"


32

33
34
35
using namespace mln;
using namespace scribo::toolchain::internal;

36
37
38
39
static
QString get_pathto(const QString& file,
		   const QString localdirsuffix = QString())
{
40
  // Not installed ?
41
42
43
44
  QFile f(SCRIBO_LOCAL_DEMODIR "/" + localdirsuffix + "/" + file);
  if (f.exists())
    return SCRIBO_LOCAL_DEMODIR "/" + localdirsuffix;

45
  // Installed in a specific path ?
46
  f.setFileName(SCRIBO_PREFIX_LIBEXECDIR "/" + file);
47
  if (f.exists())
48
    return SCRIBO_PREFIX_LIBEXECDIR;
49

50
51
52
  // Installed in a specific path but moved elsewhere ?
  QDir dir(QCoreApplication::applicationDirPath());
  dir.cdUp(); // move from prefix/bin to prefix/
53
54
55
56
57
58
59
60
61

  const char *path[] = { "lib", "libexec", 0 };

  for (int i = 0; path[i]; ++i)
  {
    f.setFileName(dir.currentPath() + QString("/%1/scribo").arg(path[i]) + file);
    if (f.exists())
      return dir.currentPath() + QString("/%1/scribo").arg(path[i]);
  }
62

63
64
65
66
67
68
  qDebug() << "FATAL ERROR: Can't locate file: " + file;

  return "";
}


69
70
71
72
73
74
75
runner::runner(QObject *parent)
  : QThread(parent)
{
  moveToThread(this);
}


76
void runner::run()
77
78
{

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  switch(mode_)
  {
    default:
    case Demat:
    {
      image2d<value::rgb8> ima;
      io::magick::load(ima, args_.at(0).toUtf8().constData());

      image2d<bool> bin_ima = preprocess(ima);
      process(ima, bin_ima);
    }
    break;

    case Export:
      export_as();
    break;
  }

  emit finished();
  qDebug() << "Done.";
99
100
101
}


102
void runner::stop()
103
{
104
105
  terminate();
}
106
107
108



109
110
111
112
// Demat related stuff

void runner::start_demat(const QString& filename)
{
113
  args_.clear();
114
115
116
117
  args_ << filename;
  mode_ = Demat;

  QThread::start();
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
}


image2d<bool>
runner::preprocess(const image2d<value::rgb8>& ima)
{
  emit new_step("Preprocessing");

  ::preprocess f;

  // Load config.
  config * const conf = config::get_instance();

  connect(&f, SIGNAL(progress()), this, SIGNAL(progress()));
  connect(&f, SIGNAL(new_progress_label(const QString&)),
	  this, SIGNAL(new_progress_label(const QString&)));

  f.enable_subsample = conf->preprocessing_subsample();
  f.enable_fg_extraction = conf->preprocessing_remove_bg();
  f.enable_deskew = conf->preprocessing_deskew();
  f.enable_denoising = conf->preprocessing_remove_noise();

  f.binarization_algo = static_cast<Binarization_Algo>(conf->preprocessing_bin_algo());

  emit new_progress_max_value(f.nsteps());

  // Perform preprocessing.
  f(ima);

  qDebug() << "Preprocess Done.";
  return f.output;
}


void runner::process(const image2d<value::rgb8>& original_ima,
		     const image2d<bool>& processed_ima)
{
  emit new_step("Page segmentation");

157
  ::process f(args_.at(0).toUtf8().constData());
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

  connect(&f, SIGNAL(progress()), this, SIGNAL(progress()));
  connect(&f, SIGNAL(new_progress_label(const QString&)),
	  this, SIGNAL(new_progress_label(const QString&)));
  connect(&f, SIGNAL(xml_saved(const QString&)),
	  this, SIGNAL(xml_saved(const QString&)));

  // Load config.
  config * const conf = config::get_instance();

  defs::FindSeparators
    find_seps = static_cast<defs::FindSeparators>(conf->segmentation_find_seps());
  f.enable_line_seps = (find_seps == defs::Lines
			|| find_seps == defs::LinesAndWhitespaces);
  f.enable_whitespace_seps = (find_seps == defs::Whitespaces
			      || find_seps == defs::LinesAndWhitespaces);
174
175
  f.enable_ocr = conf->ocr_enabled();
  f.ocr_language = conf->ocr_language().toAscii().data();
176
  f.xml_format = scribo::io::xml::PageExtended;
177
178
179


  f.save_doc_as_xml = true;
180
  QFileInfo file(args_.at(0));
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
  QString output_dir = QDir::tempPath();
  if (conf->general_save_xml_enabled())
  {
    if (conf->general_save_xml_same_dir())
      output_dir = file.absolutePath();
    else if (conf->general_save_xml_custom_dir())
      output_dir = conf->general_save_xml_custom_dir_path();
    else
      qDebug() << "runner::progress - Invalid xml saving option!";

    QDir dir(output_dir);
    if (!dir.exists() && !dir.mkpath(output_dir))
      output_dir = QDir::tempPath();
  }
  f.output_file = (output_dir  + "/" + file.baseName() + "_gui.xml").toUtf8().constData();

  emit new_progress_max_value(f.nsteps());

  // Perform text detection.
  f(original_ima, processed_ima);

  qDebug() << "Process Done.";
}


206
207
208
209
210

// Export related stuff

void runner::start_export(const QString& imgfile,
			  const QString& xmlfile, const QString& outfile)
211
{
212
  args_.clear();
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
  args_ << imgfile << xmlfile << outfile;
  mode_ = Export;

  QThread::start();
}


void runner::export_as()
{
  emit new_step("Exporting document...");
  emit new_progress_max_value(2);

  // Checking output format
  QFileInfo f(args_.at(2));

  QString pathto_xml2doc = get_pathto("scribo-xml2doc", "xml2doc");

  if (pathto_xml2doc.isEmpty())
  {
    QMessageBox::critical(0, "Fatal error", "Cannot export! Cannot find scribo-xml2doc program!");
    return;
  }

  emit progress();

  int rvalue = 0;
  if (f.suffix() == "pdf")
240
241
  {
    emit new_step("Exporting as PDF...");
242
243
244
    rvalue = system(QString("%1/scribo-xml2doc --pdf %2 %3 %4")
		    .arg(pathto_xml2doc).arg(args_.at(1)).arg(args_.at(0))
		    .arg(args_.at(2)).toAscii().constData());
245
  }
246
  else if (f.suffix() == "html" || f.suffix() == "htm")
247
248
  {
    emit new_step("Exporting as HTML...");
249
250
251
    rvalue = system(QString("%1/scribo-xml2doc --html %2 %3 %4")
		    .arg(pathto_xml2doc).arg(args_.at(1)).arg(args_.at(0))
		    .arg(args_.at(2)).toAscii().constData());
252
  }
253
254
255
256
257
  else
    QMessageBox::critical(0, "Fatal error", "Cannot export! Invalid output format!");

  if (rvalue != 0)
    QMessageBox::critical(0, "Fatal error", "Cannot export! Return value is not 0!");
258
}