Skip to content

This project demonstrates face detection from real-time or static-image-file using OpenCV's Haar Cascade classifier.

License

Notifications You must be signed in to change notification settings

flameAutomation/Face-Detection-OpenCV

 
 

Repository files navigation

Face Detection with OpenCV

This project demonstrates face detection from real-time or static-image-file using OpenCV's Haar Cascade classifier. In the real-time face detection program captures video from your webcam, detects faces in each frame, and draws rectangles around the detected faces on both project. If the camera is not detected or the image file is not found then programs will printing a alert!!

Requirements

  • Python 3.x
  • OpenCV

Installation

  1. Clone the repository:

    git clone https://github.com/flameAutomation/Face-Detection-OpenCV.git
    cd Face-Detection-OpenCV
  2. Install the required dependencies:

    pip install opencv-python

Usage

  1. Run the script:
    python face_detection_image.py

If the image is not detected, "Error: Unable to read image file unknown_path" will be printed.

This Python script uses OpenCV's Haar Cascade classifier to detect faces from a static image file.

or, Detect real-time face by using a webcam

  1. Run the script:
    python face_detection_webcam.py

If the camera is not detected, "Camera not responding" will be printed 50 times.

If the camera is detected, a window will open displaying the video feed from your webcam with rectangles drawn around detected faces.

Press the z key to exit the program.

Example

Before After
Before After

Code Explanation

# Import necessary libraries
import cv2
# Load the pre-trained Haar Cascade classifier for face detection
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Capture video from the default camera (usually the webcam)
cap = cv2.VideoCapture(0)
# Function to detect faces in an image file
def detect_faces_in_image(image_path):
# Read the image from file
image = cv2.imread(image_path)
# Check if the camera is opened successfully
if not cap.isOpened():
    print("Error: Camera not responding!")

# Check if the image is opened successfully
if image is None:
    print(f"Error: Unable to read image file '{image_path}'")
    return
# Read a frame from the video feed
ret, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

# Detect faces in the grayscale image
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
# 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 rectangles in a window
cv2.imshow('Face Detection', frame)

# Display the image with rectangles around faces
cv2.imshow('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Example usage
if __name__ == "__main__":
    image_path = 'demo.jpg' # Replace file path
    detect_faces_in_image(image_path)
# Break the loop if 'z' is pressed
if cv2.waitKey(90) & 0xFF == ord('z'):
    break
# Release the video capture object and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()

About

This project demonstrates face detection from real-time or static-image-file using OpenCV's Haar Cascade classifier.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 65.9%
  • Python 34.1%