@luii/node-tesseract-ocr 2.1.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,23 +35,48 @@ private:
35
35
  static Napi::FunctionReference constructor;
36
36
 
37
37
  // JS Methods
38
+ Napi::Value Version(const Napi::CallbackInfo &info);
39
+ Napi::Value IsInitialized(const Napi::CallbackInfo &info);
40
+ Napi::Value SetInputName(const Napi::CallbackInfo &info);
41
+ Napi::Value GetInputName(const Napi::CallbackInfo &info);
42
+ Napi::Value SetInputImage(const Napi::CallbackInfo &info);
43
+ Napi::Value GetInputImage(const Napi::CallbackInfo &info);
44
+ Napi::Value GetSourceYResolution(const Napi::CallbackInfo &info);
45
+ Napi::Value GetDataPath(const Napi::CallbackInfo &info);
46
+ Napi::Value SetOutputName(const Napi::CallbackInfo &info);
47
+ Napi::Value ClearPersistentCache(const Napi::CallbackInfo &info);
48
+ Napi::Value ClearAdaptiveClassifier(const Napi::CallbackInfo &info);
49
+ Napi::Value GetThresholdedImage(const Napi::CallbackInfo &info);
50
+ Napi::Value GetThresholdedImageScaleFactor(const Napi::CallbackInfo &info);
38
51
  Napi::Value Init(const Napi::CallbackInfo &info);
39
52
  Napi::Value InitForAnalysePage(const Napi::CallbackInfo &info);
40
- Napi::Value AnalysePage(const Napi::CallbackInfo &info);
53
+ Napi::Value AnalyseLayout(const Napi::CallbackInfo &info);
54
+ Napi::Value BeginProcessPages(const Napi::CallbackInfo &info);
55
+ Napi::Value AddProcessPage(const Napi::CallbackInfo &info);
56
+ Napi::Value FinishProcessPages(const Napi::CallbackInfo &info);
57
+ Napi::Value AbortProcessPages(const Napi::CallbackInfo &info);
58
+ Napi::Value GetProcessPagesStatus(const Napi::CallbackInfo &info);
59
+ Napi::Value SetDebugVariable(const Napi::CallbackInfo &info);
41
60
  Napi::Value SetVariable(const Napi::CallbackInfo &info);
42
61
  Napi::Value GetVariable(const Napi::CallbackInfo &info);
43
62
  Napi::Value GetIntVariable(const Napi::CallbackInfo &info);
44
63
  Napi::Value GetBoolVariable(const Napi::CallbackInfo &info);
45
64
  Napi::Value GetDoubleVariable(const Napi::CallbackInfo &info);
46
65
  Napi::Value GetStringVariable(const Napi::CallbackInfo &info);
47
- Napi::Value PrintVariables(const Napi::CallbackInfo &info);
48
66
  Napi::Value SetImage(const Napi::CallbackInfo &info);
67
+ // Napi::Value PrintVariables(const Napi::CallbackInfo &info);
49
68
  Napi::Value SetPageMode(const Napi::CallbackInfo &info);
50
69
  Napi::Value SetRectangle(const Napi::CallbackInfo &info);
51
70
  Napi::Value SetSourceResolution(const Napi::CallbackInfo &info);
52
71
  Napi::Value Recognize(const Napi::CallbackInfo &info);
53
72
  Napi::Value DetectOrientationScript(const Napi::CallbackInfo &info);
54
73
  Napi::Value MeanTextConf(const Napi::CallbackInfo &info);
74
+ Napi::Value AllWordConfidences(const Napi::CallbackInfo &info);
75
+ Napi::Value GetPAGEText(const Napi::CallbackInfo &info);
76
+ Napi::Value GetLSTMBoxText(const Napi::CallbackInfo &info);
77
+ Napi::Value GetBoxText(const Napi::CallbackInfo &info);
78
+ Napi::Value GetWordStrBoxText(const Napi::CallbackInfo &info);
79
+ Napi::Value getOSDText(const Napi::CallbackInfo &info);
55
80
  Napi::Value GetUTF8Text(const Napi::CallbackInfo &info);
56
81
  Napi::Value GetHOCRText(const Napi::CallbackInfo &info);
57
82
  Napi::Value GetTSVText(const Napi::CallbackInfo &info);
@@ -19,12 +19,125 @@
19
19
  #include <exception>
20
20
  #include <memory>
21
21
  #include <mutex>
22
+ #include <optional>
22
23
  #include <queue>
23
24
  #include <stop_token>
24
25
  #include <thread>
26
+ #include <type_traits>
25
27
  #include <variant>
26
28
  #include <vector>
27
29
 
