0% found this document useful (0 votes)
8 views18 pages

TilakSrajal.pdf

This document outlines three projects: a mini project on face detection and recognition using OpenCV, a micro project on object detection using contour detection, and a macro project on image processing and object segmentation. The mini project employs machine learning techniques for real-time face recognition, while the micro project focuses on identifying object boundaries through contour detection. The macro project discusses various image processing techniques, including edge detection and morphological operations, for effective object segmentation.

Uploaded by

22am10sh34
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)
8 views18 pages

TilakSrajal.pdf

This document outlines three projects: a mini project on face detection and recognition using OpenCV, a micro project on object detection using contour detection, and a macro project on image processing and object segmentation. The mini project employs machine learning techniques for real-time face recognition, while the micro project focuses on identifying object boundaries through contour detection. The macro project discusses various image processing techniques, including edge detection and morphological operations, for effective object segmentation.

Uploaded by

22am10sh34
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/ 18

TABLE OF CONTENT

MINI PROJECT
S.no. Title Signature
1. Face Detection and Recognition Using OpenCV

MICRO PROJECT
S.no. Title Signature
1. Object Detection using Contour Detection

MACRO PROJECT
S.no. Title Signature
1. Image Processing and Object Segmentation
Mini Project
Title: Face Detection and Recognition Using OpenCV
Introduction: This project utilizes OpenCV and machine learning techniques to
detect and recognize faces in real time. By leveraging Haar cascade classifiers
for face detection and an SVM classifier for recognition, the system processes
live video input and identifies known individuals .

Technologies Used

Python: The primary programming language for scripting and processing.


OpenCV: Used for image and video processing, including face detection.
NumPy: Assists in handling image data and numerical computations.
Scikit-Learn: Provides the SVM classifier for face recognition.

This system integrates face detection and recognition into a


single framework. It captures human movements through a webcam, processes
the frames, and provides real-time analysis of:
1. Face Detection:
: Detects faces using Haar cascades.
Extracts and preprocesses face images.

2. Face Recognition:
:
Classifies detected faces using an SVM model.

Working Mechanism
1. Frame Capture & Processing
:
o Captures video frames using OpenCV.
o Converts frames to grayscale and applies histogram equalization.
2. Pose Detection
:
o Classifies detected faces using an SVM model.
o Identifies known individuals and labels them with confidence scores.
3. Face Recognition:

o Collects face samples for training.


o Trains an SVM model to classify faces.

4. Visualization & Output


:
o Displays detected faces with bounding boxes.
o Shows the name and recognition confidence for identified faces.

Key Features

Real-time Processing: Detects and recognizes faces instantly.


User-Friendly Interface: Provides visual feedback with
labeled faces.
Model Training: Allows users to collect face samples and train
the system.
Scalability: Can be extended to support additional individuals.

Potential Applications:

Security & Access Control: Used for identity verification and


sur veillance.
Attendance Systems: Automates attendance tracking in schools and
workplaces.
Personalized User Experience: Enables recognition-based
customization in applications.

Code:
import cv2
import numpy as np
import os
from sklearn.svm import SVC
import pickle
# Create directories if they don't exist
if not os.path.exists('faces'):
os.makedirs('faces')
if not os.path.exists('models'):
os.makedirs('models')

class SimpleFaceRecognition:
def _init_(self):
# Load face detection model (Haar Cascade)
self.face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Initialize classifier
self.classifier = SVC(kernel='linear', probability=True)
self.faces_db = {}
self.labels = []
self.faces = []
self.unique_people = set()

def preprocess_image(self, image):


"""Convert to grayscale and apply histogram equalization"""
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
return gray

def collect_faces(self, name, num_samples=10):


"""Collect face images using webcam"""
print(f"Collecting faces for {name}. Press 'q' to quit.")

cap = cv2.VideoCapture(0)
count = 0

while count < num_samples:


ret, frame = cap.read()
if not ret:
break

# Preprocess the image


preprocessed = self.preprocess_image(frame)

# Detect faces
faces = self.face_detector.detectMultiScale(
preprocessed,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)

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


# Draw rectangle around face
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

if count < num_samples:


# Extract face
face_img = preprocessed[y:y+h, x:x+w]

# Standardize face size


face_img = cv2.resize(face_img, (100, 100))

# Save face image


filename = f"faces/{name}_{count}.jpg"
cv2.imwrite(filename, face_img)
count += 1

print(f"Saved {count}/{num_samples}")

# Display progress
cv2.putText(frame, f"Samples: {count}/{num_samples}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow('Face Collection', frame)

# Press 'q' to quit


if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
print(f"Collected {count} samples for {name}")
def load_training_data(self):
"""Load face images from the faces directory"""
self.faces = []
self.labels = []
self.unique_people = set()

for file in os.listdir('faces'):


if file.endswith('.jpg'):
# Extract name from filename (e.g., "john_0.jpg" -> "john")
name = file.split('_')[0]
self.unique_people.add(name)

# Load image
img_path = os.path.join('faces', file)
face_img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)

