Project 8
gdrive link of processed video:- https://drive.google.com/file/d/13_2FIhe-Mv3-NRMiC6cErg5tZ5F50n2M/view?usp=sharing

fig.1:- mask image output

fig.2.:- frame image output
CODE:-
# firstly we import cv2 library to optimising opencv features through this notebook
import cv2
# importing numpy library as the shortform of "np", numpy is working tool library to perform our multidimensional arrays
import numpy as np
def find_features(gray_image):
#goodFeaturesToTrack is used to identifying corners through shi-tomasi corner detector method
points = cv2.goodFeaturesToTrack(gray_image, mask = None, maxCorners = 100, qualityLevel = 0.1, minDistance = 10, blockSize = 5)
return points
def LK_model(gray_t_1, gray_t,prev_points):
# lucas kanade sparse optical flow model
points, status, error = cv2.calcOpticalFlowPyrLK(gray_t_1, gray_t, prev_points, None, winSize = (10,10), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
return (points, status, error)
capture = cv2.VideoCapture('video.mp4')
if not capture.isOpened():
print("Input video is not found or is not opened")
ret, prev_frame = capture.read()
if ret is False:
print("Video file is not capturing frame")
frame_width = int(capture.get(3))
frame_height = int(capture.get(4))
frame_size = (frame_width,frame_height)
fps = 20
output = cv2.VideoWriter('LK_optical_flow_output.avi', cv2.VideoWriter_fourcc('M','J','P','G'),fps,frame_size)
color = np.random.randint(0,255,(100,3))
ret, prev_frame = capture.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
prev_points = find_features(prev_gray)
mask_image = np.zeros_like(prev_frame)
ret,frame = capture.read()
while(frame is not None):
current_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
current_points, status, error = LK_model(prev_gray, current_gray, prev_points)
if current_points is not None:
current_points = current_points[status==1]
old_points = prev_points[status==1]
for i, (current, prev) in enumerate(zip(current_points, old_points)):
a,b = current.ravel()
c,d = prev.ravel()
mask_image = cv2.line(mask_image, (int(a), int(b)), (int(c), int(d)), color[i].tolist(),2)
frame = cv2.circle(frame,(int(a), int(b)), 5, color[i].tolist(), -1)
img = frame
output.write(img)
cv2.imshow('frame', img)
cv2.imshow('mask image', mask_image)
k = cv2.waitKey(5)
if k== 27:
break
prev_gray = current_gray.copy()
prev_points = current_points.reshape(-1,1,2)
ret, frame = capture.read()
cv2.destroyAllWindows()
capture.release()
output.release()