Pedestrian Detection (Default)

2016/03/28 ysmrnbt

In [2]:
import cv2 # use opencv

The HOG detector returns slightly larger rectangles than the real objects. So we slightly shrink the rectangles to get a nicer output.

In [3]:
def draw_detections(img, rects, thickness = 1):
    for x, y, w, h in rects:
        pad_w, pad_h = int(0.15*w), int(0.05*h)
        cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)

Default detector: INRIA Person Dataset is used for training

HOG(Histogram of Oriented Gradients) and SVM(Support Vector Machine) are used to detect pedestrian in 1 picture.

  • hitThreshold: Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specfied in the detector coefficients (as the last free coefficient). But if the free coefficient is omitted (which is allowed), you can specify it manually here.
  • finalThreshold: Coefficient to regulate the similarity threshold. When detected, some objects can be covered by many rectangles. 0 means not to perform grouping.
In [9]:
def PD_default(filename):
    image = cv2.imread(filename) # read image
    hog = cv2.HOGDescriptor() # derive HOG features
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # SVM
    
    # pedestrian detection
    found, w = hog.detectMultiScale(image, hitThreshold=0, winStride=(8,8), padding=(0,0), scale=1.05, finalThreshold=2)
    draw_detections(image, found) # draw rectangles
    
    # write & save image
    cv2.imshow('original',image) # write image
    cv2.waitKey() # for keyboard binding
    cv2.imwrite('./img/inria.jpg',image) # save image
    #cv2.destroyAllWindows() # clean up
In [10]:
PD_default("./img/kids.jpg")

Daimler detector: Daimler Pedestrian Detection Benchmark Dataset is used for training

In [11]:
def PD_daimler(filename):
    image = cv2.imread(filename) # read image
    hog = cv2.HOGDescriptor((48,96), (16,16), (8,8), (8,8), 9) # derive HOG features
    hog.setSVMDetector(cv2.HOGDescriptor_getDaimlerPeopleDetector()) # SVM
    
    # pedestrian detection
    found, w = hog.detectMultiScale(image, hitThreshold=0, winStride=(8,8), padding=(32,32), scale=1.05, finalThreshold=2)
    draw_detections(image, found) # draw rectangles
    
    # write & save image
    cv2.imshow('original',image) # write image
    cv2.waitKey() # for keyboard binding
    cv2.imwrite('./img/daimler.jpg',image) # save image
    #cv2.destroyAllWindows() # clean up
In [12]:
PD_daimler("./img/kids.jpg")
In [ ]: