IVA RECORD DK

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 64

UNIVERSITY COLLEGE OF ENGINEERING TINDIVANAM

(A Constituent College of Anna University, Chennai)


TINDIVANAM– 604 001

DEPARTMENT OF INFORMATION TECHNOLOGY


BONAFIDE CERTIFICATE
This is to certify to be the BONAFIDE RECORD work done by

Mr/Ms. Register Number:

Of V Semester B. Tech IT in the CCS349 Image And Video Analytics Laboratory

Practical Course during the Academic year 2023– 2024.

Date:

Staff in-charge Head of the Department

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

Internal Examiner External Examiner


S.NO DATE EXPERIMENTS PAGE.NO SIGNATURE
EXP NO :1

DATE:

WRITE A PROGRAM THAT COMPUTES THE T-PYRAMID OF AN


IMAGE

AIM:

To write a python program to computes 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 the 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 matplotlib.pyplot as plt

img = cv2.imread("nature.jpg")
layer = img.copy()

for i in range(4):

plt.subplot(2, 2, i + 1)

# using pyrDown() function

layer = cv2.pyrDown(layer)

plt.imshow(layer)

cv2.imshow("str(i)", layer)

cv2.waitKey(0)

cv2.destroyAllWindows()

OUTPUT:
RESULT:
EXP NO :2

DATE:

WRITE A PROGRAM THAT DERIVES THE QUAD TREE


REPRESENTATION OF AN IMAGE USING THE
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 visualisation, 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 the 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

import matplotlib.image as mpimg

import cv2 as cv

import numpy as np

img =cv.imread('sample2.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.south_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:
EXP.NO:3(A)

DATE:

DEVELOP PROGRAMS FOR THE FOLLOWING GEOMETRIC


TRANSFORMS

ROTATION

AIM:

To write a python program for the geometric transforms in 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
for a 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 cv2
import numpy as np
img = cv2.imread('sample2.jpg')
rows, cols, ch = img.shape

matrix_rotated = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.6)


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

