Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructing Cascade from XML causes segfault #5492

Open
stefanv opened this issue Jul 28, 2021 · 4 comments
Open

Constructing Cascade from XML causes segfault #5492

stefanv opened this issue Jul 28, 2021 · 4 comments
Labels

Comments

@stefanv
Copy link
Member

stefanv commented Jul 28, 2021

Running this gallery example on this XML file causes a segfault.

from skimage import data
from skimage.feature import Cascade

import matplotlib.pyplot as plt
from matplotlib import patches

# Initialize the detector cascade.
detector = Cascade('haarcascade_frontalface_default.xml')

img = data.astronaut()

detected = detector.detect_multi_scale(img=img,
                                       scale_factor=1.2,
                                       step_ratio=1,
                                       min_size=(60, 60),
                                       max_size=(123, 123))

plt.imshow(img)
img_desc = plt.gca()
plt.set_cmap('gray')

for patch in detected:

    img_desc.add_patch(
        patches.Rectangle(
            (patch['c'], patch['r']),
            patch['width'],
            patch['height'],
            fill=False,
            color='r',
            linewidth=2
        )
    )

plt.show()

gdb reports the problem at

__Pyx_GetItemInt_Fast (o=0x7fffaef01880, i=1, wraparound=<optimized out>, boundscheck=0, is_list=0) at feature/_cascade.cpp:43232
43232	 *     out[0] = <np_real_numeric_out>quadratic_interpolation(xr, fr)

Here's the backtrace. I'd start investigating at _load_xml (L762) in _cascade.pyx:

#0  __Pyx_GetItemInt_Fast (o=0x7fffaef01880, i=1, wraparound=<optimized out>, boundscheck=0, is_list=0) at feature/_cascade.cpp:43232
#1  0x00007fffb944e629 in __pyx_pf_7skimage_7feature_8_cascade_7Cascade_10_load_xml (__pyx_v_self=0x7fffaefdef80, __pyx_v_xml_file=<optimized out>, 
    __pyx_v_eps=0x7fffb994b630) at feature/_cascade.cpp:7116
#2  0x00007ffff7d3052e in ?? () from /usr/lib/libpython3.9.so.1.0
#3  0x00007fffb943a0cd in __Pyx_PyObject_Call (kw=0x0, arg=0x7ffff735ac00, func=0x7ffff7353db0) at feature/_cascade.cpp:108555
#4  __pyx_pf_7skimage_7feature_8_cascade_7Cascade_2__init__ (__pyx_v_eps=<optimized out>, __pyx_v_xml_file=<optimized out>, __pyx_v_self=<optimized out>)
    at feature/_cascade.cpp:4155
#5  __pyx_pw_7skimage_7feature_8_cascade_7Cascade_3__init__ (__pyx_v_self=<optimized out>, __pyx_args=<optimized out>, __pyx_kwds=<optimized out>)
    at feature/_cascade.cpp:4086
@imagesc-bot
Copy link

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/failure-to-load-cascade-from-skimage-feature/55798/6

@grlee77
Copy link
Contributor

grlee77 commented Jul 30, 2021

I figured this was likely due to out-of-bounds access somewhere, so I recompiled _cascade.pyx, but changing to boundscheck=True up top. This results in the following more helpful error:

Traceback (most recent call last):
  File "doc/examples/applications/plot_face_detection.py", line 76, in <module>
    detector = Cascade('/home/lee8rx/Downloads/haarcascade_frontalface_default.xml')
  File "skimage/feature/_cascade.pyx", line 471, in skimage.feature._cascade.Cascade.__init__
    self._load_xml(xml_file, eps)
  File "skimage/feature/_cascade.pyx", line 848, in skimage.feature._cascade.Cascade._load_xml
    new_feature = MBLBP(params[1], params[0], params[2], params[3])
IndexError: list index out of range

@grlee77
Copy link
Contributor

grlee77 commented Jul 30, 2021

I have no prior experience with this Cascade class in skimage or this feature in OpenCV, but I suspect the root difference is related to the difference in the classifier type used. The .xml file bundled in our data package uses

<featureType>LBP</featureType>

while the XML file in this issue uses

<featureType>HAAR</featureType>

LBP = Local Binary Pattern while HAAR = Haar-like wavelet features (reference)

Given that the struct where the error in my prior message occurs is named MBLBP and its members are integers it would seem our Cascade class is only compatible with the LBP feature type. We should definitely try to extract this field from the XML and raise a useful error if HAAR features are encountered instead!

@imagesc-bot
Copy link

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/failure-to-load-cascade-from-skimage-feature/55798/7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants