To Detect human from an image using Python

To detect humans in an image and draw bounding boxes around them, you can use the following steps

Import the required library. in all the following examples, the required python library is OpenCV.

Read the input image using cv2.imread() in a grayscale. specify the full image path.

Initialize a HOG descriptor object hog = cv2.HOGDescriptor() and set the SVM detector as hog.setSVMDetector() as  default people detector.

Detect humans in the input image using hog.detectMultiScale().  it returns the coordinates of detected humans in (x,y,w,h)format.

Loop over all detected  humans in the image and draw the bounding rectangles around the detected humans in the original image using cv2.rectangle().

Display the image with the drawn bounding rectangles around the humans.

import cv2
image = cv2.imread(‘people1.jpg’)
hog = cv2.HOGDescriptor()

import cv2
image = cv2.imread('people1.jpg')
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
(humans, _) = hog . detectMultiScale(image, winstride=(10, 10),
padding=(32, 32), scale=1.1)
print('Human Detected : ',len(humans))
for(x,y,w,h) in humans:
pad_w,pad_h = int(0.15 * w), int(0.01 * h)
cv2.rectangle(image,(x + pad_w,y + pad_h),(x + w - pad_w, y + h -pad_h),(0, 255,0),2)
cv2.imshow("Image",image)
cv2.waitKey(0)
cv2.destroyAllWindows()



output
Human Detected:4

 


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top