30
+ namespace {
31
+
32
+ std::string CommandName(const Command &command) {
33
+ return std::visit(
34
+ [](const auto &c) -> std::string {
35
+ using T = std::decay_t<decltype(c)>;
36
+ if constexpr (std::is_same_v<T, CommandVersion>)
37
+ return "version";
38
+ if constexpr (std::is_same_v<T, CommandIsInitialized>)
39
+ return "isInitialized";
40
+ if constexpr (std::is_same_v<T, CommandInit>)
41
+ return "init";
42
+ if constexpr (std::is_same_v<T, CommandInitForAnalysePage>)
43
+ return "initForAnalysePage";
44
+ if constexpr (std::is_same_v<T, CommandSetVariable>)
45
+ return "setVariable";
46
+ if constexpr (std::is_same_v<T, CommandSetDebugVariable>)
47
+ return "setDebugVariable";
48
+ if constexpr (std::is_same_v<T, CommandGetIntVariable>)
49
+ return "getIntVariable";
50
+ if constexpr (std::is_same_v<T, CommandGetBoolVariable>)
51
+ return "getBoolVariable";
52
+ if constexpr (std::is_same_v<T, CommandGetDoubleVariable>)
53
+ return "getDoubleVariable";
54
+ if constexpr (std::is_same_v<T, CommandGetStringVariable>)
55
+ return "getStringVariable";
56
+ if constexpr (std::is_same_v<T, CommandSetInputName>)
57
+ return "setInputName";
58
+ if constexpr (std::is_same_v<T, CommandGetInputName>)
59
+ return "getInputName";
60
+ if constexpr (std::is_same_v<T, CommandSetOutputName>)
61
+ return "setOutputName";
62
+ if constexpr (std::is_same_v<T, CommandGetDataPath>)
63
+ return "getDataPath";
64
+ if constexpr (std::is_same_v<T, CommandSetInputImage>)
65
+ return "setInputImage";
66
+ if constexpr (std::is_same_v<T, CommandGetInputImage>)
67
+ return "getInputImage";
68
+ if constexpr (std::is_same_v<T, CommandSetPageMode>)
69
+ return "setPageMode";
70
+ if constexpr (std::is_same_v<T, CommandSetRectangle>)
71
+ return "setRectangle";
72
+ if constexpr (std::is_same_v<T, CommandSetSourceResolution>)
73
+ return "setSourceResolution";
74
+ if constexpr (std::is_same_v<T, CommandGetSourceYResolution>)
75
+ return "getSourceYResolution";
76
+ if constexpr (std::is_same_v<T, CommandSetImage>)
77
+ return "setImage";
78
+ if constexpr (std::is_same_v<T, CommandGetThresholdedImage>)
79
+ return "getThresholdedImage";
80
+ if constexpr (std::is_same_v<T, CommandGetThresholdedImageScaleFactor>)
81
+ return "getThresholdedImageScaleFactor";
82
+ if constexpr (std::is_same_v<T, CommandRecognize>)
83
+ return "recognize";
84
+ if constexpr (std::is_same_v<T, CommandAnalyseLayout>)
85
+ return "analyseLayout";
86
+ if constexpr (std::is_same_v<T, CommandDetectOrientationScript>)
87
+ return "detectOrientationScript";
88
+ if constexpr (std::is_same_v<T, CommandMeanTextConf>)
89
+ return "meanTextConf";
90
+ if constexpr (std::is_same_v<T, CommandAllWordConfidences>)
91
+ return "allWordConfidences";
92
+ if constexpr (std::is_same_v<T, CommandGetUTF8Text>)
93
+ return "getUTF8Text";
94
+ if constexpr (std::is_same_v<T, CommandGetHOCRText>)
95
+ return "getHOCRText";
96
+ if constexpr (std::is_same_v<T, CommandGetTSVText>)
97
+ return "getTSVText";
98
+ if constexpr (std::is_same_v<T, CommandGetUNLVText>)
99
+ return "getUNLVText";
100
+ if constexpr (std::is_same_v<T, CommandGetALTOText>)
101
+ return "getALTOText";
102
+ if constexpr (std::is_same_v<T, CommandGetPAGEText>)
103
+ return "getPAGEText";
104
+ if constexpr (std::is_same_v<T, CommandGetLSTMBoxText>)
105
+ return "getLSTMBoxText";
106
+ if constexpr (std::is_same_v<T, CommandGetBoxText>)
107
+ return "getBoxText";
108
+ if constexpr (std::is_same_v<T, CommandGetWordStrBoxText>)
109
+ return "getWordStrBoxText";
110
+ if constexpr (std::is_same_v<T, CommandGetOSDText>)
111
+ return "getOSDText";
112
+ if constexpr (std::is_same_v<T, CommandBeginProcessPages>)
113
+ return "beginProcessPages";
114
+ if constexpr (std::is_same_v<T, CommandAddProcessPage>)
115
+ return "addProcessPage";
116
+ if constexpr (std::is_same_v<T, CommandFinishProcessPages>)
117
+ return "finishProcessPages";
118
+ if constexpr (std::is_same_v<T, CommandAbortProcessPages>)
119
+ return "abortProcessPages";
120
+ if constexpr (std::is_same_v<T, CommandGetInitLanguages>)
121
+ return "getInitLanguages";
122
+ if constexpr (std::is_same_v<T, CommandGetLoadedLanguages>)
123
+ return "getLoadedLanguages";
124
+ if constexpr (std::is_same_v<T, CommandGetAvailableLanguages>)
125
+ return "getAvailableLanguages";
126
+ if constexpr (std::is_same_v<T, CommandClearPersistentCache>)
127
+ return "clearPersistentCache";
128
+ if constexpr (std::is_same_v<T, CommandClearAdaptiveClassifier>)
129
+ return "clearAdaptiveClassifier";
130
+ if constexpr (std::is_same_v<T, CommandClear>)
131
+ return "clear";
132
+ if constexpr (std::is_same_v<T, CommandEnd>)
133
+ return "end";
134
+ return "unknown";
135
+ },
136
+ command);
137
+ }
138
+
139
+ } // namespace
140
+
28
141
  WorkerThread::WorkerThread(Napi::Env env)
