0% found this document useful (0 votes)
309 views49 pages

Ccs349 Iva Record - Final

Uploaded by

Keerthi .P
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)
309 views49 pages

Ccs349 Iva Record - Final

Uploaded by

Keerthi .P
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/ 49

GOVERNMENT COLLEGE OF ENGINEERING SRIRANGAM

SETHURAPATTI, TRICHY – 620 012.

B.E. COMPUTER SCIENCE AND ENGINEERING


VI – SEMESTER
(R-2021 Regulation)

2023-2024

CCS349 – Image and Video Analytics


Programming Lab

Name .………………………………

Reg.No .………………………………
GOVERNMENT COLLEGE OF ENGINEERING SRIRANGAM
SETHURAPATTI, TRICHY – 620 012.
(Approved by AICTE and Affiliated to Anna University, Chennai)

Anna University Register No:

CERTIFICATE

Certified that this is the bonafide record of work done by


Mr./Ms. …………………………..…………………………… of VI Semester B.E.
Computer Science and Engineering in CCS349 – IMAGE AND VIDEO
ANALYTICS (Theory-cum-Practical course) during the academic year
2023 – 2024.

Faculty In-Charge Head of the Department

Submitted for the Anna University Practical Examination held on ……………

Internal Examiner External Examiner


INDEX

SL.NO DATE NAME OF THE EXPERIMENT PAGE NO. SIGNATURE

1. COMPUTATION OF T-PYRAMID OF
AN IMAGE
QUAD TREE REPRESENTATION OF
2.
AN IMAGE USING HOMOGENEOUS
CRITERION OF EQUAL INTENSITY
3 a. GEOMETRIC TRANSFORMATION OF
AN IMAGE - ROTATION
3 b. GEOMETRIC TRANSFORMATION OF
AN IMAGE – CHANGE OF SCALE
3 c. GEOMETRIC TRANSFORMATION OF
AN IMAGE – SKEWING
AFFINE TRANSFORMATION OF AN
3 d.
IMAGE FROM THREE PAIRS OF
CORRESPONDING POINTS
BILINEAR TRANSFORMATION OF
3 e.
AN IMAGE FROM FOUR PAIRS OF
CORRESPONDING POINTS
4. OBJECT DETECTION AND
RECOGNITION
5. MOTION ANALYSIS FROM MOVING
EDGES
6. FACIAL DETECTION AND
RECOGNITION
EVENT DETECTION IN VIDEO
7.
SURVEILLANCE SYSTEM
8. MINI PROJECT
EXP NO :1

DATE:

COMPUTATION OF T-PYRAMID OF AN IMAGE

AIM:

To write a python program to compute the T-pyramid of an image.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv library as cv2 for image
processing and Matplotlib as plt for displaying images.
Step 2: Load the images in the cv2.imread and store it in the img variable.
Step 3: Create a copy of the loaded image and store it in the variable as layer.
Step 4: Create a loop to generate an image pyramid and iterate four times using a
“for” loop in I in ranging 0 to 3.
Step 5: Inside the loop use plt.subplot function to set up the grid of subplots in a
2 × 2 configuration.
Step 6: Apply the cv2.pyrDown function to the Layer image and this function
reduces the size of the image by half in each dimension.
Step 7: Use the plt.imshow to display the current layer of the subplot.
Step 8: After the loop is complete close all the Opencv windows using the
cv2.destroyAllWindows.

PROGRAM:

import cv2
import numpy as np
# Define function to generate T pyramid
def T_pyramid(image, levels):
pyramid = [image]
for i in range(levels):
image = cv2.pyrDown(image)
pyramid.append(image)
return pyramid
# Load image
image = cv2.imread(r'E:\nature.jpg')
if image is None:
print("Error: Image not found.")
exit()

# Define number of pyramid levels


levels = 4

# Generate T pyramid
pyramid = T_pyramid(image, levels)

# Display T pyramid
for i in range(len(pyramid)):
cv2.imshow(f'Level {i} T Pyramid', pyramid[i])

cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
RESULT:

Thus, the python program to compute the T-pyramid of an image was written and the
output was verified successfully.
EXP NO :2

DATE:

QUAD TREE REPRESENTATION OF AN IMAGE USING


HOMOGENEITY CRITERION OF EQUAL INTENSITY

AIM:

To write a python program that derives the Quad tree representation of an


image using the homogeneity criterion of equal intensity

ALGORITHM:

Step 1: Import the necessary libraries such as Matplotlib for visualization, cv2 for
image processing, NumPy for numerical operations.
Step 2: Load the input image in the imread.
Step 3: Define a split4(image) that splits the input image into 4 equal quadrants.
Step 4: Create a 2 × 2 subplot grid using plt.subplots and display the four
quadrants of the image in the subplot using plt.imshow.
Step 5: Define a function concatenate4(north_west , north_east , south_west ,
south_east) that concatenate the four images into single image.
Step 6: Combine the four images into a single image using the concatenate4
function and display the full image using plt.imshow.
Step 7: Define a function calculate_mean to calculate the mean colour of the image
and calculate the mean value of each four quadrants and store them in the
means variable.
Step 8: Display the mean colour of the image using plt.imshow.
Step 9: Define a function checkEqual(list) to check if all elements in the list are
equal
Step 10: Create a QuadTree class for building the Quadtree structure and create a
Quad tree structure for the input image QuadTree().insert(img).
Step 11: Display the Quadtree image at different levels of details using plt.imshow.
PROGRAM:

import matplotlib.pyplot as plt


matplotlib.image as mpimg
import cv2 as cv
import numpy as np

img =cv.imread(r'E:\tree.jpg')
plt.imshow(img)

from operator import add


from functools import reduce

def split4(image):
half_split = np.array_split(image, 2)
res = map(lambda x: np.array_split(x, 2, axis=1), half_split)
return reduce(add, res)

split_img = split4(img)
split_img[0].shape
fig, axs = plt.subplots(2, 2)
axs[0, 0].imshow(split_img[0])
axs[0, 1].imshow(split_img[1])
axs[1, 0].imshow(split_img[2])
axs[1, 1].imshow(split_img[3])

def concatenate4(north_west, north_east, south_west, south_east):


top = np.concatenate((north_west, north_east), axis=1)
bottom = np.concatenate((south_west, south_east), axis=1)
return np.concatenate((top, bottom), axis=0)

full_img = concatenate4(split_img[0], split_img[1], split_img[2], split_img[3])


plt.imshow(full_img)
plt.show()

def calculate_mean(img):
return np.mean(img, axis=(0,1))

means = np.array(list(map(lambda x:
calculate_mean(x),split_img))).astype(int).reshape(2,2,3)
print(means)
plt.imshow(means)
plt.show()

def checkEqual(myList):
first=myList[0]
return all((x==first).all() for x in myList)

class QuadTree:

def insert(self, img, level = 0):


self.level = level
self.mean = calculate_mean(img).astype(int)
self.resolution = (img.shape[0], img.shape[1])
self.final = True

if not checkEqual(img):
split_img = split4(img)
self.final = False
self.north_west = QuadTree().insert(split_img[0], level + 1)
self.north_east = QuadTree().insert(split_img[1], level + 1)
self.south_west = QuadTree().insert(split_img[2], level + 1)
self.south_east = QuadTree().insert(split_img[3], level + 1)
return self
def get_image(self, level):
if(self.final or self.level == level):
return np.tile(self.mean, (self.resolution[0], self.resolution[1], 1))
return
concatenate4(self.north_west.get_image(level),self.north_east.get_image(level),self.so
uth_west.get_image(level),self.south_east.get_image(level))

quadtree = QuadTree().insert(img)
plt.imshow(quadtree.get_image(1))
plt.show()
plt.imshow(quadtree.get_image(3))
plt.show()
plt.imshow(quadtree.get_image(7))
plt.show()
plt.imshow(quadtree.get_image(10))
plt.show()

