Тессеракт OCR не работает для веб-приложения Java - PullRequest
0 голосов
/ 09 сентября 2018

Я пытаюсь разработать приложение веб-распознавания текста Tesseract для Java. Следующий код работает отлично:

public class App {

public String getImgText(String imageLocation) {
    ITesseract instance = new Tesseract();
    instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
    System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"tessdata\").getPath() : "+Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
    instance.setLanguage("eng");

    try {
        String imgText = instance.doOCR(new File(imageLocation));
        return imgText;
    } catch (TesseractException e) {
        e.getMessage();
        return "Error while reading image";
    }
}

public static void main(String[] args) {
    App app = new App();
    System.out.println(app.getImgText("/home/user/Desktop/1.png"));
}
}

Но когда я пытаюсь использовать приведенный выше код в своем веб-приложении Java (JSF) после строки

  ITesseract instance = new Tesseract();

ничего не распечатано. Ниже приведен код моего веб-приложения:

public String uploadImage(FileUploadEvent event) {
    System.out.println("webcore bean");
    //get uploaded file from the event
    UploadedFile uploadedFile = (UploadedFile) event.getFile();
    //create an InputStream from the uploaded file
    InputStream inputStr = null;
    try {
        inputStr = uploadedFile.getInputstream();
    } catch (IOException e) {
        //log error
    }

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String directory = externalContext.getInitParameter("uploadDirectory");
    String filename = FilenameUtils.getName(uploadedFile.getFileName());

    File destFile = new File(directory, "static" + getFileExtension(filename));

    //use org.apache.commons.io.FileUtils to copy the File
    try {
        FileUtils.copyInputStreamToFile(inputStr, destFile);
    } catch (IOException e) {
        //log error
    }
    System.out.println("getImageText(directory) : " + getImageText(directory));
    FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    return null;
}

private String getImageText(String imageLocation) {
    try {
        System.out.println("Before ");
        ITesseract instance = new Tesseract1();
        System.out.println("After ");
        //instance.setDatapath("/usr/share/tesseract-ocr/4.00/tessdata");
        instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
        instance.setLanguage("eng");

        try {
            String imgText = instance.doOCR(new File(imageLocation));
            return imgText;
        } catch (TesseractException e) {
            e.getMessage();
            return "Error while reading image";
        }
    } catch (Exception e) {
        System.out.println("Before returning null");
        e.printStackTrace();
        return null;
    }

}

Журнал «До» печатается, но журнал «После» не печатается. Я использую следующие технологии:

a) Ubuntu 18.04, 64-разрядная ОС

б) Netbeans

в) Maven

г) Glassfish 4.1

...