Menu

Stop tess4j

Alex Lopes
2018-04-27
2018-05-25
  • Alex Lopes

    Alex Lopes - 2018-04-27

    How can i stop the Tess4j process or set a time out?

     
  • Quan Nguyen

    Quan Nguyen - 2018-04-27
     

    Last edit: Quan Nguyen 2018-04-27
  • Alex Lopes

    Alex Lopes - 2018-05-02

    Thanks for reply, but I tested the code and still doesn't working. Sorry if it's a stupid question, I'm a beginner and trying to understand it. I have to set a diferent class for API? I'm using 'api = TessAPI.INSTANCE;'

     
  • Quan Nguyen

    Quan Nguyen - 2018-05-03

    Not really. There is an example in the unit tests -- you may want to take a look.

     
  • Alex Lopes

    Alex Lopes - 2018-05-11

    Hi, i've tried this week make what you said, but i hadn't sucess. I made the code of TessAPITest but it doesn't stopped, and I tried to use the 'end_time' variable, and it doesn't make sense to me. I read the doc and it says "time to stop if not 0", but it ever stop at same time and with blank out put.

    My code:

    Tesseract.java

        ETEXT_DESC monitor = new ETEXT_DESC();
        ITessAPI.TimeVal timeout = new ITessAPI.TimeVal();
        timeout.tv_sec = new NativeLong(0L); 
        monitor.end_time = timeout;
        ProgressMonitor pmo = new ProgressMonitor(monitor);
        pmo.start();
        api.TessBaseAPIRecognize(handle, monitor);
    

    ProgressMonitor.java

    public class ProgressMonitor extends Thread {
    
        ITessAPI.ETEXT_DESC monitor;
        StringBuilder outputMessage = new StringBuilder();
        long startTime;
    
        private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());
    
        public ProgressMonitor(ITessAPI.ETEXT_DESC monitor) {
            this.monitor = monitor;
            this.startTime = System.currentTimeMillis();
        }
    
        public String getMessage() {
            return outputMessage.toString();
        }
    
        @Override
        public void run() {
            try {
                while (true) {
                    logger.info("ocr alive: " + (monitor.ocr_alive == TRUE));
                    logger.info("progress: " + monitor.progress);
                    outputMessage.append(monitor.more_to_come);
                    System.out.println((System.currentTimeMillis()-endTime)/1000);
                    if (((System.currentTimeMillis()-tempoInicio)/1000) == 10 ) {
                        cancel();
                        break;
                    }
                    Thread.sleep(100);
                }
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
    
        /**
         * Cancels OCR operation.
         */
        public void cancel() {
            monitor.cancel = new ITessAPI.CANCEL_FUNC() {
                @Override
                public boolean invoke(Pointer cancel_this, int words) {
                    System.out.println("Calling with " + words  +" words");
                    return true;
                }
            };
        }
    
        /**
         * Resets cancel flag.
         */
        public void reset() {
            monitor.cancel = null;
        }
    }
    
     

    Last edit: Alex Lopes 2018-05-11
  • Quan Nguyen

    Quan Nguyen - 2018-05-12

    It looks almost correct. I would make a small correction:

    if (((System.currentTimeMillis() - startTime) / 1000) >= 10 ) {

    Nevertheless, it does not work anyway when I test now. If I remember correctly, it worked at one time when I first coded the cancellation. Things could have changed since inside Tesseract's native code.

     
  • Alex Lopes

    Alex Lopes - 2018-05-15

    Do you have any sugestion or another way to stop the tesseract process?

     
  • Quan Nguyen

    Quan Nguyen - 2018-05-15

    Tess4J operates on individual images. If you perform OCR on multiple images, as in multi-page TIFF, you can use SwingWorker to control, and cancel, the OCR process, which would still complete on the current image but not subsequent ones.

     
  • Alex Lopes

    Alex Lopes - 2018-05-25

    If I send a pdf file with a sigle page, would it work?

     
  • Quan Nguyen

    Quan Nguyen - 2018-05-25

    No, it would still finish that page. If the PDF has multiple pages, then yes.

     

Log in to post a comment.