OUTPUT:
RESULT:

Thus, the python program for Quad tree representation of an image using homogeneous
criterion of equal intensity was written and the output was verified successfully.
EXP.NO:3(a)

DATE:

GEOMETRICTRANSFORMATION OF AN IMAGE - ROTATION

AIM:

To write a python program for the geometric transformation of image - rotation.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv libraries as cv2 and NumPy
libraries as np in image processing.
Step 2: Load the images in the cv2.imread function to read the image and store it
in the variable img.
Step 3: Extract the number of rows, columns and channels from the loaded image
using img.shape .
Step 4: Use the cv2.getRotationMatrix2D () function to create a rotation matrix
fora 90-degree clockwise rotation.
Step 5: Use the cv2.WrapAffine function to apply the rotation to the input image
and store it in the rotated_img.
Step 6: And then display the both the rotated and original image by using the
cv2.imshow function.
Step 7: Then destroyAllWindows function to closes all the displayed windows.

PROGRAM:

#import necessary libraries


import cv2
import numpy as np

#Read the input image


img = cv2.imread(r'E:\tree.jpg')
#Retrieve rows, columns and channels of the image and store them in a variable
rows, cols, ch = img.shape

#Create rotation matrix using a function to rotate the image 90 degrees clockwise
matrix_rotated = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.6)

#Apply rotation to the image using warpAffine function


rotated_img = cv2.warpAffine(img, matrix_rotated, (cols, rows))

#display input and rotated images


cv2.imshow("image",img)
cv2.imshow("Rotated image",rotated_img)

#close all the windows opened


cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:

ROTAED IMAGE
INPUT IMAGE

RESULT:

Thus, the python program to perform geometric transformation of an image i.e. rotation
was written and the output was verified successfully.
EXP.NO:3(b)

DATE:

GEOMETRICTRANSFORMATION OF AN IMAGE – CHANGE OF SCALE

AIM:

To write a python program for the geometric transform of an image in


change of scale.

ALGORITHM:

Step 1: Import the necessary library such as Opencv library as cv2 and NumPy
as np for changing the scale of the image.
Step 2: Use the cv2.imread function to read the image file and store it in the img.
Step 3: Now, we can use the cv2.resize() function to scale the input image and
we can use the parameters such as fx and fy to specify the horizontal and
vertical scaling factor.
Step 4: And then we can show the scaled image using the variable scaled_img.
Step 5: And then we can use the cv2.destroyAllWoindows to closes all the
displayed windows.

PROGRAM:

#Import necessary libraries


import cv2
import numpy as np

#Read the input image


img = cv2.imread(r'E:\tree.jpg')

#Scale the input image in x and y direction


scaled_img = cv2.resize(img, None, fx=2.0, fy=3.0)
#display the input and scaled images
cv2.imshow("Original image",img)
cv2.imshow("Scaled image",scaled_img)

#close all the windows opened


cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:

RESULT:

Thus, the python program to perform geometric transformation of an image i.e. change
of scale was written and the output was verified successfully.
EXP.NO:3(c)

DATE:

GEOMETRICTRANSFORMATION OF AN IMAGE – SKEWING

AIM:

To write a python program for the geometric transformation of an image - Skewing.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv libraries as cv2 and
NumPylibraries as np in image processing.
Step 2: Load the images in the cv2.imread function to read the image and
store itin the variable img
Step 3: Retrive the number of rows , columns , and channels from the loaded
imageusing the img.shape
Step 4: Use the cv2.getPerspectiveTransform() function to calculate the
prespective transformation matrix of the image.
Step 5: And then use the im.show function to display the skewed image
with the window name ‘Transformed’.
Step 6: Use cv2.imwrite() to save the transformed image as the current directory.

PROGRAM:

#import necessary libraries


import cv2
import numpy as np

#Read the input image


img = cv2.imread(r'E:\tree.jpg')