# Add to training data


self.faces.append(face_img.flatten()) # Flatten image to 1D array
self.labels.append(name)

print(f"Loaded {len(self.faces)} face images for training")


print(f"Found {len(self.unique_people)} unique people: {', '.join(self.unique_people)}")

def train_model(self):
"""Train face recognition model"""
if len(self.faces) == 0:
print("No training data. Please collect face samples first.")
return False

# Check if we have at least 2 different people


if len(self.unique_people) < 2:
print("Error: Need at least 2 different people to train the model.")
print("Please collect samples for at least one more person.")
return False

# Convert training data to numpy arrays


X = np.array(self.faces)
y = np.array(self.labels)

# Train the classifier


self.classifier.fit(X, y)

# Save the trained model


with open('models/face_model.pkl', 'wb') as f:
pickle.dump(self.classifier, f)

print("Model trained successfully")


return True
def load_model(self):
"""Load trained model from file"""
try:
with open('models/face_model.pkl', 'rb') as f:
self.classifier = pickle.load(f)
print("Model loaded successfully")
return True
except:
print("No model found. Please train the model first.")
return False

def recognize_faces(self):
"""Run real-time face recognition"""
cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
break

# Preprocess the image


preprocessed = self.preprocess_image(frame)

# Detect faces
faces = self.face_detector.detectMultiScale(
preprocessed,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)

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


# Extract and preprocess face region
face_img = preprocessed[y:y+h, x:x+w]
face_img = cv2.resize(face_img, (100, 100))

# Prepare for prediction


face_features = face_img.flatten().reshape(1, -1)

# Predict face
prediction = self.classifier.predict_proba(face_features)[0]
idx = np.argmax(prediction)
confidence = prediction[idx] * 100
# Get name
name = self.classifier.classes_[idx]

# Set color based on confidence


if confidence > 50:
color = (0, 255, 0) # Green for high confidence
else:
color = (0, 0, 255) # Red for low confidence
name = "Unknown"

# Draw rectangle and label


cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
label = f"{name} ({confidence:.1f}%)"
cv2.putText(frame, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,

0.5, color, 2)

# Display the frame


cv2.imshow('Face Recognition', frame)

# Press 'q' to quit


if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

# Main function
def main():
system = SimpleFaceRecognition()

while True:
print("\n----- Face Recognition System -----")
print("1. Collect face samples")
print("2. Train recognition model")
print("3. Run face recognition")
print("4. Exit")

choice = input("Select an option (1-4): ")

if choice == '1':
name = input("Enter person's name: ")
system.collect_faces(name)

elif choice == '2':


system.load_training_data()
system.train_model()

elif choice == '3':


# Try to load the model first
if system.load_model():
system.recognize_faces()

elif choice == '4':


print("Exiting program...")
break

else:
print("Invalid option! Please try again.")
Result and Conclusion: This project successfully combines face detection

and recognition into a single system using OpenCV and machine learning

techniques. It provides real-time insights into facial identification, making it

highly useful for security, authentication, and human-computer interaction

applications.
Micro Project
Title: Object Detection using Contour Detection
Introduction Object detection is a key task in computer vision that aims to
identify and locate objects within an image. In this project, we use contour
detection to find and outline the boundaries of objects. Contour detection is an
edge-detection technique that identifies the outlines of objects based on
differences in pixel intensity. This method is particularly useful for identifying
distinct shapes or objects in images, even in noisy environments.

Objective The main goal of this project is to use contour detection to identify
objects in an image by detecting their boundaries and drawing contours
around them. This technique can be applied to applications like object
tracking, shape recognition, and image analysis.

Contour Detection
Contours are curves or boundaries in an image that represent the edges of
objects. They are useful for object detection and shape analysis. OpenCV
provides a robust algorithm to find contours in an image. The process
involves the following steps:

Edge Detection: Detect edges in the image using methods like Canny edge detection
Contour Extraction: Find the contours of the detected edges.
Contour Drawing: Draw contours on the original image to outline objects.

Methodology

Load the Image: Read the image from the file system.
Preprocess the Image: Convert the image to grayscale to simplify the analysis.

Apply Gaussian Blur: Reduce noise in the image to improve edge detection.
Detect Edges: Use edge detection (e.g., Canny edge detection) to identify potential
boundaries of objects.
Find Contours: Use OpenCV's findContours() function to extract the contours.
Draw Contours: Draw the contours on the original image to visualize the detected
objects

Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the image


image_path = "box2.jpg" # Make sure this image is in the same folder
image = cv2.imread(image_path)

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian Blur to reduce noise


blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# Apply edge detection (Canny)


edges = cv2.Canny(blurred, 50, 150)

# Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on the original image


contour_image = image.copy()
cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2) # Green contours