29
142
  : _env(env),
30
143
  _main_thread(Napi::ThreadSafeFunction::New(
@@ -51,7 +164,14 @@ void WorkerThread::MakeCallback(std::shared_ptr<Job> *p_job) {
51
164
  delete _job;
52
165
 
53
166
  if (job->error.has_value()) {
54
- job->deffered.Reject(Napi::Error::New(env, *job->error).Value());
167
+ Napi::Error error = Napi::Error::New(env, *job->error);
168
+ if (job->error_code.has_value()) {
169
+ error.Set("code", Napi::String::New(env, *job->error_code));
170
+ }
171
+ if (job->error_method.has_value()) {
172
+ error.Set("method", Napi::String::New(env, *job->error_method));
173
+ }
174
+ job->deffered.Reject(error.Value());
55
175
  return;
56
176
  }
57
177
 
@@ -69,6 +189,7 @@ void WorkerThread::MakeCallback(std::shared_ptr<Job> *p_job) {
69
189
  }
70
190
 
71
191
  void WorkerThread::Run(std::stop_token token) {
192
+ std::optional<ProcessPagesSession> process_pages_session;
72
193
 
73
194
  auto drain_queue = [&](std::vector<std::shared_ptr<Job>> &pending_jobs) {
74
195
  while (!_request_queue.empty()) {
@@ -81,6 +202,8 @@ void WorkerThread::Run(std::stop_token token) {
81
202
  const std::vector<std::shared_ptr<Job>> &pending_jobs) {
82
203
  for (const auto &pending_job : pending_jobs) {
83
204
  pending_job->error = message;
205
+ pending_job->error_code = "ERR_WORKER_STOPPED";
206
+ pending_job->error_method = CommandName(pending_job->command);
84
207
  auto *sp_pending_job = new std::shared_ptr<Job>(pending_job);
85
208
  MakeCallback(sp_pending_job);
86
209
  }
@@ -113,12 +236,33 @@ void WorkerThread::Run(std::stop_token token) {
113
236
 
114
237
  try {
115
238
  job->result = std::visit(
116
- [&](const auto &command) -> Result { return command.invoke(_api); },
239
+ [&](const auto &command) -> Result {
240
+ if constexpr (requires {
241
+ command.invoke(_api, process_pages_session,
242
+ _initialized);
243
+ }) {
244
+ return command.invoke(_api, process_pages_session, _initialized);
245
+ } else if constexpr (requires {
246
+ command.invoke(_api, _initialized);
247
+ }) {
248
+ return command.invoke(_api, _initialized);
249
+ } else if constexpr (requires {
250
+ command.invoke(_api, process_pages_session);
251
+ }) {
252
+ return command.invoke(_api, process_pages_session);
253
+ } else {
254
+ return command.invoke(_api);
255
+ }
256
+ },
117
257
  job->command);
118
258
  } catch (const std::exception &error) {
119
259
  job->error = error.what();
260
+ job->error_code = "ERR_TESSERACT_RUNTIME";
261
+ job->error_method = CommandName(job->command);
120
262
  } catch (...) {
121
263
  job->error = "Something unexpected happened";
264
+ job->error_code = "ERR_TESSERACT_RUNTIME";
265
+ job->error_method = CommandName(job->command);
122
266
  }
123
267
 
124
268
  auto *sp_job = new std::shared_ptr<Job>(job);
@@ -49,6 +49,7 @@ private:
49
49
  std::queue<std::shared_ptr<Job>> _request_queue;
50
50
 
51
51
  tesseract::TessBaseAPI _api;
52
+ std::atomic<bool> _initialized{false};
52
53
 
53
54
  std::jthread _worker_thread;
54
55
  };
@@ -63,7 +64,9 @@ template <typename C> Napi::Promise WorkerThread::Enqueue(C &&command) {
63
64
  std::stop_token token = _worker_thread.get_stop_token();
64
65
 
65
66
  if (_closing.load() || token.stop_requested()) {
66
- deferred.Reject(Napi::Error::New(_env, "Worker is closing").Value());
67
+ Napi::Error error = Napi::Error::New(_env, "Worker is closing");
68
+ error.Set("code", Napi::String::New(_env, "ERR_WORKER_CLOSED"));
69
+ deferred.Reject(error.Value());
67
70
  return deferred.Promise();
68
71
  }
69
72