#Extract rows, columns and channels of input image and store in a variable
rows, cols, ch = img.shape
#define two sets of points representing source and destination using numpy array
pts1 = np.float32([[cols*0.45, rows*.95],[cols*.90, rows*.95],[cols*.10, 0],[cols, 0]])
pts2 = np.float32([[cols*0.1, rows],[cols,rows],[0,0],[cols,0]])

#Calculate perfpective transformation matrix using a function


M = cv2.getPerspectiveTransform(pts1,pts2)
#Apply perspective transformation
dst = cv2.warpPerspective(img, M, (cols, rows))

#display the original and transformed images


cv2.imshow('Original image',img)
cv2.imshow('Transformed', dst)

#Store the transformed image into a separate directory


cv2.imwrite('E:\My Zen Garden.jpg', dst)
#Close the window
cv2.waitKey()

OUTPUT:

RESULT:

Thus, the python program to perform geometric transformation of an image i.e.


skewing was written and the output was verified successfully.
EXP.NO:3(d)

DATE:

AFFINE TRANSFORMATION OF AN IMAGE FROM THREE


PAIRS OFCORRESPONDING POINTS

AIM:

To write a python program for the geometric transforms calculated from


three pairs of corresponding points.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv libraries as cv2, NumPy
libraries as np in image processing and pyplot module from the matplotlib.
Step 2: Read the image file using the cv2.imread and store the image in the img
variable.
Step 3: Retrieve the number pf rows, columns and channels of the input image
using the img.shape and store the values in the rows , cols and ch variable.
Step 4: Define the two sets of points pts1 and pts2 using the numpy arrays and
these points are used to define the transformation.
Step 5: Pts1 represents the source points and the pts2 represents the corresponding
destination points.
Step 6: Use the wrap affine function to apply the affine transformation. And use
the plot function is to display the two subplots with input and the output
images.
Step 7: And destroyAllWindows function is used to close all the output windows
which has been opened.

PROGRAM:

#Import necessary libraries

import cv2

import numpy as np
from matplotlib import pyplot as plt

#Read the input image

img = cv2.imread(r'E:\tree.jpg')

#Retrieve the number pf rows, columns and channels of the input image and store in a

variable

rows, cols, ch = img.shape

#define two sets of points representing source and destination using numpy arrays

pts1 = np.float32([[50, 50],[200, 50],[50, 200]])

pts2 = np.float32([[10, 100],[200, 50],[100, 250]])

#Use affinetransform function

M = cv2.getAffineTransform(pts1, pts2)

dst = cv2.warpAffine(img, M, (cols, rows))

#display input and output images in subplot

plt.subplot(121)

plt.imshow(img)

plt.title('Input')

plt.subplot(122)

plt.imshow(dst)

plt.title('Output')

plt.show()

#close all output windows opened

cv2.destroyAllWindows()
OUTPUT:

RESULT:

Thus, the python program to perform affine transformation of an image using three
pairs of corresponding points was written and the output was verified successfully.
EXP.NO:3(e)

DATE:

BILINEAR TRANSFORMATION OF AN IMAGE FROM FOUR


PAIRS OFCORRESPONDING POINTS

AIM:

To write a python program for the geometric transformation of an image


in bilinear transform calculated from four pairs of corresponding points.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv libraries as cv2, NumPy
libraries as np in image processing.
Step 2: Read the image in the imread() function and store it in the img variable.
Step 3: Create a list of 4 pairs of (x,y) coordinates in the points variable and these
points represents the location in the image where bilinear interpolition will
be performed.
Step 4: Create a function called bilinear_interpolation that takes an image and the
(x,y) coordinates as input.
Step 5: And calculate the integer parts of the coordinates as x1 and y1 and their
corresponding images as x2 and y2.
Step 6: Calculate the fractional parts dx and dy by subtracting the integer parts
from the original coordinates.
Step 7: Retrieve the pixel values in the four surroundings points in the image such
as top-left(tf), top-right(tr), bottom-left(bl) and bottom-right(br).
Step 8: For each point in the list draw a red circle at the point on the image using
the circle function and this visually marks the interpolation points with a
red circle.
Step 9: Use the imshow function to display the output image with the interpolated
points marked.
Step 10: The waitKey() and the destroyAllWindows() functions is used to close all
the output images.

