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

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 – https://youtu.be/5_muyu-Lv2Y

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()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

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.rectangle(copy,(x,y),(x+w,y+h),(0,0,255),2)

cv2.imshow('before suppression',copy)
cv2.waitKey(0)

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.rectangle(img,(xa,ya),(xb,yb),(0,255,0),2)

cv2.imshow('after suppression',img)
cv2.waitKey(0)

cv2.destroyAllWindows()
  • 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 ?…

Read my previous post: WEIGHT CATEGORY PREDICTION USING RANDOM FOREST 

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

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

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