0% found this document useful (0 votes)
10 views3 pages

12 Ip

The document details a program to detect faces in an image using OpenCV. It loads a pre-trained Haar cascade classifier, reads an input image, converts it to grayscale, detects faces, draws rectangles around detected faces, and displays the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

12 Ip

The document details a program to detect faces in an image using OpenCV. It loads a pre-trained Haar cascade classifier, reads an input image, converts it to grayscale, detects faces, draws rectangles around detected faces, and displays the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IP - Program – 12

Write a program to detect a face/s in an image.


# Import Necessary Modules
import cv2
import matplotlib.pyplot as plt

# 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')

# Read the input image


image = cv2.imread("images/image 9.jpg")

# Convert the image to grayscale


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

# Print Original Image


img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title("Original Image")
plt.axis("off")
plt.show()

# Detect faces in the image


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

# Draw rectangles around detected faces


for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Convert the image from BGR to RGB for Matplotlib


image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the result using Matplotlib


plt.imshow(image_rgb)
plt.title('Detected Faces')
plt.axis('off')
plt.show()

You might also like