PROGRAM:

#import necessary libraries


import cv2
import numpy as np

# Load an image (replace 'your_image.jpg' with the path to your image)


image = cv2.imread(r'E:\tree.jpg')

# Define 4 pairs of points as (x, y) coordinates


points = [(70, 90), (50, 60), (60, 70), (50, 20)]

def bilinear_interpolation(image, x, y):


x1, y1 = int(x), int(y)
x2, y2 = x1 + 1, y1 + 1

# Calculate the fractional parts


dx = x - x1
dy = y - y1

# Get the pixel values at the four surrounding points


tl = image[y1, x1]
tr = image[y1, x2]
bl = image[y2, x1]
br = image[y2, x2]

# Perform bilinear interpolation


result = (1 - dx) * (1 - dy) * tl + dx * (1 - dy) * tr + (1 - dx) * dy * bl + dx *dy * br
return result
# Perform bilinear interpolation for each specified point
for point in points:
x, y = point
interpolated_value = bilinear_interpolation(image, x, y)
print(f"Interpolated value at ({x}, {y}): {interpolated_value}")

# Optionally, display or save the image with interpolated points marked


for point in points:
x, y = point
cv2.circle(image, (x, y), 5, (0, 0, 255), -1)

# Draw a red circle at each point


# Save or display the image with interpolated points
cv2.imshow('Interpolated Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
RESULT:

Thus, the python program to perform bilinear transformation of an image using four
pairs of corresponding points was written and the output was verified successfully.
EXP.NO:4

DATE:

OBJECT DETECTION AND RECOGNITION

AIM:

To write a python program to implement the object detection and


recognition.

ALGORITHM:

Step 1: Import the necessary libraries such as Opencv for the computer vision and
matplotlib.pyplot for displaying the image.
Step 2: Read the image using imread function and store it in the img variable.
Step 3: Convert the BGR colour image to grayscale image using the cvtcolor and
store it in the gray variable and the Grayscale image are often used for the
object detection because they reduce the complexity of the image.
Step 4: And then converts the BGR image to the RGB format using the cvtcolor
and store it in the rgb variable and this conversion is necessary for the
proper display in the Matplotlib.
Step 5: Load the classifier in the XML file and stores it in the data variable and the
classifier is used for detecting the cat faces in the grayscale image.
Step 6: Calls the detectMultiScale() method on the grayscale image with specified
parameters, including the minimum object size for detection.
Step 7: The detected objects are stored in the found variables as a list of rectangles
where the each rectangle is represented by the four values (x,y) coordinates
of the top-left corner,width and height.
Step 8: Checks if the length of the found list is not equal to 0, indicating the cat
faces were detected.
Step 9: Iterate through the list of detected rectangles and draw green rectangles
around each detected cat face using cv2.rectangle and the coordinates
,width and height are extracted from each rectangle in the found list and
the rectangles are drawn on the RGB image.
Step 10: Using the matplotlib to display the RGB image with detected cat faces and
the green rectangles.
Step 11: And display the image with the detected result using the plt.show function.

PROGRAM:

#Import necessary libraries


import cv2
from matplotlib import pyplot as plt

# Read the input image


img = cv2.imread(r"E:\sign.jpg")

# OpenCV opens images as BRG


# but we want it as RGB We'll
# also need a grayscale version
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Use minSize because for not


# bothering with extra-small
# dots that would look like STOP signs
stop_data = cv2.CascadeClassifier('E:\stop_data.xml')

found = stop_data.detectMultiScale(img_gray, minSize =(20, 20))

# Don't do anything if there's no sign


amount_found = len(found)

if amount_found != 0:

# There may be more than one sign in the image


for (x, y, width, height) in found:

# We draw a green rectangle around


# every recognized sign
cv2.rectangle(img_rgb, (x, y), (x + height, y + width), (0, 255, 0), 5)

# Creates the environment of


# the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
print('Traffic sign detected')

OUTPUT:
RESULT:

Thus, the python program for object detection and recognition was written and the
output was verified successfully.
EXP.NO:5

DATE:

MOTION ANALYSIS USING MOVING EDGES

AIM:

To write a python program to implement the program for the motion


analysis using moving edges and apply it to the image sequence.

ALGORITHM:

Step 1: Import the necessary libraries cv2 (OpenCV) for computer vision tasks and
numpy for numerical operations.
Step 2: Define a function motion_analysis that takes a sequence of images
(img_seq) as input.
Step 3: Convert each image in the sequence from color to grayscale and store them
in the gray_seq list.
Step 4: Initialize an empty list motion_vect to store the number of moving vectors
between frames.
Step 5: Initialize the prev_frame variable with the first frame from gray_seq. This
will be used as the reference frame.
Step 6: Use cv.goodFeaturesToTrack to find good feature points (corners) in the
prev_frame. These feature points will be tracked from frame to frame. The
prev_pts variable stores the initial set of points.
Step 7: Calculate optical flow using cv.calcOpticalFlowPyrLK between the
prev_frame and the current frame. This function estimates the motion of
the feature points.
Step 8: Filter the points based on the status returned by cv.calcOpticalFlowPyrLK.
Only points with a status of 1 are considered valid, meaning they were
successfully tracked from the previous frame to the current frame.
Step 9: Extract the valid feature points and their corresponding positions in the
current frame as next_pts.
Step 10: Append the count of valid points (number of moving vectors) to the
motion_vect list.
Step 11: Update prev_frame to be the current frame for the next iteration.
Step 12: Return the motion_vect list, which contains the number of moving vectors
for each frame in the input sequence.
Step 13: Update prev_pts with the valid feature points for the next iteration.
Step 14: Load a sequence of images (img_seq) into the list. Each image corresponds
to a frame in the video sequence.
Step 15: Call the motion_analysis function with the img_seq as input to perform
motion analysis.
Step 16: Loop through the motion_vect list and print the number of moving vectors
for each frame.

PROGRAM:

import cv2
import numpy as np

# Function to perform motion analysis using moving edges


def motion_analysis(video_path):
# Open the video file
cap = cv2.VideoCapture(video_path)

# Check if the video file opened successfully


if not cap.isOpened():
print("Error: Could not open video file.")
return
# Read the first frame
ret, prev_frame = cap.read()

# Convert the first frame to grayscale


prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)

