Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active January 14, 2019 07:26
Show Gist options
  • Save keithweaver/8208226cfca7d1fd3f122f6e106b2f3e to your computer and use it in GitHub Desktop.
Save keithweaver/8208226cfca7d1fd3f122f6e106b2f3e to your computer and use it in GitHub Desktop.
Object detection using custom Haar Cascade on an image with OpenCV
# Running:
# $ python run-custom-cascade.py
# Import OpenCV
import cv2
# Image file
IMAGE_FILE = './example.png' # Change this to be your image
# Cascade file
CASCADE_FILE = './cascade-file-goes-here.xml'
# Cascade item name
CASCADE_ITEM = 'Front Face'
# Load image file
image = cv2.imread(IMAGE_FILE)
# Convert the image to gray
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Load your cascade file
cascade = cv2.CascadeClassifier(CASCADE_FILE)
# Detect cascade items and put rectangles around them
rectangles = cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=10,
minSize=(75, 75))
for (i, (x, y, w, h)) in enumerate(rectangles):
# Surround cascade with rectangle
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(image, CASCADE_ITEM + " #{}".format(i + 1), (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
# Display the cascade to the user
cv2.imshow(CASCADE_ITEM + "s", image)
cv2.waitKey(0)
# For more:
# http://docs.opencv.org/2.4/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html
# http://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Object_Detection_Face_Detection_Haar_Cascade_Classifiers.php
# https://stackoverflow.com/questions/30857908/face-detection-using-cascade-classifier-in-opencv-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment