0% found this document useful (0 votes)
28 views

Search Creators CG LAB Program-12

lab7
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)
28 views

Search Creators CG LAB Program-12

lab7
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/ 4

21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Subject Code:21CSL66

Subject: COMPUTER GRAPHICS AND IMAGE PROCESSING


LABORATORY

Program-12

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

Program

import cv2

# Load the cascade classifier for face detection

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

# Load the image

image = cv2.imread('image/face.jpeg')

# Convert the image to grayscale

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

# Detect faces in the grayscale image

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,


minSize=(30, 30))

# Draw rectangles around the detected faces


Search Creators…. Page 1
21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

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

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

# Display the image with detected faces

cv2.imshow('Face Detection', image)

# Wait for a key press to close the window

cv2.waitKey(0)

cv2.destroyAllWindows()

Output

Search Creators…. Page 2


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Explanation

1. This code demonstrates how to perform face detection in an image using


OpenCV in Python. Here's a breakdown of what the code does:

2. The cv2 library is imported from OpenCV.


3. The Haar cascade classifier for face detection is loaded using
cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml'). This classifier is a pre-trained model
that can detect frontal faces in images.
4. The image is loaded using cv2.imread('image/face.jpeg'). Make sure to
replace 'image/face.jpeg' with the correct path to your image file containing
faces.
5. The image is converted to grayscale using cv2.cvtColor(image,
cv2.COLOR_BGR2GRAY). Face detection is typically performed on
grayscale images.
6. The face_cascade.detectMultiScale method is used to detect faces in the
grayscale image. The parameters scaleFactor=1.1, minNeighbors=5, and
minSize=(30, 30) control the detection process:
7. scaleFactor=1.1 specifies the scale factor used to resize the input image for
different scales.
8. minNeighbors=5 specifies the minimum number of neighboring rectangles
that should overlap to consider a face detection as valid.
9. minSize=(30, 30) specifies the minimum size of the face to be detected.
10.For each detected face, a rectangle is drawn around it using
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2). The rectangle
coordinates are obtained from the faces list returned by detectMultiScale. The
Search Creators…. Page 3
21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

(0, 255, 0) argument specifies the color (green in this case), and the 2
argument specifies the thickness of the rectangle lines.
11.The image with the detected faces and rectangles is displayed using
cv2.imshow('Face Detection', image).
12.The script waits for a key press (cv2.waitKey(0)) before closing the window.
13.Finally, the window is closed using cv2.destroyAllWindows().

Search Creators…. Page 4

You might also like