# Display the original and contour-detected images


plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis("off")

plt.subplot(1, 2, 2)
plt.title("Contour Detection")
plt.imshow(cv2.cvtColor(contour_image, cv2.COLOR_BGR2RGB))
plt.axis("off")

plt.show()
Results & Conclusion

The experiment was conducted using an image of a box and objects inside it. The contour
detection successfully outlined the boundaries of the objects. Here's what we observed:

Effective Object Detection: The contour detection method successfully identified the
objects in the image, drawing clear outlines around them.

Edge Detection Precision: The Canny edge detection highlighted the borders of objects
effectively, making it easier to identify their contours.

Comparison of Original and Contour-detected Images:


The original image contains all the details of the objects, including their color and texture.
The contour-detected image highlights the boundaries of each object, making it simpler to
distinguish between different objects.
Macro Project
Title: Image Processing Introduction
and Object Segmentation Using OpenCV
Image processing is a crucial step in computer vision, enabling feature extraction,
enhancement, and segmentation for various applications such as medical imaging,
autonomous vehicles, and surveillance. This document explains the image
processing techniques applied in the given script, including preprocessing, edge
detection, morphological operations, and object segmentation.

Methodology
The image processing workflow consists of the following steps:

Preprocessing
Convert the image to grayscale to reduce complexity and computational
cost.
Apply Gaussian Blur to smooth the image and reduce noise.
Edge Detection
Canny Edge Detection: Detects edges by computing gradients and
applying non-maximum suppression.
Sobel Edge Detection (X & Y): Computes gradients in horizontal and
vertical directions.
Laplacian Edge Detection: Captures variations in intensity to detect
edges.

Morphological Operations
Dilation: Expands white regions, making objects more visible.
Erosion: Shrinks white regions, removing noise and small details.
Opening: Erosion followed by dilation, used to remove small noise.
Closing: Dilation followed by erosion, used to fill small holes.

Object Segmentation
Apply thresholding to create a binary image.
Find external contours using cv2.findContours.
Draw contours on a blank image to highlight segmented objects.
Code:
import cv2
import numpy as np
import os

def preprocess_image(image):
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian Blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
return blurred

def edge_detection(image):
edges_canny = cv2.Canny(image, 100, 200)
edges_sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
edges_sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
edges_laplacian = cv2.Laplacian(image, cv2.CV_64F)
return edges_canny, edges_sobelx, edges_sobely, edges_laplacian

def morphological_operations(image):
kernel = np.ones((5,5), np.uint8)
dilation = cv2.dilate(image, kernel, iterations=1)
erosion = cv2.erode(image, kernel, iterations=1)
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
return dilation, erosion, opening, closing

def object_segmentation(image):
_, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
segmented = np.zeros_like(image)
cv2.drawContours(segmented, contours, -1, (255, 255, 255), thickness=cv2.FILLED)
return segmented, contours

def process_images_in_folder(folder_path):
output_folder = os.path.join(folder_path, 'processed')
os.makedirs(output_folder, exist_ok=True)

for filename in os.listdir(folder_path):


if filename.endswith(('.png', '.jpg', '.jpeg')):
image_path = os.path.join(folder_path, filename)
image = cv2.imread(image_path)
preprocessed = preprocess_image(image)
edges = edge_detection(preprocessed)
morph = morphological_operations(preprocessed)
segmented, _ = object_segmentation(preprocessed)
cv2.imwrite(os.path.join(output_folder, f'preprocessed_{filename}'), preprocessed)
cv2.imwrite(os.path.join(output_folder, f'edges_canny_{filename}'), edges[0])
cv2.imwrite(os.path.join(output_folder, f'morph_opening_{filename}'), morph[2])
cv2.imwrite(os.path.join(output_folder, f'segmented_{filename}'), segmented)

print(f"Processing complete. Results saved in {output_folder}")

# Example usage
folder_path = "./images-2" # Change this to the path of your images folder
process_images_in_folder(folder_path)
Results & Conclusion:
Original Image:

Segmented Image: Processed image:


Morph opening image: Edges canny image:

After processing, the following output images are generated:


1. Preprocessed Image: Grayscale and smoothed version of the original
image.
2. Edge Detection Results: Images showing detected edges using Canny,
Sobel, and Laplacian methods.
3. Morphological Transformations: Images demonstrating dilation, erosion,
opening, and closing.
4. Segmented Image: A binary image highlighting the detected objects.
The implemented pipeline efficiently processes images for feature extraction
and segmentation.

1. Preprocessing enhances image quality and reduces noise.


2. Edge detection highlights object boundaries.
3. Morphological operations refine edges and remove noise.
4. Segmentation effectively isolates objects for further analysis.

This approach can be extended to real-world applications such as medical


image analysis, autonomous driving, and industrial defect detection. Future
improvements could involve deep learning-based segmentation techniques for
more accurate results.

You might also like