Object Detection with OpenCV
Detect faces, objects, and features using classical and deep learning approaches built into OpenCV.
Haar Cascade Classifiers
Haar cascades are pre-trained XML classifiers that detect objects by scanning an image at multiple scales. OpenCV ships with cascades for faces, eyes, smiles, and more.
import cv2
# Load pre-trained cascade
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
eye_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_eye.xml"
)
img = cv2.imread("group_photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Detect eyes within each face region
roi_gray = gray[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.circle(img, (x+ex+ew//2, y+ey+eh//2), ew//2, (0, 255, 0), 2)
print(f"Found {len(faces)} faces")
Template Matching
img = cv2.imread("screenshot.png")
template = cv2.imread("icon.png")
h, w = template.shape[:2]
# Match template
result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
locations = np.where(result >= threshold)
# Draw rectangles around matches
for pt in zip(*locations[::-1]):
cv2.rectangle(img, pt, (pt[0]+w, pt[1]+h), (0, 255, 0), 2)
Contour Detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 500: # Filter small contours
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Shape approximation
epsilon = 0.02 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
print(f"Vertices: {len(approx)}, Area: {area}")
DNN Module for Deep Learning
OpenCV's DNN module can load and run pre-trained models from TensorFlow, PyTorch, ONNX, Caffe, and Darknet (YOLO).
# Load YOLO model
net = cv2.dnn.readNetFromDarknet("yolov4.cfg", "yolov4.weights")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
# Prepare input
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
# Get output layer names
ln = net.getLayerNames()
ln = [ln[i - 1] for i in net.getUnconnectedOutLayers()]
# Forward pass
outputs = net.forward(ln)
# Process detections
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Scale bounding box to image size
box = detection[0:4] * np.array([w, h, w, h])
(cx, cy, bw, bh) = box.astype("int")
x = int(cx - bw / 2)
y = int(cy - bh / 2)
cv2.rectangle(img, (x, y), (x+int(bw), y+int(bh)),
(0, 255, 0), 2)
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX