我正在使用 tesseract 识别 pdf 中的文本,但我遇到了一些奇怪的错误。错误是 Error opening data file data/tessdata/eng.traineddata 请确保将 TESSDATA_PREFIX 环境变量设置为“tessdata”目录的父目录。
现在,我理解了这个错误的含义,并且我的路径已更新到数据文件夹的父目录。但奇怪的是,当我运行我的代码时,我并没有立即得到这个错误,而是在识别 10-15 个 pdf(每个大约 40 页)后得到它。在我收到此错误并从程序之前停止的 pdf 再次运行我的程序后,我在另外 10-15 个 pdf 读数中没有收到任何错误。
现在这很奇怪,我不明白这背后的原因。如果有人遇到过这种情况或知道这背后的原因,请发表评论。
这是我用来识别 pdf 特定页面中的文本的功能。我为每个 pdf 的每一页调用这个函数。
public static void getOCRDataFromPage(PDDocument pdDoc, int page, ArrayList<Characters> ch)
{
TessBaseAPI handle=TessAPI1.TessBaseAPICreate();
TessAPI1.TessBaseAPIInit3(handle, "data", "eng");
try
{
double w=pdDoc.getPage(page-1).getCropBox().getWidth();
double h=pdDoc.getPage(page-1).getCropBox().getHeight();
PDFRenderer pdfRenderer = new PDFRenderer(pdDoc);
BufferedImage image = pdfRenderer.renderImageWithDPI(page-1, 300,ImageType.GRAY);
//image=ImageHelper.convertImageToGrayscale(image);
ImageIOUtil.writeImage(image,"G:/Trial/tempImg.png", 300);
int bpp = image.getColorModel().getPixelSize();
int bytespp = bpp / 8;
int bytespl = (int) Math.ceil(image.getWidth() * bpp / 8.0);
int height = image.getHeight();
int width = image.getWidth();
TessAPI1.TessBaseAPISetImage(handle, ImageIOHelper.convertImageData(image), width, height, bytespp, bytespl);
TessAPI1.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO);
image.flush();
image=null;
ETEXT_DESC monitor = new ETEXT_DESC();
TessAPI1.TessBaseAPIRecognize(handle, monitor);
TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handle);
TessPageIterator pi = TessAPI1.TessResultIteratorGetPageIterator(ri);
TessAPI1.TessPageIteratorBegin(pi);
int level = TessAPI1.TessPageIteratorLevel.RIL_WORD;
ArrayList<Words> wd=new ArrayList<Words>();
do {
Pointer ptr = TessAPI1.TessResultIteratorGetUTF8Text(ri, level);
if(ptr == null || ptr.toString().length()==0)
break;
String word = ptr.getString(0);
TessAPI1.TessDeleteText(ptr);
float confidence = TessAPI1.TessResultIteratorConfidence(ri, level);
IntBuffer leftB = IntBuffer.allocate(1);
IntBuffer topB = IntBuffer.allocate(1);
IntBuffer rightB = IntBuffer.allocate(1);
IntBuffer bottomB = IntBuffer.allocate(1);
TessAPI1.TessPageIteratorBoundingBox(pi, level, leftB, topB, rightB, bottomB);
int left = leftB.get();
int top = topB.get();
int right = rightB.get();
int bottom = bottomB.get();
IntBuffer boldB = IntBuffer.allocate(1);
IntBuffer italicB = IntBuffer.allocate(1);
IntBuffer underlinedB = IntBuffer.allocate(1);
IntBuffer monospaceB = IntBuffer.allocate(1);
IntBuffer serifB = IntBuffer.allocate(1);
IntBuffer smallcapsB = IntBuffer.allocate(1);
IntBuffer pointSizeB = IntBuffer.allocate(1);
IntBuffer fontIdB = IntBuffer.allocate(1);
String fontName = TessAPI1.TessResultIteratorWordFontAttributes(ri, boldB, italicB, underlinedB,
monospaceB, serifB, smallcapsB, pointSizeB, fontIdB);
boolean bold = boldB.get() == TRUE;
boolean italic = italicB.get() == TRUE;
boolean underlined = underlinedB.get() == TRUE;
boolean monospace = monospaceB.get() == TRUE;
boolean serif = serifB.get() == TRUE;
boolean smallcaps = smallcapsB.get() == TRUE;
int pointSize = pointSizeB.get();
int fontId = fontIdB.get();
Words chr=new Words(); // Words is a user-defined class
chr.c=word.concat(" ");
chr.b=bold==true?1:0;
chr.x=left*w/width;
chr.size=pointSize;
chr.name=fontName;
chr.y=top*h/height;
chr.w=(right-left)*w/width;
chr.h=(bottom-top)*h/height;
if(pointSize>1)
wd.add(chr);
} while (TessAPI1.TessPageIteratorNext(pi, level) == TRUE);
sortWords(wd);
wordsToChar(wd,ch);
wd=null;
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
finally
{
//TessAPI1.TessBaseAPIClearAdaptiveClassifier(handle);
TessAPI1.TessBaseAPIClearPersistentCache(handle);
TessAPI1.TessBaseAPIClear(handle);
//TessAPI1.TessBaseAPIDelete(handle);
TessAPI1.TessBaseAPIEnd(handle);
handle=null;
}
}
如果有人发现任何异常情况,请回复。