Pedestrian Detection using OpenCV - Python

Last Updated : 15 Jun, 2026

Pedestrian Detection is a computer vision technique used to identify and locate people in images or video streams. It is widely used in applications such as autonomous vehicles, surveillance systems, and traffic monitoring.

OpenCV provides a pre-trained HOG (Histogram of Oriented Gradients) and SVM (Support Vector Machine) model that enables pedestrian detection without requiring custom model training.

Histogram of Oriented Gradients

Histogram of Oriented Gradients (HOG) is a feature extraction technique commonly used for object detection. It analyzes gradient patterns in an image to capture the shape and structural characteristics of objects, making it particularly effective for pedestrian detection.

  • Extracts edge and gradient information to represent object shapes.
  • Works with an SVM classifier to identify pedestrians in images and videos.

Working

  1. The image is divided into small cells.
  2. Gradient directions are calculated for each pixel.
  3. A histogram of gradient orientations is created for every cell.
  4. Histograms are normalized to reduce the effects of illumination changes.
  5. The extracted features are passed to an SVM classifier for pedestrian detection.

Implementation

OpenCV provides a pre-trained HOG and SVM detector that can be used to detect pedestrians in both images and videos. The following examples demonstrate pedestrian detection in an image and a video stream.

Example 1: Detecting Pedestrians in an Image

In this example, the HOG descriptor and OpenCV's pre-trained SVM detector are used to identify pedestrians in an image and draw bounding boxes around them.

Image Used: python-opncv

Python
import cv2
import imutils
 
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
 
image = cv2.imread('img.png')
 
image = imutils.resize(image,
                       width=min(400, image.shape[1]))
 
(regions, _) = hog.detectMultiScale(image, 
                                    winStride=(4, 4),
                                    padding=(4, 4),
                                    scale=1.05)
 
for (x, y, w, h) in regions:
    cv2.rectangle(image, (x, y), 
                  (x + w, y + h), 
                  (0, 0, 255), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)
 
cv2.destroyAllWindows()

Output: python-opnecv-1

Example 2: Detecting Pedestrians in a Video

In this example, each frame of a video is processed using the HOG + SVM detector. Detected pedestrians are highlighted with bounding boxes in real time.

Python
import cv2
import imutils
 
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
 
cap = cv2.VideoCapture('vid.mp4')
 
while cap.isOpened():
    ret, image = cap.read()
    if ret:
        image = imutils.resize(image, 
                               width=min(400, image.shape[1]))
 
        (regions, _) = hog.detectMultiScale(image,
                                            winStride=(4, 4),
                                            padding=(4, 4),
                                            scale=1.05)
 
        for (x, y, w, h) in regions:
            cv2.rectangle(image, (x, y),
                          (x + w, y + h), 
                          (0, 0, 255), 2)
 
        cv2.imshow("Image", image)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()

Output:

Comment