%matplotlib inline import numpy as np import cv2 import imutils from matplotlib import pyplot as plt from imutils.object_detection import non_max_suppression hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # Change to your own image with pedestrians image = cv2.imread('../data/diridon/P1070543.jpg') image = imutils.resize(image, width=min(1080, image.shape[1])) original = image.copy() # Detect people in the image (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) # Draw the original bounding boxes for (x, y, w, h) in rects: cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) # Apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.6) # Draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) print("Number of pedestrians: {}".format(len(pick))) # Show the output images plt.imshow(image) plt.show()