# Loop through the frames


while True:
# Read the next frame
ret, next_frame = cap.read()

# If no frame is read, break the loop


if not ret:
break

# Convert the next frame to grayscale


next_gray = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY)

# Calculate the absolute difference between the current and previous frames
diff = cv2.absdiff(next_gray, prev_gray)

# Apply a threshold to the difference image_, thresh = cv2.threshold(diff, 30, 255,


cv2.THRESH_BINARY)

# Find contours in the thresholded image


contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on the original frame


cv2.drawContours(next_frame, contours, -1, (0, 255, 0), 2)
# Display the frame with moving edges
cv2.imshow('Motion Analysis', next_frame)

# Update the previous frame


prev_gray = next_gray

# Check for the 'q' key to quit


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

# Release the video capture object and close all windows


cap.release()
cv2.destroyAllWindows()

# Main function
def main():
# Path to the video file
video_path = "E:\car.mp4" # Replace this with the path to your video file

# Perform motion analysis


motion_analysis(video_path)

if __name__ == "__main__":
main()
OUTPUT:

RESULT:

Thus, the python program for motion analysis using moving edges was written and the
output was verified successfully.
EXP.NO:6

DATE:

FACIAL DETECTION AND RECOGNITION

AIM:

To write a python program for facial detection andrecognition.

ALGORITHM:

