aspose file tools*
The moose likes Other Java Products and Servers and the fly likes Web Service For Optical Character Recognition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Web Service For Optical Character Recognition " Watch "Web Service For Optical Character Recognition " New topic
Author

Web Service For Optical Character Recognition

prakash manem
Greenhorn

Joined: Nov 17, 2011
Posts: 5
I created RestFul Web Service for Optical Character Recognition Using Tesseract OCR Engine with J4L ocr Wrapper. I got javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class com.java4less.ocr.tess3.Wrapper
this Error.


My Web Service:

public static String getText(@PathParam("image") String img){

OCRFacade facade=new OCRFacade();
System.out.println("facade object created----------------> "+facade.hashCode());
System.out.println(img);
java.io.File currentDir= new java.io.File(img);

String path=currentDir.getAbsolutePath();
String text=facade.recognizeFile(path+".png", "eng");
System.out.println(text);
return text;
}

Can anyony one solve my problem.


Thanks,
Surya



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Welcome to the Ranch!

Please CarefullyChooseOneForum. This forum is about user interface questions; I don't see any user interface specific issue in your question. I'll move this thread to a better location.

The cause of the problem is a missing library on the class path. The class com.java4less.ocr.tess3.Wrapper class can be found, but a class it requires cannot. Check the website for all dependencies, and add those to the web application's WEB-INF/lib folder as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
prakash manem
Greenhorn

Joined: Nov 17, 2011
Posts: 5
My Web services is working fine for J4L wrapper on Tesseract .Now I added another method for Tess4j wrapper on Tesseract.But its not working.I got Exceptions like:



23 Nov, 2011 3:47:29 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.util.NoSuchElementException
at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:816)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:801)
at net.sourceforge.vietocr.ImageIOHelper.getImageByteBuffer(ImageIOHelper.java:189)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:133)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:88)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:75)
at org.ocr.OcrDAO.ocrTess4j(OcrDAO.java:141)
at org.ocr.OcrResource.getTess4jText(OcrResource.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:851)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:278)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
23 Nov, 2011 3:47:29 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey] in context with path [/OcrWebService] threw exception
java.util.NoSuchElementException
at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:816)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:801)
at net.sourceforge.vietocr.ImageIOHelper.getImageByteBuffer(ImageIOHelper.java:189)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:133)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:88)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:75)
at org.ocr.OcrDAO.ocrTess4j(OcrDAO.java:141)
at org.ocr.OcrResource.getTess4jText(OcrResource.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:851)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:278)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

My code:

public static String ocrTess4j(HttpServletRequest req) throws Exception
{
// InputStream uploadedInputStream = null;
if (ServletFileUpload.isMultipartContent(req)) {

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {

items = upload.parseRequest(req);

if (items != null) {
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField() && item.getSize() > 0) {
fileName = item.getName();
InputStream is = item.getInputStream();
System.out.println("Image Name------------>"+fileName);

String uploadedFileLocation = "D://OCR_Images/" + fileName;


// save it
writeToFile(is, uploadedFileLocation);

//String output = "File uploaded to : " + uploadedFileLocation;
System.out.println("Image Path-------------->"+uploadedFileLocation);


File imageFile = new File("D://OCR_Images/"+fileName);
System.out.println("-----------------"+imageFile);
Tesseract instance = Tesseract.getInstance();

text = instance.doOCR(imageFile);
System.out.println("result:\n"+text);

saveText();

}
}
}
}
catch (TesseractException e) {

System.err.println(e.getMessage());
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

return text;

}
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 144
Where is stack trace pointing to?is it this line -
FileItem item = iter.next();

?
prakash manem
Greenhorn

Joined: Nov 17, 2011
Posts: 5
I got problem here:

text = instance.doOCR(imageFile);


When I run this java file using standalone application this ocr process is working fine and give the output.But when i integrated this code to Web Services its getting problem in the above line.




thanks,

Surya
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 144
What I meant was the line number that is coming in your exception stack trace is pointing to which line in your code.
how can "text = instance.doOCR(imageFile);" this line result in this error -

java.util.NoSuchElementException
at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:816)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:801)


I basically want you to look at this line - "at org.ocr.OcrDAO.ocrTess4j(OcrDAO.java:141) " - the exception is pointing to 141st line of OcrDAO.java.
prakash manem
Greenhorn

Joined: Nov 17, 2011
Posts: 5

Hi thanks for your Reply.

public static String ocrTess4j(HttpServletRequest req) throws Exception
{
// InputStream uploadedInputStream = null;
if (ServletFileUpload.isMultipartContent(req)) {

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {

items = upload.parseRequest(req);

if (items != null) {
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField() && item.getSize() > 0) {
fileName = item.getName();
InputStream is = item.getInputStream();
System.out.println("Image Name------------>"+fileName);

String uploadedFileLocation = "D://OCR_Images/" + fileName;


// save it
writeToFile(is, uploadedFileLocation);

//String output = "File uploaded to : " + uploadedFileLocation;
System.out.println("Image Path-------------->"+uploadedFileLocation);


File imageFile = new File("D://OCR_Images/"+fileName);
System.out.println("-----------------"+imageFile);
Tesseract instance = Tesseract.getInstance();

text = instance.doOCR(imageFile);


In the above code i got the image file using this line

File imageFile = new File("D://OCR_Images/"+fileName);

I created tesseract instance and pass this file to doOCR() like below:

Tesseract instance = Tesseract.getInstance();

text = instance.doOCR(imageFile); //this is 141 line


But doOCR() cannot process the OCR operation to my imageFile .









Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 144
This seems to be Tesseract api issue - someone who knows about that API would be able to help.
Just one confirmation - The image path is correct ?Are you deploying your code on Windows OS?
prakash manem
Greenhorn

Joined: Nov 17, 2011
Posts: 5
Thanks Jitesh Sinha .


My ImagePath is Correct and I am using Windows 7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Web Service For Optical Character Recognition
 
Similar Threads
Read images in PDF document
Read character from the tiff image file
Project Idea
Recognising Characters
How to read a text from an image