evermeer / PassportScanner

Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.
BSD 3-Clause "New" or "Revised" License
457 stars 101 forks source link

Reduce Tesseract delay? #31

Closed alejandroruizponce closed 5 years ago

alejandroruizponce commented 5 years ago

Hi Edwin!

I've been making tests with this scanner and I noticed every time Tesseract is called to analyze the processed capture (according to the code is every 0.2 secs) cause a delay in the camera view and the view get freeze constantly. I guess Tesseract take time to read the eng.traineddata and that is the cause of the delay.

Is there some way to reduce that delay and get a camera view with no freezing?

evermeer commented 5 years ago

It looks like it already goes a lot smoother when adding an extra async call in the scan function like this:

    private func scanning() {
        DispatchQueue.global().asyncAfter(deadline: .now() + 0.2) {
            //print("Start OCR")
            self.pictureOutput = PictureOutput()
            self.pictureOutput.encodedImageFormat = .png
            self.pictureOutput.onlyCaptureNextFrame = true
            self.pictureOutput.imageAvailableCallback = { sourceImage in
                DispatchQueue.global().async() {
                    if self.processImage(sourceImage: sourceImage) { return }
                    // Not successful, start another scan
                    self.scanning()
                }
            }
            self.crop --> self.pictureOutput
        }
    }

Could you confirm that this indeed improving the perceived performance?

alejandroruizponce commented 5 years ago

Yes, it's that part of the code. It's provoking a delay in the camera. Maybe when Tesseract read trained data. Some way to solve it? Or to improve it?

evermeer commented 5 years ago

The 0.2 is not a delay. It's the time between scans. The tesseract data is only read once. The screen freezing is because of the OCR duration itself. With the code above it will be done on another thread and the screen will not freeze anymore. I will do some more tests and publish that change later this week.

evermeer commented 5 years ago

I have pushed the change as version 4.4.0 to cocoapods. Could you please confirm that it's improved?

Mattijah commented 5 years ago

There are several improvements that could be done in order to improve overall performance. I would probably start by using OCR-B traineddata instead of the regular English ones, this will not only increase accuracy but it should also positively affect speed.

Here is my implementation of the MRZ scanner. Feel free to try it out https://github.com/Mattijah/QKMRZScanner