15
votes

This is probably one of the strangest errors that I have ever ran into when using OpenCV. There is a lot going on, so let me try to explain this to the best of my ability.

  1. I am using the Django web framework and OpenCV (cv2) together. I am trying to read a file off my disk from a view in Django.

    imagePath = os.path.dirname(__file__) + "/1.jpg"
    

    Basically, in the same path as views.py file there is a file called "1.jpg". That is all this code is doing. Easy enough. But the next step is where things get crazy.

  2. Now, I want to read the image file located at 'imagePath'. This requires a call to cv2.imread

    image = cv2.imread(imagePath)
    

    But this is where my problems start. Somehow, Apache (or maybe even OpenCV, I can't tell) starts hanging and the file is never loaded. There is no error message, no nothing.

Doing some detective work I decided to try out an older version of OpenCV (import cv). Strangely enough, when I call cv.LoadImage(imagePath) Apache does not hang and my image is loaded just fine. I have absolutely no idea why.

A potential work around for my problem is to use PIL.

from PIL import Image
import numpy as np
image = Image.open(imagePath)
image = np.asarray(image)

One again, using PIL Apache does not hang and I can proceed as normal with my image represented as numpy array and apply any of the cv2 functions to it.

However, I'm not one to settle for workarounds and the fact that cv2.imread is hanging really bothers me.

Has anyone ran into this before?

EDIT: Using cv.imread from a Python shell works fine, it's just from an Apache request that the hang happens.

>>> import cv2
>>> image = cv2.imread("1.jpg")
>>> image.shape
(400, 344, 3)
>>> 
2
I was running OpenCV 2.4.1 when this problem occurred. And then compiled and installed 2.4.0 and for whatever reason, imread no longer hangs. Very strange.Adrian Rosebrock
I have the same problem using OpenCV 2.4.2, strange indeed...Tickon
I encountered this problem lately, is there any fix for older OpenCV Versions? It seems to work fine using OpenCV 2.4.6.1.Mailerdaimon
I'm not positive, but I think the issue was actually related to Python2.6 and the thread handling in WSGI. For the past year I have been using OpenCV 2.4.0 with Python2.7 and it has been working fine.Adrian Rosebrock
I encountered the problem with an embedded Python script with no Apache / WSGI Code. So the Bug could be related to Python 2.6 but not (only) to WSGI. I searched through the OpenCV Bugtracker and changes to imread but could not find anything related to this bug there. I will report here if i find out something more, as older Python /OpenCV Version are still widely used.Mailerdaimon

2 Answers

8
votes

I had a similar issue and found a fix -> just add to your apache configuration:

WSGIScriptAlias application-group=%{GLOBAL}

Apparently it happens when you have an extension module not designed to work in sub interpreter. The above forces it to run in main interpreter.

Sources: django apache mod-wsgi hangs on importing a python module from .so file http://blog.rtwilson.com/how-to-fix-flask-wsgi-webapp-hanging-when-importing-a-module-such-as-numpy-or-matplotlib/

3
votes

Wrong

imagePath = os.path.dirname(__file__) + "/1.jpg"

Right

from os.path import abspath, join, dirname

imagePath = abspath( join(dirname(__file__), "1.jpg") )