cv2.imshow("image",img)
cv2.imshow("Rotated image",rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
RESULT:
EXP.NO:3(B)

DATE:

CHANGE OF SCALE

AIM:

To write a python program for the geometric transforms in Changes 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 cv2

import numpy as np

img = cv2.imread('sample2.jpg')
scaled_img = cv2.resize(img, None, fx=2.2, fy=2.5)

cv2.imshow("Scaled image",scaled_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

OUTPUT:

RESULT:
EXP.NO:3(C)

DATE:

SKEWING

AIM:

To write a python program for the geometric transforms in


Skewing.

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: Retrive the number of rows , columns , and channels from the loaded
image using 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 transformed image with
the window name My Zen Garden.
Step 6: Use cv2.imwrite() to save the transformed image as the current directory.

PROGRAM:

import cv2

import numpy as np

img = cv2.imread('Nature1.jpg')

rows, cols, ch = img.shape

pts1 = np.float32(
[[cols*.25, rows*.95],

[cols*.90, rows*.95],

[cols*.10, 0],

[cols, 0]]

pts2 = np.float32(

[[cols*0.1, rows],

[cols, rows],

[0, 0],

[cols, 0]]

M = cv2.getPerspectiveTransform(pts1,pts2)

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

cv2.imshow('My Zen Garden', dst)

cv2.imwrite('apple.jpg', dst)

cv2.waitKey()
OUTPUT:

RESULT:
EXP.NO:3(D)

DATE:

AFFINE TRANSFORM CALCULATED FROM THREE PAIRS OF


CORRESPONDING 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 cv2

import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('apple.jpg')

rows, cols, ch = img.shape

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

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

M = cv2.getAffineTransform(pts1, pts2)

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

plt.subplot(121)

plt.imshow(img)

plt.title('Input')

plt.subplot(122)

plt.imshow(dst)

plt.title('Output')

plt.show()

cv2.destroyAllWindows()
OUTPUT:

RESULT:
EXP.NO:3(E)

DATE:

BILINEAR TRANSFORM CALCULATED FROM FOUR PAIRS OF


CORRESPONDING POINTS

AIM:

To write a python program for the geometric transforms 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 the 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 cv2

import numpy as np

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

image = cv2.imread('apple.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:
EXP.NO:4

DATE:

DEVELOP A PROGRAM TO IMPLEMENT 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 cv2

from matplotlib import pyplot as plt

img= cv2.imread("cat.jpg")

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

rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

data = cv2.CascadeClassifier('cascade_cat.xml')

found = data.detectMultiScale(gray,minSize =(20, 20))

result = len(found)

if result != 0:

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

cv2.rectangle(rgb, (x, y),

(x + height, y + width),

(0, 255, 0), 5)


else:

print("not found")

plt.imshow(rgb)

plt.show()

OUTPUT:

RESULT:
EXP.NO:5

DATE:

DEVELOP A PROGRAM FOR MOTION ANALYSIS USING MOVING


EDGES, AND APPLY IT TO YOUR IMAGE SEQUENCES

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 as cv

import numpy as np

def motion_analysis(img_seq):

gray_seq = [cv.cvtColor(image, cv.COLOR_BGR2GRAY) for i in img_seq]

motion_vect = []

prev_frame = gray_seq[0]
prev_pts = cv.goodFeaturesToTrack(prev_frame, maxCorners=50,
qualityLevel=0.01, minDistance=10)

for frame in gray_seq[1:]:

next_pts, status ,_= cv.calcOpticalFlowPyrLK(prev_frame, frame,


prev_pts, None)

status = status.ravel()

valid_pts = prev_pts[status == 1]

next_pts = next_pts[status == 1]

motion_vect.append(len(next_pts))

prev_pts = next_pts

prev_frame = frame

return motion_vect

img_seq = [cv.imread(f"frame ({i}).jpg") for i in range(1,10)]

motion_vect = motion_analysis(img_seq)

for i in range(len(motion_vect)):

print(f"Frame {i+1}: {motion_vect[i]} moving vectors")


OUTPUT:

RESULT:
EXP.NO:6

DATE:

DEVELOP A PROGRAM FOR FACIAL DETECTION AND


RECOGNITION

AIM:

To write a python program to develop a program for facial detection and


recognition.

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:

from deepface import DeepFace

import cv2 as cv

import matplotlib.pyplot as plt

img = cv.imread("m1.jpg")

res = DeepFace.analyze(img, actions=['age', 'gender'])

gender = res[0]['gender']

age = res[0]['age']

face_axis = res[0]['region']

x, y, width, height = face_axis['x'], face_axis['y'], face_axis['w'], face_axis['h']

cv.rectangle(img, (x, y), (x + width, y + height), (0, 255, 0), 2)

plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))

plt.title(f"gender :{gender} , age: {age}")


plt.show()

OUTPUT:

RESULT:
EXP.NO:7

DATE:

WRITE A PROGRAM FOR 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 using
cv2.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

cap = cv2.VideoCapture('video.mp4')

fgbg = cv2.createBackgroundSubtractorMOG2()

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

fg_mask = fgbg.apply(frame)

fg_mask = cv2.erode(fg_mask, None, iterations=2)

fg_mask = cv2.dilate(fg_mask, None, iterations=2)

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


cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:

if cv2.contourArea(contour) > 500:

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

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

cv2.imshow('Event', frame)

if cv2.waitKey(30) & 0xFF == 27:

break

cap.release()

cv2.destroyAllWindows()
OUTPUT:

RESULT:
PROJECT.NO:

DATE

MINI PROJECT

GAMMA

CORRECTION

AIM:

To write a python program for the Gamma corrections 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 = "main3.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:
PROJECT.NO:

DATE:

INTEL IMAGE CLASSIFICATION

AIM:

To write a python program for the intel detection of the image in the
given dataset.

ALGORITHM:

Step 1: Import the necessary libraries such as tensorflow, matplotlib , keras ,


numpy , seaborn and cv2 .
Step 2: Define the image categories and the image paths such as the train data ,
test data and validation path .
Step 3: Define a function to plot the sample image from the each category and
generate the image data generators for testing , training and validation.
Step 4: Build the convolution neural network model to train the model and plot
the training history and save the training model .
Step 5: Load the saved model and create a class map and define a function to
make predictions.
Step 6: And atlast it will makes the sample of the predictions .

PROGRAM:

import tensorflow as tf

import os

from keras.preprocessing.image import ImageDataGenerator

from keras.preprocessing import image

import matplotlib.pyplot as plt


from PIL import Image

from keras.models import *

from keras.layers import *

import numpy as np

import seaborn as sns

from tensorflow.keras.preprocessing.image import load_img

import keras.utils as image

from keras.utils import load_img, img_to_array

import cv2 as cv

img_cat =os.listdir("data/seg_train/seg_train")

train_path = "data/seg_train/seg_train"

test_path = "data/seg_test/seg_test"

val_path = "data/seg_pred/seg_pred"

def plot (img_cat):

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

for i, category in enumerate(img_cat):

img_path = train_path + '/' + category

img_folder = os.listdir(img_path)

f_img = img_folder[0]

f_img_path = img_path+'/'+ f_img

img= image.load_img(f_img_path)
img_arr = image.img_to_array(img) /225

plt.subplot(2,3,i+1)

plt.imshow(img_arr)

plt.title(category)

plt.show()

plot(img_cat)

train_generator = ImageDataGenerator(rescale = 1.0/255.0)

train_image_generator = train_generator.flow_from_directory(

train_path,

target_size = (150,150),

batch_size = 32,

class_mode = 'categorical'

test_generator = ImageDataGenerator(rescale = 1.0/255.0)

test_image_generator = test_generator.flow_from_directory(

test_path,

target_size = (150,150),

batch_size = 32,

class_mode = 'categorical'

val_generator = ImageDataGenerator(rescale = 1.0/255.0)

val_image_generator = val_generator.flow_from_directory(
val_path,

target_size = (150,150),

batch_size = 32,

class_mode = 'categorical'

model = Sequential()

#add layers

model.add(Conv2D(filters=32, activation='ReLU',
padding='same',kernel_size=3, input_shape =[150,150,3] ))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(filters=64, activation='ReLU',
padding='same',kernel_size=3 ))

model.add(MaxPooling2D())

#flattening_layers

model.add(Flatten())

#fully_connected

model.add(Dense(128,

activation='ReLU')) model.add(Dense(64,

activation='ReLU')) #output layers

model.add(Dense(6, activation = 'softmax'))

model.summary()
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics =
'accuracy')

his = model.fit(train_image_generator ,

epochs=12,

validation_data= test_image_generator,

#train acc in loss

x=his

plt.plot(x.history['loss'],label='train loss')

plt.plot(x.history['val_loss'],label='valid loss')

plt.legend()

plt.show()

#graph acc in train

plt.plot(x.history['accuracy'],label='train acc')

plt.plot(x.history['val_accuracy'],label='valid acc')

plt.legend()

plt.show()

model.save('intel.h5')

class_map = dict([v,k] for k,v in train_image_generator.class_indices.items())

print(class_map)

from keras.models import load_model

model = load_model("intel.h5")
import numpy as np

import matplotlib.pyplot as plt

from keras.preprocessing import image

def predict(s):

im = image.load_img(s, target_size=(150, 150))

im_arr = image.img_to_array(im) / 255.0

im_input = im_arr.reshape((1, im_arr.shape[0], im_arr.shape[1],


im_arr.shape[2]))

label = np.argmax(model.predict(im_input))

pred_img = class_map[label]

plt.figure(figsize=(4, 4))

plt.imshow(im_arr)

print(pred_img)

plt.show()

predict('1.jpg')

OUTPUT:

Found 14034 images belonging to 6 classes.

Found 3000 images belonging to 6 classes.

Found 0 images belonging to 0 classes.

{0: 'buildings', 1: 'forest', 2: 'glacier', 3: 'mountain', 4: 'sea', 5: 'street'}


RESULT:
PROJECT.NO:

DATE:

FACE ATTENDENCE MANAGEMENT

AIM:

To write a python program for the face attendance management using the

dataset.

ALGORITHM:

Step 1: Import the necessary libraries such as cv2 , face_recognition , os , numpy


and pickle for the below program.
Step 2: Load the image and the class names for the mylist .
Step 3: Encoded the faces in the given data set which can be taken by us and
mark the attendance function for the dataset.
Step 4: And now open the webcam and perform the face recognition
Step 5: And then the face attendance will be added to the csv file which has been
created by us.
Step 6: And the attendance will be marked by name , date and time .

PROGRAM:

import cv2

import face_recognition

import os

import numpy as np

from datetime import datetime

import pickle

path = 'Students'
images = []

classNames = []

mylist = os.listdir(path)

for cl in mylist:

curImg = cv2.imread(f'{path}/{cl}')

images.append(curImg)

classNames.append(os.path.splitext(cl)[0])

def findEncodings(images):

encodeList = []

for img in images:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

encoded_face = face_recognition.face_encodings(img)[0]

encodeList.append(encoded_face)

return encodeList

encoded_face_train = findEncodings(images)

def markAttendance(name):

with open('Attendance.csv','r+') as f:

myDataList = f.readlines()

nameList = []

for line in myDataList:

entry = line.split(',')

nameList.append(entry[0])
if name not in nameList:

now = datetime.now()

time = now.strftime('%I:%M:%S:%p')

date = now.strftime('%d-%B-%Y')

f.writelines(f'n{name}, {time}, {date}')

import cv2

import face_recognition

import numpy as np

cap = cv2.VideoCapture(0)

while True:

success, img = cap.read()

imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)

imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

faces_in_frame = face_recognition.face_locations(imgS)

encoded_faces = face_recognition.face_encodings(imgS, faces_in_frame)

for encode_face, faceloc in zip(encoded_faces, faces_in_frame):

matches = face_recognition.compare_faces(encoded_face_train,
encode_face)
faceDist = face_recognition.face_distance(encoded_face_train,
encode_face)

matchIndex = np.argmin(faceDist)

print(matchIndex)

if matches[matchIndex]:

name = classNames[matchIndex].upper().lower()

y1, x2, y2, x1 = faceloc

# since we scaled down by 4 times

y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4

cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)

cv2.putText(img, name, (x1 + 6, y2 - 5),


cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)

markAttendance(name)

cv2.imshow('webcam', img)

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

break

cap.release()

cv2.destroyAllWindows()
OUTPUT:

RESULT:
PROJECT.NO:

DATE:

VIDEO SURVEILLANCE

AIM:

To write a python program for the video surveillance to detect the


vehicles counting.

ALGORITHM:

Step 1: Import the necessary libraries such as cv2 , time and numpy for the given
program.
Step 2: Create a color constants for drawing the rectangles and the lines and the
font type for the text annotations and fps for the frames per second
preprocessing .
Step 3: Calculates the center position of a bounding box given its coordinates (x,
y) and dimensions (w, h).
Step 4: Performs vehicle detection using background subtraction.
Step 5: Create a background subtractor and a morphological kernel.Initialize
variables for storing detected vehicles and the count of detected
cars.Enter a loop that reads frames from the video capture.Apply
background subtraction and morphological operations to the frame.
Step 6: Find contours in the processed frame.Draw a detection line on the
frame.For each valid contour, draw a rectangle and calculate the center of
the bounding box.If a vehicle crosses the detection line, update the count.
Step 7: Open a video file named 'video.mp4'.Call the bg_sub function with the
parameters 'yep' (to show the background subtraction result) and the
video capture object.
PROGRAM:

import cv2

import time

import numpy as np

RED = (0, 0, 255)

GREEN = (0, 255, 0)

BLUE = (176, 130, 39)

ORANGE = (0, 127, 255)

FONT = cv2.FONT_HERSHEY_COMPLEX

offset = 6

fps = 60

min_width = 80

min_height = 80

linePos = 550

ground_truth1 = 62

ground_truth2 = 36

def center_position(x, y, w, h):

center_x = x + (w // 2)

center_y = y + (h // 2)

return center_x, center_y

def bg_sub(show_detect, CAP):


subtract = cv2.createBackgroundSubtractorMOG2()

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

detect_vehicle = []

counts = 0

while CAP.isOpened():

duration = 1 / fps

time.sleep(duration)

ret, frame = CAP.read()

if frame is None:

break

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

blur = cv2.GaussianBlur(gray, (3, 3), 5)

img_sub = subtract.apply(blur)

dilation = cv2.dilate(img_sub, np.ones((5, 5)))

opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel)

contours = cv2.findContours(

opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]

cv2.line(frame, (25, linePos), (1200, linePos), BLUE, 2)

for contour in contours:

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

valid_contour = (w >= min_width) and (h >= min_height)

if not valid_contour:
continue

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

center_vehicle = center_position(x, y, w, h)

detect_vehicle.append(center_vehicle)

cv2.circle(frame, center_vehicle, 4, RED, -1)

for x, y in detect_vehicle:

if y < linePos + offset and y > linePos - offset:

cv2.line(frame, (25, linePos), (1200, linePos), ORANGE, 3)

detect_vehicle.remove((x, y))

counts += 1

cv2.putText(

frame, f"Car Detected: {counts}", (50, 70), FONT, 2, RED, 3,


cv2.LINE_AA)

cv2.imshow('Detect', frame)

if show_detect.startswith('y'):

cv2.imshow('Detector', opening)

if cv2.waitKey(1) == 27:

break

cv2.destroyAllWindows()

CAP.release()
return counts

vd = cv2.VideoCapture('video.mp4')

bg_sub("yep", vd)

OUTPUT:
RESULT:

You might also like