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.
Here were the results I got:
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?
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.
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.
detectMultiScale(gray, 1.3, 5) MultiScale detects objects of different sizes in the input image and returns rectangles positioned on the faces.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With