How to do it...

  1. Import the modules:
import cv2import matplotlib.pyplot as plt
  1. Load the test image:
image = cv2.imread('../data/people.jpg')
  1. Create the HOG feature computer and detector:
hog = cv2.HOGDescriptor()hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
  1. Detect the people in the image:
locations, weights = hog.detectMultiScale(image)
  1. Draw the detected people bounding boxes:
dbg_image = image.copy()for loc in locations:    cv2.rectangle(dbg_image, (loc[0], loc[1]),                   (loc[0]+loc[2], loc[1]+loc[3]), (0, 255, 0), 2)
  1. Visualize the results:
plt.figure(figsize=(12,6))plt.subplot(121)plt.title('original')plt.axis('off')plt.imshow(image[:,:,[2,1,0]])plt.subplot(122)plt.title('detections')plt.axis('off')plt.imshow(dbg_image[:,:,[2,1,0]]) ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.