26#ifdef INCLUDE_TENSORFLOW
27# include <tensorflow/core/lib/core/threadpool.h>
29#include <allheaders.h>
49 "Maximum number of instances to run in parallel at any given "
50 "instant. The number of concurrent instances cannot exceed "
51 "reps * number_of_langs_tested, which is also the default value.");
55static const char *kTessLangs[] = {
"eng",
"vie",
nullptr};
56static const char *kTessImages[] = {
"HelloGoogle.tif",
"viet.tif",
nullptr};
57static const char *kTessTruthText[] = {
"Hello Google",
"\x74\x69\xe1\xba\xbf\x6e\x67",
nullptr};
59static const char *kCubeLangs[] = {
"hin",
"ara",
nullptr};
60static const char *kCubeImages[] = {
"raaj.tif",
"arabic.tif",
nullptr};
61static const char *kCubeTruthText[] = {
"\xe0\xa4\xb0\xe0\xa4\xbe\xe0\xa4\x9c",
62 "\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a",
nullptr};
67 CHECK(FLAGS_test_tesseract || FLAGS_test_cube)
68 <<
"Need to test at least one of Tesseract/Cube!";
70 std::vector<std::string> image_files;
71 if (FLAGS_test_tesseract) {
73 while (kTessLangs[
i] && kTessTruthText[
i] && kTessImages[
i]) {
74 langs_.emplace_back(kTessLangs[
i]);
76 image_files.emplace_back(kTessImages[
i]);
79 LOG(
INFO) <<
"Testing Tesseract on " <<
i <<
" languages.";
81 if (FLAGS_test_cube) {
83 while (kCubeLangs[
i] && kCubeTruthText[
i] && kCubeImages[
i]) {
84 langs_.emplace_back(kCubeLangs[
i]);
86 image_files.emplace_back(kCubeImages[
i]);
89 LOG(
INFO) <<
"Testing Cube on " <<
i <<
" languages.";
98 for (
int i = 0;
i < n; ++
i) {
99 std::string path = TESTING_DIR
"/" + image_files[
i %
num_langs_];
100 Image new_pix = pixRead(path.c_str());
101 QCHECK(new_pix !=
nullptr) <<
"Could not read " << path;
102 pix_.push_back(new_pix);
105#ifdef INCLUDE_TENSORFLOW
106 pool_size_ = (FLAGS_max_concurrent_instances < 1) ?
num_langs_ * FLAGS_reps
107 : FLAGS_max_concurrent_instances;
112 for (
auto &pix :
pix_) {
117#ifdef INCLUDE_TENSORFLOW
120 new tensorflow::thread::ThreadPool(tensorflow::Env::Default(),
"tessthread", pool_size_));
123 void WaitForPoolWorkers() {
124 pool_.reset(
nullptr);
127 std::unique_ptr<tensorflow::thread::ThreadPool> pool_;
128 static int pool_size_;
130 static std::vector<Image >
pix_;
137#ifdef INCLUDE_TENSORFLOW
138int BaseapiThreadTest::pool_size_;
145static void InitTessInstance(
TessBaseAPI *tess,
const std::string &lang) {
146 CHECK(tess !=
nullptr);
150static void GetCleanedText(
TessBaseAPI *tess, Image pix, std::string &ocr_text) {
152 char *result = tess->GetUTF8Text();
158#ifdef INCLUDE_TENSORFLOW
159static void VerifyTextResult(
TessBaseAPI *tess, Image pix,
const std::string &lang,
160 const std::string &expected_text) {
166 InitTessInstance(tess_local, lang);
168 std::string ocr_text;
169 GetCleanedText(tess_local, pix, ocr_text);
171 if (tess_local != tess) {
180 for (
int i = 0;
i < num_langs_; ++
i) {
182 InitTessInstance(&tess, langs_[
i]);
183 std::string ocr_text;
184 GetCleanedText(&tess, pix_[
i], ocr_text);
185 CHECK(strcmp(gt_text_[
i].c_str(), ocr_text.c_str()) == 0) <<
"Failed with lang = " << langs_[
i];
191#ifdef INCLUDE_TENSORFLOW
192 const int n = num_langs_ * FLAGS_reps;
194 std::vector<TessBaseAPI> tess(n);
195 for (
int i = 0;
i < n; ++
i) {
196 pool_->Schedule(std::bind(InitTessInstance, &tess[
i], langs_[
i % num_langs_]));
198 WaitForPoolWorkers();
204#ifdef INCLUDE_TENSORFLOW
205 const int n = num_langs_ * FLAGS_reps;
206 std::vector<TessBaseAPI> tess(n);
208 for (
int i = 0;
i < n; ++
i) {
209 InitTessInstance(&tess[
i], langs_[
i % num_langs_]);
213 for (
int i = 0;
i < n; ++
i) {
214 pool_->Schedule(std::bind(VerifyTextResult, &tess[
i], pix_[
i], langs_[
i % num_langs_],
215 gt_text_[
i % num_langs_]));
217 WaitForPoolWorkers();
222#ifdef INCLUDE_TENSORFLOW
223 const int n = num_langs_ * FLAGS_reps;
225 for (
int i = 0;
i < n; ++
i) {
226 pool_->Schedule(std::bind(VerifyTextResult,
nullptr, pix_[
i], langs_[
i % num_langs_],
227 gt_text_[
i % num_langs_]));
229 WaitForPoolWorkers();
struct TessBaseAPI TessBaseAPI
#define EXPECT_EQ(val1, val2)
#define EXPECT_STREQ(s1, s2)
INT_PARAM_FLAG(reps, 1, "Num of parallel test repetitions to run.")
BOOL_PARAM_FLAG(test_tesseract, true, "Test tesseract instances")
TEST_F(EuroText, FastLatinOCR)
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
static void TearDownTestCase()
static std::vector< std::string > langs_
static std::vector< Image > pix_
static void SetUpTestCase()
static std::vector< std::string > gt_text_