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