- Import the modules:
import cv2import matplotlib.pyplot as plt
- Load the test image:
image = cv2.imread('../data/people.jpg')
- Create the HOG feature computer and detector:
hog = cv2.HOGDescriptor()hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
- Detect the people in the image:
locations, weights = hog.detectMultiScale(image)
- 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)
- 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]]) ...