Face And Eye Detection Example with OpenCV

    One of the essential tasks in computer vision is detecting faces and eyes in images or real-time video. This technology has a wide range of applications, from photography to surveillance systems. In this blog post, we'll explore how to perform face and eye detection using OpenCV library. The tutorial covers:

  1. Face detection function
  2. Eye detection function
  3. Face and eye detection example 
  4. Conclusion

     Let's get started.

      Face and eye detection is the process of locating and marking regions of an image that correspond to faces and eyes. It's a crucial component of many applications, such as facial recognition, emotion analysis, and others. Detecting faces and eyes is challenging due to variations in lighting, poses, and facial expressions.
    OpenCV is widely used for various computer vision tasks, including face and eye detection. OpenCV provides pre-trained models and functions to make the implementation of these tasks relatively straightforward.

 

Face detection function
 
   OpenCV provides a pre-trained Haar Cascade Classifier for face detection. We can load it from 'cv2.data'.
 
 
# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

     
    The face_cascade.detectMultiScale() function is a part of OpenCV's Haar Cascade Classifier-based face detection. This function detects objects (in this case, faces) at different scales within the image. It works by resizing the image multiple times and running the classifier on each scale to find potential face regions. The following code shows the capturing the frame from the video and detect the faces in captured frame.
 
 
# Initialize the webcam or load a video file
cap = cv2.VideoCapture(0) # Use 0 for the default camera
 
# Read a frame from the webcam or video
ret, frame = cap.read()
 
# Convert the frame to grayscale 
# (face detection works on grayscale images)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
 
# ...
 
 
 
Eye detection function

    Now, let's add eye detection within the detected faces. We'll use another Haar Cascade Classifier for eye detection. Then we can detect eyes in the region of interest (ROI) in an image. Here we can use detected face region as a ROI. The eye_cascade.detectMultiScale() function helps us to detect eyes in a given image region.
  
 
# Load the pre-trained Haar Cascade classifier for eye detection
eye_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_eye.xml')

# Detect eyes in the ROI
eyes = eye_cascade.detectMultiScale(roi_gray)
 
 
 
Face and eye detection example

   Now, let's take a look at an example of step-by-step face and eye detection from webcam frames with OpenCV.  We'll start loading the required libraries and Haar Cascade classifiers. Then we initialize the webcam (for video define video file name) and read the captured video from the video. We convert frame to grayscale and apply histogram equalization to improve the detection quality. We detect face and draw rectangle bounding box. For eye detection we extract ROI region from face, detect eyes and draw rectangle boxes for each eye. Finally, we display frames and detected areas. 'Esc' button will stop the capturing the video. 
 
 
import cv2
 

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


cap = cv2.VideoCapture(0)

while True:
# Read a frame from the webcam or video
ret, frame = cap.read()

# Convert the frame to grayscale 
# (face and eye detection works on grayscale images)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Apply histogram equalization
gray = cv2.equalizeHist(gray)

# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))

# Loop through the detected faces
for (x, y, w, h) in faces:
# Draw a rectangle around each detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Extract the region of interest (ROI) for eyes from the face
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]

# Detect eyes in the ROI
eyes = eye_cascade.detectMultiScale(
roi_gray)

# Loop through the detected eyes
for (ex, ey, ew, eh) in eyes:
# Draw a rectangle around each detected eye (within the face)
cv2.rectangle(roi_color, (ex, ey),
(ex + ew, ey + eh), (0, 255, 0), 2)

# Display the frame with detected faces and eyes
cv2.imshow('Face and Eye Detection', frame)

if cv2.waitKey(30) & 0xff == 27:
break

cap.release()
cv2.destroyAllWindows()

 
 
 Conclusion
    
    In this tutorial, you've learned how to detect face and eye from webcam video with OpenCV. Face and eye detections are fundamental building blocks for various computer vision applications. The tutorial gives you an idea of how to implement face and eye detection with OpenCV. You can use the provided code as a starting point for your projects and explore more advanced techniques, such as facial recognition and emotion analysis.  
 
 



No comments:

Post a Comment