Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@clarle
Created June 23, 2017 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarle/e31955984fbb7c1a6392c2c0e9d0effb to your computer and use it in GitHub Desktop.
Save clarle/e31955984fbb7c1a6392c2c0e9d0effb to your computer and use it in GitHub Desktop.
Pedestrian Detection
%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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment