face detection is a quintessential application of Artificial Intelligence (AI), specifically within computer vision and machine learning (ML). It uses AI algorithms—often deep learning neural networks—to scan, locate, and analyze human facial features in images or video streams, distinguishing them from backgrounds.
Applications: It is used widely in security, surveillance, social media tagging, smartphone unlocking, and marketing analytics.
import cv2
from google.colab.patches import cv2_imshow
import numpy as np
from PIL import Image
import matplotlib
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
img = cv2.imread("friends.png")
#img = cv2.imread("lena.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
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)
cv2_imshow(img) ##instead of cv2.imshow()
OpenCV provides pretrained face detection models
Haar Cascade is a classical AI face detector
This line: Model file: haarcascade_frontalface_default.xml
Loads pre-trained face detection model which is stored inside OpenCV library folder.
What is Haar Cascade?
It is a ML based object detection method trained on thousands of face images
Detects features like:
Eyes
Nose
Edge patterns