Step 1: Import the necessary libraries deepface for face analysis , cv2 (OpenCV)
for image manipulation matplotlib.pyplot for displaying the image with
annotations.
Step 2: Load an image from a file (in this case, "m1.jpg") using OpenCV and store
it in the img variable.
Step 3: Use the DeepFace library to analyze the face in the loaded image
Step 4: DeepFace.analyze(img, actions=['age', 'gender']) performs face analysis on
the image img and specifies that you want to extract information about age
and gender.
Step 5: Extract the gender and age information from the analysis results. These
results are stored in the res variable, which is a list of dictionaries, and you
access the values using dictionary keys.
Step 6: Retrieve the face region coordinates (x, y, width, and height) from the
analysis results. This information is stored in the face_axis dictionary
within the first dictionary in the res list.
Step 7: Draw a green rectangle around the detected face using OpenCV's
cv.rectangle function. The rectangle coordinates are based on the x, y,
width, and height values obtained from the face_axis dictionary.
Step 8: Convert the image from BGR color space to RGB color space using
OpenCV's cv.cvtColor function. This step is necessary for displaying the
image correctly with Matplotlib.
Step 9: Display the image with the annotated face region using Matplotlib. The
plt.imshow function is used to show the image, and the plt.title function
adds a title with gender and age information.
Step 10: Finally, use plt.show() to display the annotated image in a pop-up window.

PROGRAM:

import cv2
import numpy as np

# Load a pre-trained face detection classifier


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

# Load the image of the person you want to recognize


known_face = cv2.imread('E:\known_face.jpg')

# Convert it to grayscale
known_face_gray = cv2.cvtColor(known_face, cv2.COLOR_BGR2GRAY)

# Detect keypoints on the known face


known_face_keypoints = face_cascade.detectMultiScale(known_face_gray,
scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

# Extract the first face detected (assuming only one face is present)
x, y, w, h = known_face_keypoints[0]
known_face_roi = known_face_gray[y:y+h, x:x+w]

# Function to recognize a face


def recognize_face(frame):
# Convert frame to grayscale
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the frame


faces = face_cascade.detectMultiScale(frame_gray, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30))

# Iterate over detected faces


for (x, y, w, h) in faces:
# Extract the region of interest (ROI) for the detected face
roi_gray = frame_gray[y:y+h, x:x+w]

# Resize the known face ROI to match the size of the detected face ROI
known_face_resized = cv2.resize(known_face_roi, (w, h))

# Calculate the absolute difference between the known face and the detected face
diff = cv2.absdiff(roi_gray, known_face_resized)

# Calculate the mean squared error as a measure of similarity


mse = np.mean(diff)
# Define a threshold for similarity
similarity_threshold = 40

# If the mean squared error is below the threshold, recognize the face
if mse < similarity_threshold:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, 'Recognized', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 255, 0), 2)
else:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.putText(frame, 'Unknown', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 0, 255), 2)
return frame

# Open the default camera


cap = cv2.VideoCapture(0)

while True:
# Read frame from the camera
ret, frame = cap.read()

# If frame is successfully read


if ret:
# Recognize faces in the frame
frame = recognize_face(frame)

# Display the resulting frame


cv2.imshow('Face Recognition', frame)

# Break the loop if 'q' is pressed


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

# Release the camera and close all OpenCV windows


cap.release()
cv2.destroyAllWindows()
OUTPUT:

RESULT:

Thus, the python program for facial detection and recognition was written and the
output was verified successfully.
EXP.NO:7

DATE:

EVENT DETECTION IN VIDEO SURVEILLANCE SYSTEM

AIM:

To write a python program for event detection in the video surveillance


system.

ALGORITHM:

Step 1: Import the OpenCV library.


