问题
I am writing a program using tess4j.jar. The program is extracting text and its location from within an image. I get this error:
Exception in thread "main" java.lang.Error: Invalid memory access
at net.sourceforge.tess4j.TessAPI1.TessBaseAPIRecognize(Native Method)
at TesseractUtility.TessBoxForLogo.run(TessBoxForLogo.java:50)
The funny thing is that it does not appear for every image. Does anybody know where I have an error?
Here my code:
public static ArrayList<Info> run(String imageName, List<String[]> wordsToFind){
int WayToGetInfo = TessAPI1.TessPageIteratorLevel.RIL_TEXTLINE;
wordsToFind = ParsData.addAnomalie(wordsToFind);
//Prepare lists
ArrayList<Info> infos = new ArrayList<Info>();
ArrayList<Rect> rectangles = new ArrayList<Rect>();
//Buff image
File imageFile = new File(imageName);
BufferedImage image = null;
ByteBuffer buf = null;
int w = 0;
int h = 0;
try {
image = ImageIO.read(new FileInputStream(imageFile));
w = image.getWidth();
h = image.getHeight();
buf = ImageIOHelper.convertImageData(image);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Get image dimensions
int bpp = image.getColorModel().getPixelSize();
int bytespp = bpp / 8;
int bytespl = (int) Math.ceil(w * bytespp);
//Tess4 preparation
TessBaseAPI handler = TessAPI1.TessBaseAPICreate();
String treiningDataPath = "C:\\Users\\Karmen\\Documents\\Bitbucket\\Podjetje\\lib\\Tess4J\\tessdata";
String lang = "slv";
TessAPI1.TessBaseAPIInit3(handler, treiningDataPath, lang);
TessAPI1.TessBaseAPISetPageSegMode(handler, TessAPI1.TessPageSegMode.PSM_AUTO_ONLY);
TessAPI1.TessBaseAPISetImage(handler, buf, w, h, bytespp, bytespl);
TessAPI1.TessBaseAPIRecognize(handler, null);
TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handler);
Pointer str = TessAPI1.TessResultIteratorGetUTF8Text(ri, WayToGetInfo);
TessAPI1.TessPageIterator pi = TessAPI1.TessResultIteratorGetPageIterator(ri);
TessAPI1.TessPageIteratorBegin(pi);
//Iterate thru rows
while (TessAPI1.TessPageIteratorNext(pi,WayToGetInfo) == TessAPI1.TRUE){
str = TessAPI1.TessResultIteratorGetUTF8Text(ri, WayToGetInfo);
if (str!=null){
float confidence = TessAPI1.TessResultIteratorConfidence(ri, WayToGetInfo);
String word = str.getString(0);
IntBuffer leftB = IntBuffer.allocate(1);
IntBuffer topB = IntBuffer.allocate(1);
IntBuffer rightB = IntBuffer.allocate(1);
IntBuffer bottomB = IntBuffer.allocate(1);
TessAPI1.TessPageIteratorBoundingBox(pi, WayToGetInfo, leftB, topB, rightB, bottomB);
int left = leftB.get();
int top = topB.get();
int right = rightB.get();
int bottom = bottomB.get();
Rect rectangle1 = new Rect((int) (left-0.05*left),
(int) (top-0.05*top),
(int) ((right+0.05*right)-(left-0.05*left)),
(int) ((bottom+0.5*bottom)-(top-0.05*top)));
rectangles.add(rectangle1);
String kword = "";
for (String[] s:wordsToFind){
//System.out.println(s[1]);
if (word.toLowerCase().contains(s[1].toLowerCase())
){
Rect rect = new Rect(left, top, right-left,bottom-top);
infos.add(copmare(word, s, rect, confidence));
kword = s[0];
}
}
}
}
draw(imageName, rectangles);
return infos;
}
回答1:
Convert your image to gray scale before running through tesseract
回答2:
That exception is usually due to Tesseract being unable to find tessdata
folder. The datapath should be set to the parent of that folder.
String treiningDataPath = "C:\\Users\\Karmen\\Documents\\Bitbucket\\Podjetje\\lib\\Tess4J\\";
来源:https://stackoverflow.com/questions/35295582/tess4j-memory-access-error-in-tess4j-java