Real-time face detection is a practical application of computer vision where faces are detected live using a webcam.
With OpenCV’s VideoCapture() and Haar Cascade Classifiers, you can build a simple but effective face detection system.
Use cv2.VideoCapture(0) to access the default webcam (0 = first camera, 1 = external camera).
import cv2
# Load Haar cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# Start webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30,30))
# Draw rectangles around faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Real-Time Face Detection", frame)
# Exit on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
✅ You now know how to build a real-time face detection system using OpenCV.
This is the foundation for attendance systems, security applications, and live video analytics.
👉 Next, we will learn object detection using color spaces.
