aboutsummaryrefslogtreecommitdiff
path: root/cpp/libchelleport.cpp
blob: ff3f7d637fa79bda58d42aecf0f6cc6d2e28a423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <leptonica/allheaders.h>
#include <memory>
#include <ostream>
#include <tesseract/baseapi.h>
#include <thread>
#include <vector>

#include "../include/image.h"
#include "../include/libchelleport.h"
#include "../include/recognizer.h"

extern "C" OCRMatch *findWordCoordinates(const char *image_path, int *size) {
  OCRMatchSet matches;
  MEASURE("OCR", { matches = extractTextMatches(image_path); });

  OCRMatch *ptr = new OCRMatch[matches.size()];
  std::copy(matches.begin(), matches.end(), ptr);

  *size = matches.size();
  std::cout << "Match count: " << *size << std::endl;

  return ptr;
}

OCRMatchSet extractTextMatches(const char *imagePath) {
  Pix *image = image::loadImage(imagePath);

  if (image == nullptr) {
    return OCRMatchSet();
  }

  // printf("imagePath: %s\n", imagePath);
  // pixWrite(imagePath, image, IFF_JFIF_JPEG);

  int width = pixGetWidth(image);
  int height = pixGetHeight(image);

  std::vector<std::unique_ptr<Recognizer>> recognizers;

  // clang-format off
  recognizers.push_back(
    std::make_unique<Recognizer>("top-left",      0,          0,           width / 2,  height / 2));
  recognizers.push_back(
    std::make_unique<Recognizer>("top-right",     width / 2,  0,           width / 2,  height / 2));
  recognizers.push_back(
    std::make_unique<Recognizer>("bottom-left",   0,          height / 2,  width / 2,  height / 2));
  recognizers.push_back(
    std::make_unique<Recognizer>("bottom-right",  width / 2,  height / 2,  width / 2,  height / 2));
  // clang-format on

  return runRecognizers(recognizers, image);
}

OCRMatchSet
runRecognizers(std::vector<std::unique_ptr<Recognizer>> &recognizers,
               Pix *image) {
  OCRMatchSet results;
  std::shared_ptr<Pix> sharedImage(image, [](Pix *p) { pixDestroy(&p); });

  std::vector<std::thread> workers;
  workers.reserve(recognizers.size());

  for (auto &recognizer : recognizers) {
    workers.push_back(std::thread([&recognizer, &sharedImage]() {
      MEASURE(recognizer->id, { recognizer->recognize(sharedImage.get()); })
    }));
  }

  for (std::thread &t : workers) {
    if (t.joinable())
      t.join();
  }

  for (auto &recognizer : recognizers) {
    for (auto &match : recognizer->getResults())
      results.insert(match);
  }

  return results;
}