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.
- 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.
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.
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 ?…