0% found this document useful (0 votes)
23 views4 pages

Face Detection

The document contains a Python script that uses OpenCV to detect faces in images. It loads an image, converts it to grayscale, and applies a Haar Cascade Classifier to identify faces, displaying the results with rectangles around detected faces. Additionally, it processes another image and saves the output with detected faces highlighted.

Uploaded by

Atharav Pawar
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)
23 views4 pages

Face Detection

The document contains a Python script that uses OpenCV to detect faces in images. It loads an image, converts it to grayscale, and applies a Haar Cascade Classifier to identify faces, displaying the results with rectangles around detected faces. Additionally, it processes another image and saves the output with detected faces highlighted.

Uploaded by

Atharav Pawar
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

import numpy as np

import cv2
import matplotlib.pyplot as plt
%matplotlib inline

# Helper function to convert BGR images to RGB


def convertToRGB(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Load and convert the first image to grayscale


test_image_path = '/content/driver.jpeg'
test_image = cv2.imread(test_image_path)

if test_image is not None:


# Convert to grayscale
test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)

# Display grayscale image


plt.imshow(test_image_gray, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')
plt.show()

# Load Haar Cascade Classifier


haar_cascade_face = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_alt2.xml')

# Detect faces
faces_rects = haar_cascade_face.detectMultiScale(test_image_gray,
scaleFactor=1.2, minNeighbors=5)

# Print the number of faces found


print('Faces found:', len(faces_rects))

# Draw rectangles around faces


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

# Display the result


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

else:
print("Error: Test image not found at", test_image_path)

# Function to detect faces in any given image


def detect_faces(cascade, image, scaleFactor=1.2):
# Create a copy of the image to avoid altering the original
image_copy = image.copy()

# Convert to grayscale
gray_image = cv2.cvtColor(image_copy, cv2.COLOR_BGR2GRAY)

# Detect faces
faces_rect = cascade.detectMultiScale(gray_image,
scaleFactor=scaleFactor, minNeighbors=5)

# Draw rectangles around each face


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

return image_copy

# Paths to additional images


additional_image_paths = ['/content/images (2).jpeg']

for idx, image_path in enumerate(additional_image_paths):


# Load image
test_image2 = cv2.imread(image_path)

if test_image2 is not None:


# Detect faces in the image
faces = detect_faces(haar_cascade_face, test_image2)

# Display the result


plt.imshow(convertToRGB(faces))
plt.title(f'Detected Faces in Image {idx+1}')
plt.axis('off')
plt.show()

# Save the result


output_filename = f'detected_faces_{idx+1}.png'
cv2.imwrite(output_filename, faces)
print(f'Saved: {output_filename}')
else:
print(f"Error: Image not found at {image_path}")
Faces found: 1
Saved: detected_faces_1.png

You might also like