halkar / Tesseract.Xamarin

Tesseract OCR wrapper for Xamarin
MIT License
121 stars 40 forks source link

Application crash on IOS9 #20

Closed vdurif closed 8 years ago

vdurif commented 8 years ago

Hi,

I'm currently building an OCR app and I use Xamarin.Tesseract for IOS. When I use my camera preview screen and send data to tesseract using Recognize function the application just shut down 20 seconds later. In logs there's memory warnings, any idea about where it can come from ?

Here is a code sample from the part that might be "bugging": class DataOutputDelegate : AVCaptureVideoDataOutputSampleBufferDelegate {

        private TesseractApi api;
        private OcrController controller;
        private volatile bool processing;

        public DataOutputDelegate(OcrController controller) {
            this.controller = controller;
        }

        public async override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
        {
            Console.WriteLine ("Try processing :: ");
            try {
                if(!processing) {
                    var image = ImageFromSampleBuffer (sampleBuffer);

                    if(api == null) {
                        api = new TesseractApi();
                    }

                    if(api != null && image != null) {
                        Console.WriteLine ("Processing :: ");
                        TesseractUtils.Instance.Configure(api);
                        processing = true;
                        await api.Recognise(image); /// this hangs 
                        processing = false;
                        image.Dispose();
                        Console.WriteLine ("end processing :: ");
                    }
                }
            } catch (Exception e){
                Console.WriteLine (e);
            } finally {
                sampleBuffer.Dispose ();
            }
        }

        UIImage ImageFromSampleBuffer (CMSampleBuffer sampleBuffer)
        {
            // Get the CoreVideo image
            using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer) {
                // Lock the base address
                pixelBuffer.Lock (0);
                // Get the number of bytes per row for the pixel buffer
                var baseAddress = pixelBuffer.BaseAddress;
                int bytesPerRow = (int) pixelBuffer.BytesPerRow;
                int width = (int) pixelBuffer.Width;
                int height = (int) pixelBuffer.Height;
                var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
                // Create a CGImage on the RGB colorspace from the configured parameter above
                using (var cs = CGColorSpace.CreateDeviceRGB ()) {
                    using (var context = new CGBitmapContext (baseAddress, width, height, 8, bytesPerRow, cs, (CGImageAlphaInfo)flags)) {
                        using (CGImage cgImage = context.ToImage ()) {
                            pixelBuffer.Unlock (0);
                            return UIImage.FromImage (cgImage);
                        }
                    }
                }
            }
        }

        void TryDispose (IDisposable obj)
        {
            if (obj != null)
                obj.Dispose ();
        }
    }
}

Thanks !

halkar commented 8 years ago

Hi,

Will have a look today.

Artur.

halkar commented 8 years ago

Sorry, had no chance to have a look this week. Will try to check it out tomorrow.

halkar commented 8 years ago

Hey Vincent,

I'm still investigating the problem. Looks like there is a memory leak but I haven't find it yet.

Artur.

vdurif commented 8 years ago

Hi Artur !

Thanks a lot for looking at it ! :D

Vincent.

halkar commented 8 years ago

Issue should be fixed by now. Check out the last version 0.2.10.