java - Not entering inside tessract method doOCR(File imageFile) -
i have created small console application ocr on .tiff image file, have done using tess4j.
public class javaapplication10 { /** * @param args command line arguments */ public static void main(string[] args) { file imagefile = new file("c:\\users\\manesh\\desktop\\license_plate.tiff"); tesseract instance = tesseract.getinstance(); // jna interface mapping // tesseract1 instance = new tesseract1(); // jna direct mapping try { string result = instance.doocr(imagefile); //empty result system.out.println("hahahaha"); system.out.println("the result is: " + result); } catch (tesseractexception e) { system.out.println("error:" + e); } } }
i'm not getting value inside result, when looked code of tesseract class , inserted couple of system.out.println not getting printed in console. tesseract code given below.
public class tesseract { private static tesseract instance; private final static rectangle empty_rectangle = new rectangle(); private string language = "eng"; private string datapath = "tessdata"; private int psm = tessapi.tesspagesegmode.psm_auto; private boolean hocr; private int pagenum; private int ocrenginemode = tessapi.tessocrenginemode.oem_default; private properties prop = new properties(); public final static string htmlbegintag = "<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\"" + " \"http://www.w3.org/tr/html4/loose.dtd\">\n" + "<html>\n<head>\n<title></title>\n" + "<meta http-equiv=\"content-type\" content=\"text/html;" + "charset=utf-8\" />\n<meta name='ocr-system' content='tesseract'/>\n" + "</head>\n<body>\n"; public final static string htmlendtag = "</body>\n</html>\n"; private tesseract() { system.setproperty("jna.encoding", "utf8"); } public static synchronized tesseract getinstance() { if (instance == null) { instance = new tesseract(); } return instance; } public void setdatapath(string datapath) { this.datapath = datapath; } public void setlanguage(string language) { this.language = language; } public void setocrenginemode(int ocrenginemode) { this.ocrenginemode = ocrenginemode; } public void setpagesegmode(int mode) { this.psm = mode; } public void sethocr(boolean hocr) { this.hocr = hocr; prop.setproperty("tessedit_create_hocr", hocr ? "1" : "0"); } public void settessvariable(string key, string value) { prop.setproperty(key, value); } public string doocr(file imagefile) throws tesseractexception { system.out.println("hiiiiiii "); //not getting printed return doocr(imagefile, null); } public string doocr(file imagefile, rectangle rect) throws tesseractexception { try { system.out.println("be: "); //not getting printed return doocr(imageiohelper.getiioimagelist(imagefile), rect); } catch (ioexception ioe) { throw new tesseractexception(ioe); } } public string doocr(bufferedimage bi) throws tesseractexception { return doocr(bi, null); } public string doocr(bufferedimage bi, rectangle rect) throws tesseractexception { iioimage oimage = new iioimage(bi, null, null); list<iioimage> imagelist = new arraylist<iioimage>(); imagelist.add(oimage); return doocr(imagelist, rect); } public string doocr(list<iioimage> imagelist, rectangle rect) throws tesseractexception { stringbuilder sb = new stringbuilder(); pagenum = 0; (iioimage oimage : imagelist) { pagenum++; try { bytebuffer buf = imageiohelper.getimagebytebuffer(oimage); renderedimage ri = oimage.getrenderedimage(); string pagetext = doocr(ri.getwidth(), ri.getheight(), buf, rect, ri.getcolormodel().getpixelsize()); sb.append(pagetext); } catch (ioexception ioe) { //skip problematic image system.err.println(ioe.getmessage()); } } if (hocr) { sb.insert(0, htmlbegintag).append(htmlendtag); } return sb.tostring(); } public string doocr(int xsize, int ysize, bytebuffer buf, rectangle rect, int bpp) throws tesseractexception { tessapi api = tessapi.instance; tessapi.tessbaseapi handle = api.tessbaseapicreate(); api.tessbaseapiinit2(handle, datapath, language, ocrenginemode); api.tessbaseapisetpagesegmode(handle, psm); enumeration em = prop.propertynames(); while (em.hasmoreelements()) { string key = (string) em.nextelement(); api.tessbaseapisetvariable(handle, key, prop.getproperty(key)); } int bytespp = bpp / 8; int bytespl = (int) math.ceil(xsize * bpp / 8.0); api.tessbaseapisetimage(handle, buf, xsize, ysize, bytespp, bytespl); if (rect != null && !rect.equals(empty_rectangle)) { api.tessbaseapisetrectangle(handle, rect.x, rect.y, rect.width, rect.height); } pointer utf8text = hocr ? api.tessbaseapigethocrtext(handle, pagenum - 1) : api.tessbaseapigetutf8text(handle); string str = utf8text.getstring(0); api.tessdeletetext(utf8text); api.tessbaseapidelete(handle); return str; } }
i'm using tesseract first time please tell me i'm doing wrong.
for tesseract have pass exact image want ocr on, example, suppose reading chest numbers of players, if pass cropped , gray scaled image of chest number read text, if pass whole image not read. can using.
string doocr(bufferedimage img, rectangle rect);
well i'm passing cropped image directly i'm not using above method, code looks rite now.
public class javaapplication10 { /** * @param args command line arguments */ public static void main(string[] args) { try { file imagefile = new file("c:\\users\\manesh\\desktop\\116.jpg"); //this cropped image of chest number. bufferedimage img = imageio.read(imagefile); //bufferedimageop grayscaleconv = new colorconvertop(colorframe.getcolormodel().getcolorspace(), grayscaleconv.filter(colorframe, grayframe); tesseract instance = tesseract.getinstance(); // jna interface mapping colorspace cs = colorspace.getinstance(colorspace.cs_gray); colorconvertop op = new colorconvertop(cs, null); op.filter(img, img); // gray scaling image // tesseract1 instance = new tesseract1(); // jna direct mapping try { string result = instance.doocr(img); system.out.println("hahahaha"); system.out.println("the result is: " + result); } catch (tesseractexception e) { system.out.println("error:" + e); } } catch (ioexception ex) { logger.getlogger(javaapplication10.class.getname()).log(level.severe, null, ex); } } }
this found please feel free correct me if i'm wrong anywhere.
Comments
Post a Comment