Step 2: Create a video capture object (cap) to open and read the video file
"video.mp4."
Step 3: Create a background subtractor object using the MOG2 method. The
cv2.createBackgroundSubtractorMOG2() function creates an instance of a
background subtractor, which is used to distinguish moving objects from
the background.
Step 4: Start an infinite loop (while True) to process each frame of the video.Read
the next frame from the video capture object using cap.read(). The frame
is stored in the frame variable, and ret is a boolean indicating whether the
frame was successfully read.
Step 5: Apply the background subtraction to the current frame using
fgbg.apply(frame). This creates a binary foreground mask (fg_mask)
where moving objects are detected against the background.
Step 6: Perform morphological operations to clean up the binary mask. First, erode
the mask to remove small noise or gaps in the foreground regions
usingcv2.erode, and then dilate the mask to connect or expand the
foreground regions using cv2.dilate.
Step 7: Find contours in the binary mask using cv2.findContours. The
cv2.RETR_EXTERNAL flag retrieves only the extreme outer contours,
and cv2.CHAIN_APPROX_SIMPLE simplifies the contour by removing
redundant points.
Step 8: Loop through the detected contours.Check if the area of the contour is
larger than a threshold (in this case, 500). This threshold is used to filter
out small noise or artifacts. If the area is above the threshold, it is
considered a significant object.
Step 9: Get the bounding rectangle (x, y, w, h) that encloses the detected object
using cv2.boundingRect.
Step 10: Draw a green rectangle around the detected object on the original frame
using cv2.rectangle.
Step 11: Display the frame with rectangles drawn around detected objects using
cv2.imshow.
Step 12: Check for a key press using cv2.waitKey(30). If the key pressed is the 'Esc'
key (ASCII code 27), break out of the loop to exit the program.
Step 13: Release the video capture object using cap.release() to free up system
resources.
Step 14: Close all OpenCV windows using cv2.destroyAllWindows().

PROGRAM:

import cv2

# Initialize video capture object

cap = cv2.VideoCapture('E:\people.mp4')

# Initialize background subtractor

fgbg = cv2.createBackgroundSubtractorMOG2()

while True:

ret, frame = cap.read()

if not ret:

break
# Apply background subtraction

fgmask = fgbg.apply(frame)

# Apply morphological operations to remove noise

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)

# Find contours

contours, _ = cv2.findContours(fgmask, cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:

# Filter out small contours

if cv2.contourArea(contour) > 500:

# Draw bounding box around the contour

x, y, w, h = cv2.boundingRect(contour)

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

# Display the resulting frame

cv2.imshow('Frame', frame)

cv2.imshow('Foreground Mask', fgmask)

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

break
# Release video capture object and close windows

cap.release()

cv2.destroyAllWindows()

OUTPUT:
RESULT:

Thus, the python program for event detection in video surveillance system was
written and the output was verified successfully.
EX.NO:

DATE

MINI PROJECT

GAMMA CORRECTION

AIM:

To write a python program for the Gamma correction in the given image.

ALGORITHM:

Step 1: Import the necessary libraries such as cv2 for the computer vision and
numpy for the numerical operations.
Step 2: Set the path to the input image as the image_path.
Step 3: Define a function adjust_gamma to perform the gamma correction to the
input image.
Step 4: Read the input image using the cv2 and set the gamma value for the
correction.
Step 5: Apply the gamma correction to the input image and display the original
image and the gamma corrected image using the opencv.
Step 6: Wait for the key and close all the opencv windows.

PROGRAM:

import cv2

import numpy as np

image_path = "E:\lady.jpg"

def adjust_gamma(image, gamma):


lookup_table = np.array([((i / 255.0) ** gamma) * 255 for i in np.arange(0,
256)]).astype(np.uint8)

gamma_corrected = cv2.LUT(image, lookup_table)

return gamma_corrected

image = cv2.imread(image_path)

gamma_value = 0.6

gamma_corrected = adjust_gamma(image, gamma_value)

cv2.imshow("Original", image)

cv2.imshow("Gamma Corrected", gamma_corrected)

cv2.waitKey(0)

cv2.destroyAllWindows()

OUTPUT:
RESULT:
Thus, the python program for mini project was written and the output was verified
successfully.

You might also like