tesseract  5.0.0
par_control.cpp
Go to the documentation of this file.
1 // File: par_control.cpp
3 // Description: Control code for parallel implementation.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2013, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 #include "tesseractclass.h"
20 #ifdef _OPENMP
21 # include <omp.h>
22 #endif // _OPENMP
23 
24 namespace tesseract {
25 
26 struct BlobData {
27  BlobData() = default;
28  BlobData(int index, Tesseract *tess, const WERD_RES &word)
29  : blob(word.chopped_word->blobs[index])
30  , tesseract(tess)
31  , choices(&(*word.ratings)(index, index)) {}
32 
33  TBLOB *blob = nullptr;
34  Tesseract *tesseract = nullptr;
35  BLOB_CHOICE_LIST **choices = nullptr;
36 };
37 
38 void Tesseract::PrerecAllWordsPar(const std::vector<WordData> &words) {
39  // Prepare all the blobs.
40  std::vector<BlobData> blobs;
41  for (const auto &w : words) {
42  if (w.word->ratings != nullptr && w.word->ratings->get(0, 0) == nullptr) {
43  for (size_t s = 0; s < w.lang_words.size(); ++s) {
44  Tesseract *sub = s < sub_langs_.size() ? sub_langs_[s] : this;
45  const WERD_RES &word = *w.lang_words[s];
46  for (unsigned b = 0; b < word.chopped_word->NumBlobs(); ++b) {
47  blobs.emplace_back(b, sub, word);
48  }
49  }
50  }
51  }
52  // Pre-classify all the blobs.
53  if (tessedit_parallelize > 1) {
54 #ifdef _OPENMP
55 # pragma omp parallel for num_threads(10)
56 #endif // _OPENMP
57  // NOLINTNEXTLINE(modernize-loop-convert)
58  for (size_t b = 0; b < blobs.size(); ++b) {
59  *blobs[b].choices =
60  blobs[b].tesseract->classify_blob(blobs[b].blob, "par", ScrollView::WHITE, nullptr);
61  }
62  } else {
63  // TODO(AMD) parallelize this.
64  for (auto &blob : blobs) {
65  *blob.choices = blob.tesseract->classify_blob(blob.blob, "par", ScrollView::WHITE, nullptr);
66  }
67  }
68 }
69 
70 } // namespace tesseract.
BLOB_CHOICE_LIST ** choices
Definition: par_control.cpp:35
BlobData(int index, Tesseract *tess, const WERD_RES &word)
Definition: par_control.cpp:28
void PrerecAllWordsPar(const std::vector< WordData > &words)
Definition: par_control.cpp:38
unsigned NumBlobs() const
Definition: blobs.h:449
tesseract::Tesseract * tesseract
Definition: pageres.h:278
TWERD * chopped_word
Definition: pageres.h:210
BLOB_CHOICE_LIST * classify_blob(TBLOB *blob, const char *string, ScrollView::Color color, BlamerBundle *blamer_bundle)
Definition: wordclass.cpp:52