Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

Face detection in Python using the OpenCV library

You can perform face detection in Python using the OpenCV library. Here's a simple example:


import cv2

# Load the pre-trained face detection model

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Load an image

image = cv2.imread('image.jpg')

# Convert the image to grayscale

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image

faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

# Draw rectangles around the detected faces

for (x, y, w, h) in faces:

    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the image with the detected faces

cv2.imshow('Face Detection', image)




Make sure to replace `'image.jpg'` with the path to your input image. This code loads a pre-trained face detection model, applies it to the input image, and draws rectangles around the detected faces. Finally, it displays the image with the detected faces.

You can install OpenCV using pip:

pip install opencv-python


This is a simple example to get you started with face detection. OpenCV provides more advanced techniques for face detection and recognition, such as using deep learning models. You can explore those options based on your requirements.

caa March 29 2024 269 reads 0 comments Print


Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?