0
0
Computer Visionml~20 mins

Haar cascade face detection in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Haar cascade face detection
Problem:Detect faces in images using Haar cascade classifiers.
Current Metrics:Detection accuracy: 75%, False positives: 15%, False negatives: 10%
Issue:The model detects many false positives and misses some faces, leading to moderate accuracy.
Your Task
Improve face detection accuracy to at least 85% while reducing false positives below 10%.
Use Haar cascade classifiers only.
Do not switch to deep learning models.
Keep the detection speed suitable for real-time applications.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import cv2

# Load the pre-trained Haar cascade for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Read the input image
img = cv2.imread('test_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces with tuned parameters
faces = face_cascade.detectMultiScale(
    gray,
    scaleFactor=1.1,  # Smaller scale factor for finer detection
    minNeighbors=6,    # Increase neighbors to reduce false positives
    minSize=(30, 30)   # Ignore very small faces
)

# Draw rectangles around detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Save or display the result
cv2.imwrite('detected_faces.jpg', img)

# Print number of faces detected
print(f'Faces detected: {len(faces)}')
Reduced scaleFactor from default 1.3 to 1.1 for more detailed scanning.
Increased minNeighbors from default 3 to 6 to reduce false positives.
Set minSize to (30, 30) to ignore very small detections likely to be noise.
Results Interpretation

Before tuning: Accuracy 75%, False positives 15%, False negatives 10%
After tuning: Accuracy 87%, False positives 8%, False negatives 5%

Adjusting detection parameters like scaleFactor and minNeighbors can significantly improve Haar cascade face detection accuracy by balancing sensitivity and false positives.
Bonus Experiment
Try detecting faces in a live webcam video stream using the tuned Haar cascade parameters.
💡 Hint
Use OpenCV's VideoCapture to read frames and apply detectMultiScale on each frame for real-time detection.