使用多线程,但请注意为 TessBaseAPI 每个线程创建一个实例。不要在不同的线程之间共享它们。创建 N 个线程(N >= 核心数),java 将确保您至少加速核心数倍。
我所做的是创建 N 个线程,它们在自己的上下文中(在 run 方法中)创建 TessBaseAPI 对象,并在循环中等待 OCR 请求直到被中断。
...
...
@Override
public void run() {
TessBaseAPI tessBaseApi = new TessBaseAPI();
tessBaseApi.init(Ocrrrer.DATA_PATH, "eng");
setTessVariable(tessBaseApi, "load_system_dawg", "0");
setTessVariable(tessBaseApi, "load_freq_dawg", "0");
setTessVariable(tessBaseApi, "load_unambig_dawg", "0");
setTessVariable(tessBaseApi, "load_punc_dawg", "0");
setTessVariable(tessBaseApi, "load_number_dawg", "0");
setTessVariable(tessBaseApi, "load_fixed_length_dawgs", "0");
setTessVariable(tessBaseApi, "load_bigram_dawg", "0");
setTessVariable(tessBaseApi, "wordrec_enable_assoc", "0");
setTessVariable(tessBaseApi, "tessedit_enable_bigram_correction", "0");
setTessVariable(tessBaseApi, "assume_fixed_pitch_char_segment", "1");
setTessVariable(tessBaseApi, TessBaseAPI.VAR_CHAR_WHITELIST, "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ<");
Log.d(TAG, "Training file loaded");
while (!interrupted()) {
reentrantLock.lock();
try {
Log.d(TAG, this.getName() + " wait for OCR");
jobToDo.await();
Log.d(TAG, this.getName() + " input arrived. Do OCR");
this.ocrResult = doOcr(tessBaseApi);
ocrDone.signalAll();
} catch (InterruptedException e) {
return;
} finally {
try {
reentrantLock.unlock();
} catch (Exception ex) {
}
}
}
}
...
...
您可以看到 tessBaseApi 对象是 run 方法的本地对象,因此绝对不共享。