IVA RECORD DK
IVA RECORD DK
IVA RECORD DK
Date:
DATE:
AIM:
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
img = cv2.imread("nature.jpg")
layer = img.copy()
for i in range(4):
plt.subplot(2, 2, i + 1)
layer = cv2.pyrDown(layer)
plt.imshow(layer)
cv2.imshow("str(i)", layer)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
RESULT:
EXP NO :2
DATE:
AIM:
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 cv2 as cv
import numpy as np
img =cv.imread('sample2.jpg')
plt.imshow(img)
def split4(image):
half_split = np.array_split(image, 2)
split_img = split4(img)
split_img[0].shape
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])
plt.imshow(full_img)
plt.show()
def calculate_mean(img):
print(means)
plt.imshow(means)
plt.show()
def checkEqual(myList):
first=myList[0]
class QuadTree:
self.level = level
self.mean = calculate_mean(img).astype(int)
self.final = True
if not checkEqual(img):
split_img = split4(img)
self.final = False
return self
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:
ROTATION
AIM:
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
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:
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:
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')
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)
cv2.imwrite('apple.jpg', dst)
cv2.waitKey()
OUTPUT:
RESULT:
EXP.NO:3(D)
DATE:
AIM:
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')
M = cv2.getAffineTransform(pts1, pts2)
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:
AIM:
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
image = cv2.imread('apple.jpg')
x2, y2 = x1 + 1, y1 + 1
dx = x - x1
dy = y - y1
# Get the pixel values at the four surrounding
tr = image[y1, x2]
bl = image[y2, x1]
br = image[y2, x2]
return result
x, y = point
interpolated_value = bilinear_interpolation(image, x, y)
x, y = point
cv2.circle(image, (x, y), 5, (0, 0, 255), -1) # Draw a red circle at each point
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
RESULT:
EXP.NO:4
DATE:
AIM:
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
img= cv2.imread("cat.jpg")
data = cv2.CascadeClassifier('cascade_cat.xml')
result = len(found)
if result != 0:
(x + height, y + width),
print("not found")
plt.imshow(rgb)
plt.show()
OUTPUT:
RESULT:
EXP.NO:5
DATE:
AIM:
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):
motion_vect = []
prev_frame = gray_seq[0]
prev_pts = cv.goodFeaturesToTrack(prev_frame, maxCorners=50,
qualityLevel=0.01, minDistance=10)
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
motion_vect = motion_analysis(img_seq)
for i in range(len(motion_vect)):
RESULT:
EXP.NO:6
DATE:
AIM:
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 as cv
img = cv.imread("m1.jpg")
gender = res[0]['gender']
age = res[0]['age']
face_axis = res[0]['region']
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
OUTPUT:
RESULT:
EXP.NO:7
DATE:
AIM:
ALGORITHM:
PROGRAM:
import cv2
cap = cv2.VideoCapture('video.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cap.read()
fg_mask = fgbg.apply(frame)
x, y, w, h = cv2.boundingRect(contour)
cv2.imshow('Event', frame)
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"
return gamma_corrected
image = cv2.imread(image_path)
gamma_value = 0.6
cv2.imshow("Original", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
RESULT:
PROJECT.NO:
DATE:
AIM:
To write a python program for the intel detection of the image in the
given dataset.
ALGORITHM:
PROGRAM:
import tensorflow as tf
import os
import numpy as np
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"
plt.figure(figsize=(10,10))
img_folder = os.listdir(img_path)
f_img = img_folder[0]
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_image_generator = train_generator.flow_from_directory(
train_path,
target_size = (150,150),
batch_size = 32,
class_mode = 'categorical'
test_image_generator = test_generator.flow_from_directory(
test_path,
target_size = (150,150),
batch_size = 32,
class_mode = 'categorical'
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,
model.summary()
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics =
'accuracy')
his = model.fit(train_image_generator ,
epochs=12,
validation_data= test_image_generator,
x=his
plt.plot(x.history['loss'],label='train loss')
plt.plot(x.history['val_loss'],label='valid loss')
plt.legend()
plt.show()
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')
print(class_map)
model = load_model("intel.h5")
import numpy as np
def predict(s):
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:
DATE:
AIM:
To write a python program for the face attendance management using the
dataset.
ALGORITHM:
PROGRAM:
import cv2
import face_recognition
import os
import numpy as np
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 = []
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 = []
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')
import cv2
import face_recognition
import numpy as np
cap = cv2.VideoCapture(0)
while True:
faces_in_frame = face_recognition.face_locations(imgS)
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()
markAttendance(name)
cv2.imshow('webcam', img)
break
cap.release()
cv2.destroyAllWindows()
OUTPUT:
RESULT:
PROJECT.NO:
DATE:
VIDEO SURVEILLANCE
AIM:
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
FONT = cv2.FONT_HERSHEY_COMPLEX
offset = 6
fps = 60
min_width = 80
min_height = 80
linePos = 550
ground_truth1 = 62
ground_truth2 = 36
center_x = x + (w // 2)
center_y = y + (h // 2)
detect_vehicle = []
counts = 0
while CAP.isOpened():
duration = 1 / fps
time.sleep(duration)
if frame is None:
break
img_sub = subtract.apply(blur)
contours = cv2.findContours(
x, y, w, h = cv2.boundingRect(contour)
if not valid_contour:
continue
center_vehicle = center_position(x, y, w, h)
detect_vehicle.append(center_vehicle)
for x, y in detect_vehicle:
detect_vehicle.remove((x, y))
counts += 1
cv2.putText(
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: