OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Face detection is one of the most popular applications of computer vision. In this tutorial, we'll leverage the power of OpenCV and Python to perform real-time face detection using a webcam.
Setup:
First, install OpenCV:
pip install opencv-python
Code for Face Detection with Webcam:
import cv2 # Load the pre-trained cascade classifier face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Start video capture from the default camera (webcam) cap = cv2.VideoCapture(0) while True: # Read a frame from the webcam ret, frame = cap.read() # If the frame was not retrieved, break out of the loop if not ret: break # Convert the frame to grayscale for face detection gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces in the image faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # Draw rectangles around the detected faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # Display the frame with the detected faces cv2.imshow('Face Detection', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object and close all windows cap.release() cv2.destroyAllWindows()
Run the Code:
After running the code, you should see a window displaying the video feed from your webcam. Any detected faces will be highlighted with green rectangles. Press 'q' to quit the application.
The face detection relies on Haar cascades, which are pre-trained classifiers that OpenCV provides for detecting objects of interest (in our case, faces).
The parameters for detectMultiScale
can be adjusted based on the specific conditions of the video feed:
scaleFactor
: This compensates for some faces appearing larger or closer to the camera than others.minNeighbors
: This parameter controls the number of rectangles (neighbors) a candidate region should have to retain it. A higher number will reduce false positives but might also miss some faces.minSize
: The minimum possible object size. Objects smaller than this are ignored.Keep in mind that Haar cascades may occasionally produce false positives or miss some faces. There are more advanced methods for face detection, like DNN-based approaches (e.g., Single Shot MultiBox Detector (SSD) or Faster R-CNN), which might offer better accuracy but can be computationally more intensive.
This tutorial provides a basic method for real-time face detection using OpenCV and a webcam. It's a great starting point and can be further enhanced based on specific needs.
Python OpenCV face detection from webcam stream:
import cv2 # Load the pre-trained face detector face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) while True: # Read a frame from the webcam ret, frame = cap.read() # Convert to grayscale for face detection gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces in the frame faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # Draw rectangles around the faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # Display the frame with face detection cv2.imshow('Face Detection', frame) # Break the loop if 'q' key is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the webcam and close the window cap.release() cv2.destroyAllWindows()