Pedestrian Detection using HOGs in Python – simplest way – easy project – 2023

Machine Learning Projects

In today’s blog, we will perform pedestrian detection using HOG short for Histogram for Gradients. HOGs are great feature detectors and can also be used for object detection with SVM but due to many other State of the Art object detection algorithms like YOLO, and SSD, present out there, we don’t use HOGs much for object detection.

This is going to be a very short and crisp blog so without any further due, Let’s do it…

Checkout the video here –

Code for pedestrian detection using HOGs…

import cv2
from imutils.object_detection import non_max_suppression
from imutils import resize
import numpy as np

hog = cv2.HOGDescriptor()

img = cv2.imread('f.jpg')
img = resize(img,height=500)

rects,weights = hog.detectMultiScale(img,winStride=(4,4),padding=(8,8),scale=1.05)

copy = img.copy()
for x,y,w,h in rects:

cv2.imshow('before suppression',copy)

r = np.array([[x,y,x+w,y+h] for x,y,w,h in rects])
pick = non_max_suppression(r,probs=None,overlapThresh=0.65)    

for xa,ya,xb,yb in pick:

cv2.imshow('after suppression',img)

  • Line 1-4 – Importing required libraries.
  • Line 6 – Making a HogDescriptor object.
  • Line 7 – Importing the default people detector classifier from the HOG library. This is the main step where we are importing a pre trained model for Pedestrian Detection using HOG.
  • Line 9-10 – Read and resize the image.
  • Line 12 – Let’s detect people.
  • Line 14 – Keeping a copy of an original image for future use.
  • Line 15-16 – Drawing rectangles around persons(if found any).
  • Line 18-19 – Showing results.
Pedestrian Detection using HOG
before suppression
  • Line 21-22 – Performing non-maximum suppression.
  • Line 24-25 – Now again drawing these new rectangles with non-maximum rectangles removed.
  • Line 27-28 – Show the results.
Pedestrian Detection using HOG
after suppression

NOTE – You can see that majority of the boxes in the before suppression image (in the right bottom portion) are suppressed in the after suppression image.

  • Line 30 – Destroy all open windows.

NOTE – You can see that as we performed Pedestrian Detection using HOG, we are not getting unbelievable results. It is picking some noise also. That’s why we use YOLO or SSDs for Object Detection nowadays where accuracy is our main requirement.

Download the Source Code…

Do let me know if there’s any query regarding this topic by contacting me by email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…


Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at

Leave a Comment

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