Ask Your Question

RGB2Gray::operator() causing segmentation fault when using Python

asked 2012-09-02 09:06:13 -0500

mgj gravatar image

Im using the OpenCV python wrappers to expose the functionality of OpenCV as a webservice.

When the webservice receives a request, it does object detection and return a list of rectangles. This works perfectly for some, small, images. But for the picture i've attached, its causing a segmentation fault.

Python code:

import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename

app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True

import as cv

def detectFaces(image):
    """Detects faces in image and makes a rectangle for each"""
    rects = []
    cascades = [os.environ.get('MY_DATA_DIR') + '/opencv/haarcascades/' + 'haarcascade_frontalface_alt2.xml',
        os.environ.get('MY_DATA_DIR') + '/opencv/haarcascades/' + "haarcascade_frontalface_alt.xml",
        os.environ.get('MY_DATA_DIR') + '/opencv/haarcascades/' + "haarcascade_frontalface_default.xml",
        os.environ.get('MY_DATA_DIR') + '/opencv/haarcascades/' + "haarcascade_profileface.xml"]

    for i in cascades:
        hc = cv.Load(i)
        faces = cv.HaarDetectObjects(image, hc, cv.CreateMemStorage())
        for (x,y,w,h),n in faces:
            rects.append( ((x,y),(x+w,y+h)) )

    return rects

import Image
import StringIO
import json

def detect_faces():
    img ='input.jpg')
    cv_im = cv.CreateImageHeader(img.size, cv.IPL_DEPTH_8U, 3)
    cv.SetData(cv_im, img.tostring())
    rects = detectFaces(cv_im)
    return json.dumps({'rects': rects}, indent=4)

if __name__ == "__main__":

gdb output:

    (gdb) run
Starting program: /usr/bin/python
[Thread debugging using libthread_db enabled]  
 * Running on           

Program received signal SIGSEGV, Segmentation fault.
0x00007fffee1485d6 in cv::RGB2Gray<unsigned char>::operator() (this=0x7fffffff6dd0, src=0x7fffe8e37000 "", dst=
    0x1676410 "\004\004\005", '\004' <repeats 13 times>, "\005\005\005\005\005\004\004\004\005\005\005\005\006\005\005\005\005\005\005\004\005\005\005\005\005\005\005\006\006\005\005\005\a\a\a\006\006\005\005\005\006\006\006\006\006\005\005\005\006\a\a\006\005\005\006\006\006\005\006\006\a\006\006\006\006\006\006\a\a\a\a\a\a\b\t\b", '\a' <repeats 11 times>, "\006\a\a\a\b\a\006\006\006\a\a\a\a\a\a\a\a\b\b\b\a\a\a\a\a\a\b\n\t\t\b\b\b\a\a\a\b\b\b\b\b\t\t\t\n\n\t\t\n\t\t\t\b", '\t' <repeats 11 times>, "\n\n\n\n\n\n\t\t\t\v\v\t\n\n\n\n\n\n\b\n\n\n\v\n\n\v\v\f\v\v\n\n\n"..., n=5038848)                                   
    at /home/mgj/Downloads/opencv/modules/imgproc/src/color.cpp:435                                                                                                                                            
435                 dst[i] = (uchar)((_tab[src[0]] + _tab[src[1]+256] + _tab[src[2]+512]) >> yuv_shift);                                                                                                       
(gdb) backtrace                                                                                                                                                                                                
#0  0x00007fffee1485d6 in cv::RGB2Gray<unsigned char>::operator() (this=0x7fffffff6dd0, src=0x7fffe8e37000 "", dst=
    0x1676410 "\004\004\005", '\004' <repeats 13 times>, "\005\005\005\005\005\004\004\004\005\005\005\005\006\005\005\005\005\005\005\004\005\005\005\005\005\005 ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-09-03 01:20:49 -0500

Daniil Osokin gravatar image

I'm think, you should use another image format with Haar (now you have PIL). Here is an example of usage: Haar Feature-based Cascade Classifier for Object Detection. Conversion between formats here: Cookbook.

edit flag offensive delete link more


Im already converting from PIL as per the instructions in the Cookbook. I cannot use cv.Load() to load the image directly for reasons unrelated to this issue.

mgj gravatar imagemgj ( 2012-09-03 11:21:18 -0500 )edit

I'm think, the problem is with image formats. Just try to use Load (if it's possible), is all works fine? Or you can save images, when they come to Haar: are they the same with read images?

Daniil Osokin gravatar imageDaniil Osokin ( 2012-09-03 23:42:51 -0500 )edit

You are correct. I CAN actually use the code if i use cvLoadImage() instead. I would however very much like to avoid doing so, as it requires me to having the image stored on the filesystem. Is this a bug in OpenCV? As far as i can tell, im following the Cookbook example correctly.

mgj gravatar imagemgj ( 2012-09-06 11:01:01 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-09-02 09:06:13 -0500

Seen: 1,028 times

Last updated: Sep 03 '12