Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV 3 Python - Haar Cascade Upper Body Detector doesn't work on a half body (waist up) photo?

I've been trying to detect people in photos and I've had some success with pedestrians. However, for my use case scenario, I need to be able to detect half body/upper body (waist up) or heads in photo.

I tried the haar cascade for the upper body. Here is the code I used:

import numpy as np
import cv2

img = cv2.imread('/path/to/img.jpg',0)

upperBody_cascade = cv2.CascadeClassifier('path/to/haarcascade_upperbody.xml')    

arrUpperBody = upperBody_cascade.detectMultiScale(img)
if arrUpperBody != ():
        for (x,y,w,h) in arrUpperBody:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        print 'body found'

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

And I had 3 test images I used. Kindly note that it's the same image, cropped at certain levels.

  1. Knee Up
  2. Waist Up
  3. Chest Up

Here were the results I got:

  1. Knee Up

Knee Up

  1. Waist Up

Waist Up

  1. Chest Up

Chest Up

As you can see, the Knee Up and Chest Up photos were able to detect the upper body and head area respectively.

However, the waist up photo didn't return any results, even if the upper body and the head are visible.

Does anyone know how or why this happens and what can be done to make the upper body detection much more consistent?

like image 271
Razgriz Avatar asked Apr 01 '17 15:04

Razgriz


People also ask

How do you use Haar cascade in OpenCV?

Loading Haar Cascade in OpenCV We can load any haar-cascade XML file using cv2. CascadeClassifier function. Once cascade is loaded in OpenCV, we can call the detector function. results It lists coordinates (x, y, w,h) of bounding boxes around the detected object.

How does OpenCV CascadeClassifier identify objects?

Haar-cascade Detection in OpenCV First, a cv::CascadeClassifier is created and the necessary XML file is loaded using the cv::CascadeClassifier::load method. Afterwards, the detection is done using the cv::CascadeClassifier::detectMultiScale method, which returns boundary rectangles for the detected faces or eyes.

What is Face_detector detectMultiScale?

detectMultiScale(gray, 1.3, 5) MultiScale detects objects of different sizes in the input image and returns rectangles positioned on the faces.

What is detectMultiScale in OpenCV?

detectMultiScale (InputArray image, std::vector< Rect > &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size()) Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.


1 Answers

If you want consistency don't use haar cascade. In general these models are meant to be fast and light-weight, but have bad detection accuracy specially under occlusion situations. My intuition for why this doesn't work is that the model might rely on features from the legs attached to the upper body in order to detect and localize the upper body. Since there is occlusion to the legs, it does not find the upper body.

For the sake of consistency, I'd recommend using something like OpenPose which is more robust to occlusion. You can then use the body keypoints to draw your bounding boxes around regions of interest.

like image 51
pip1726 Avatar answered Oct 13 '22 14:10

pip1726