Skip to content

landmark_demo.py only works if input image contains a face #3548

@kkawabat

Description

@kkawabat
Detailed description

I was using the landmark_demo.py as a reference for a personal project of mine and I kept running into the below error:

cv2.error: OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix.cpp:1193: error: (-15:Bad number of channels) The total width is not divisible by the new number of channels in function 'cv::Mat::reshape'

It turned out that when CascadeClassifier.detectMultiScale does not detect any faces in the input image it returns an empty tuple which Facemark.fit is unable to handle. the demo does not have any indication that this would be the behavior and the exception that gets raised is pretty obtuse making it difficult to diagnose this problem.

This isn't really a bug I don't think but it is pretty confusing. Some possible suggestions:

  • the demo should indicate this behavior for clarity if this is the intended behavior
  • change the return of CascadeClassifier.detectMultiScale when no faces are detected so that it's compatible with Facemark.fit() function
  • raise a more description exception in Facemark.fit() when faces are empty so that it's easier to diagnose the problem.
Steps to reproduce

Replace lena.jpg image in the landmark_demo.py with an image without any faces. And run the script.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions