Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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 cv2.cv 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

@app.route('/')
def detect_faces():
    img = Image.open('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__":
    app.run()

gdb output:

    (gdb) run myflaskapp.py
Starting program: /usr/bin/python myflaskapp.py
[Thread debugging using libthread_db enabled]  
 * Running on http://127.0.0.1:5000/           

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\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
#1  0x00007fffee162095 in cv::CvtColorLoop<cv::RGB2Gray<unsigned char> > (srcmat=..., dstmat=..., cvt=...) at /home/mgj/Downloads/opencv/modules/imgproc/src/color.cpp:174
#2  0x00007fffee158d9e in cv::cvtColor (_src=..., _dst=..., code=6, dcn=1) at /home/mgj/Downloads/opencv/modules/imgproc/src/color.cpp:3267
#3  0x00007fffee160951 in cvCvtColor (srcarr=0x7fffffff9c00, dstarr=0x110a550, code=6) at /home/mgj/Downloads/opencv/modules/imgproc/src/color.cpp:3737
#4  0x00007fffeccd85ff in cvHaarDetectObjectsForROC (_img=0xe6e200, cascade=0x1667490, storage=0x117df90, rejectLevels=std::vector of length 0, capacity 0, levelWeights=std::vector of length 0, capacity 0,
    scaleFactor=1.1000000000000001, minNeighbors=3, flags=0, minSize=..., maxSize=..., outputRejectLevels=false) at /home/mgj/Downloads/opencv/modules/objdetect/src/haar.cpp:1074
#5  0x00007fffeccda444 in cvHaarDetectObjects (_img=0xe6e200, cascade=0x1667490, storage=0x117df90, scaleFactor=1.1000000000000001, minNeighbors=3, flags=0, minSize=..., maxSize=...)
    at /home/mgj/Downloads/opencv/modules/objdetect/src/haar.cpp:1333
#6  0x00007fffeee1a879 in pycvHaarDetectObjects (self=0x0, args=0x114e050, kw=0x0) at /home/mgj/Downloads/opencv/debug/modules/python/generated0.i:3830
#7  0x000000332f2deb24 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#8  0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#9  0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#10 0x000000332f26eead in ?? () from /usr/lib64/libpython2.6.so.1.0
#11 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#12 0x000000332f2dd5b0 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#13 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#14 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#15 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#16 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#17 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#18 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#19 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#20 0x000000332f26edb0 in ?? () from /usr/lib64/libpython2.6.so.1.0
#21 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#22 0x000000332f25970f in ?? () from /usr/lib64/libpython2.6.so.1.0
#23 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#24 0x000000332f29ce44 in ?? () from /usr/lib64/libpython2.6.so.1.0
#25 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#26 0x000000332f2de0b4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#27 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#28 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#29 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#30 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#31 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#32 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#33 0x000000332f26edb0 in ?? () from /usr/lib64/libpython2.6.so.1.0
#34 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#35 0x000000332f25970f in ?? () from /usr/lib64/libpython2.6.so.1.0
#36 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#37 0x000000332f2de0b4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#38 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#39 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#40 0x000000332f26edb0 in ?? () from /usr/lib64/libpython2.6.so.1.0
#41 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#42 0x000000332f25970f in ?? () from /usr/lib64/libpython2.6.so.1.0
#43 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#44 0x000000332f29ca7e in ?? () from /usr/lib64/libpython2.6.so.1.0
#45 0x000000332f29b528 in ?? () from /usr/lib64/libpython2.6.so.1.0
#46 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#47 0x000000332f2de0b4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#48 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#49 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#50 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#51 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#52 0x000000332f26edb0 in ?? () from /usr/lib64/libpython2.6.so.1.0
#53 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#54 0x000000332f25970f in ?? () from /usr/lib64/libpython2.6.so.1.0
#55 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#56 0x000000332f2de0b4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#57 0x000000332f2dfccf in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#58 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#59 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#60 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#61 0x000000332f26eead in ?? () from /usr/lib64/libpython2.6.so.1.0
#62 0x000000332f244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
#63 0x000000332f2dd5b0 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#64 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#65 0x000000332f2debe4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
#66 0x000000332f2e0797 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
#67 0x000000332f2e0872 in PyEval_EvalCode () from /usr/lib64/libpython2.6.so.1.0
#68 0x000000332f2fbbbc in ?? () from /usr/lib64/libpython2.6.so.1.0
#69 0x000000332f2fbc90 in PyRun_FileExFlags () from /usr/lib64/libpython2.6.so.1.0
#70 0x000000332f2fd17c in PyRun_SimpleFileExFlags () from /usr/lib64/libpython2.6.so.1.0
#71 0x000000332f309c32 in Py_Main () from /usr/lib64/libpython2.6.so.1.0
#72 0x000000331c01ecdd in __libc_start_main () from /lib64/libc.so.6
#73 0x0000000000400649 in _start ()

I'm running this on CentOS 6.3 x64 , and i'm using OpenCV 2.4.2.

Attachment: Input image file input.jpg