我正在使用tesseract识别来自pdfs的文本,我面临一些奇怪的错误。 错误是 打开数据文件数据/ 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;
}
}
如果有人发现任何异常情况,请回复。