faustomorales / keras-ocr

A packaged and flexible version of the CRAFT text detector and Keras CRNN recognition model.
https://keras-ocr.readthedocs.io/
MIT License
1.37k stars 349 forks source link

Segmentation fault (core dumped) when using the latest version of opencv-python (4.5.5.62) #189

Open Dobiasd opened 2 years ago

Dobiasd commented 2 years ago

Recognition attempts end with exit code 139 (interrupted by signal 11: SIGSEGV). Minimal example:

import keras_ocr

pipeline = keras_ocr.pipeline.Pipeline()
image = keras_ocr.tools.read('https://i.imgur.com/NpJdN6R.png')
pipeline.recognize([image])

The following Dockerfile can be used to reproduce the error:

FROM python:3.9.9

# libGL is needed to avoid "ImportError: libGL.so.1" in OpenCV
# libglib2.0-0 is needed to avoid "ImportError: libgthread-2.0.so.0" in OpenCV
RUN apt-get update && apt-get install -y \
  libglib2.0-0 \
  libgl1-mesa-glx \
  build-essential \
  && rm -rf /var/lib/apt/lists/*

# With the old opencv-python==4.5.4.60 things are fine.
RUN pip install --no-cache-dir tensorflow==2.7.0 keras-ocr==0.8.9 opencv-python==4.5.5.62

RUN echo "import keras_ocr" > main.py
RUN echo "pipeline = keras_ocr.pipeline.Pipeline()" >> main.py
RUN echo "image = keras_ocr.tools.read('https://i.imgur.com/NpJdN6R.png')" >> main.py
RUN echo "pipeline.recognize([image])" >> main.py

RUN python main.py

Result:

Segmentation fault (core dumped)
Dobiasd commented 2 years ago

With the debugger, I found the crash happens in the call to cv2.connectedComponentsWithStats in the getBoxes function.

I've created a minimal example to reproduce this OpenCV crash without keras-ocr, and opened an issue in the opencv-python repository: https://github.com/opencv/opencv-python/issues/604

parayamelo commented 2 years ago

Thanks @Dobiasd ! This helped me. It would be good to add a note on this.

Dobiasd commented 2 years ago

It seems the OpenCV devs have fixed it (https://github.com/opencv/opencv/issues/21366) already. Maybe it's just not yet released.

parayamelo commented 2 years ago

I had the latest version of opencv-python, and it was crashing. I guess it has not been released, as you say.

Thank you!

On Tue, Feb 8, 2022, 14:42 Tobias Hermann @.***> wrote:

It seems the OpenCV devs have fixed it (opencv/opencv#21366 https://github.com/opencv/opencv/issues/21366) already. Maybe it's just not yet released.

— Reply to this email directly, view it on GitHub https://github.com/faustomorales/keras-ocr/issues/189#issuecomment-1032623344, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACDJO3LMZOS2NMI7AWO5E5LU2EMVXANCNFSM5LFMQ5JA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

Dobiasd commented 2 years ago

Smalll update:

Using opencv-python==4.5.5.64 (released two days ago, i.e., on 2022-03-09) instead of opencv-python==4.5.5.62, it still produces the segfault.

FROM python:3.9.9

# libGL is needed to avoid "ImportError: libGL.so.1" in OpenCV
# libglib2.0-0 is needed to avoid "ImportError: libgthread-2.0.so.0" in OpenCV
RUN apt-get update && apt-get install -y \
  libglib2.0-0 \
  libgl1-mesa-glx \
  build-essential \
  && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir tensorflow==2.7.0 keras-ocr==0.8.9

# Tested using different versions here.
RUN pip install --no-cache-dir opencv-python==4.5.5.64

RUN echo "import keras_ocr" > main.py
RUN echo "pipeline = keras_ocr.pipeline.Pipeline()" >> main.py
RUN echo "image = keras_ocr.tools.read('https://i.imgur.com/NpJdN6R.png')" >> main.py
RUN echo "pipeline.recognize([image])" >> main.py

RUN python main.py
Segmentation fault (core dumped)

Results with different versions:

So let's hope for the next version, I guess. :shrug: