Nothing Special   »   [go: up one dir, main page]

CG Program No. 12-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY 21CSL66

Program No. 12

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


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('desktop/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

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()

NOOR SUMAIYA, ASST.PROF, DEPT OF CSE, TOCE Page 1


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY 21CSL66

Output:

NOOR SUMAIYA, ASST.PROF, DEPT OF CSE, TOCE Page 2

You might also like