1

I have a set of web APIs that I run on Apache with WSGI for image processing. Recently I upgraded my OpenCV to 3.2 (I'm using Python 2.7) OpenCV seems to work fine when I run it from console, but when I make web API calls openCV hangs on converting image to grayscale:

cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

It's totally random, couldn't find a pattern. Some times it hangs and some times it works for the same image. Any ideas?

More Info: When I do it from shell command all works fine:

>>> sudo python manage.py shell
>>> import cv2
>>> import numpy as np
>>> from PIL import Image
>>> image = Image.open(img_path)
>>> image = np.asarray(image)
>>> print image
    array([[[255, 255, 255],
            [255, 255, 255],
            [255, 255, 255],...]]], dtype=uint8)
>>> img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
>>> print img
    array([[255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       ..., 
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

UPDATE: The issue is with WSGI. When I specify more than 1 process in apache config for WSGIDaemonProcess I can easily reproduce the hangs. After setting it to 1 it works

WSGIDaemonProcess processes=1 threads=25 maximum-requests=100

I'm still not sure why multiple processes in wsgi cause troubles

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Have you inspected the image sizes when it hangs? – ilke444 Jan 30 '17 at 19:42
  • @ilke444 the one that I found hanging consistently is 852x852. Here it is: https://drive.google.com/open?id=0B_eaJCfhUFmFWmdJQzhKVGJNbDQ – Dmitry Birulia Jan 30 '17 at 20:33
  • Possible duplicate of [OpenCV imread hanging when called from a web request](http://stackoverflow.com/questions/11315043/opencv-imread-hanging-when-called-from-a-web-request) – ilke444 Jan 31 '17 at 00:40

1 Answers1

2

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

WSGIApplicationGroup %{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:

Community
  • 1
  • 1
Hugo Flick
  • 109
  • 1
  • 3