-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo_segmentation.py
57 lines (41 loc) · 1.39 KB
/
video_segmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# importing the necessary libraries
import cv2
import numpy as np
import datetime
import matplotlib.pyplot as plt
arr = []
arrx = []
arry = []
#################### Setting up the file ################
videoFile = "l1.mp4"
vidcap = cv2.VideoCapture(videoFile)
frame_counter = 0
success_prev, image_prev = vidcap.read()
frame_counter += 1
success_next, image_next = vidcap.read()
frame_counter += 1
#################### Setting up parameters ################
frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = vidcap.get(cv2.CAP_PROP_FPS)
#################### Initiate Process ################
while (success_prev & success_next):
dest_xor = cv2.bitwise_xor(image_prev, image_next, mask = None)
number_of_total_pix = np.sum(dest_xor >= 0)
number_of_changed_pix = np.sum(dest_xor > 0)
if (number_of_changed_pix/number_of_total_pix > 0.57): # random threshold
arr.append(frame_counter * (1/fps))
arrx.append(frame_counter * (1/fps))
arry.append(number_of_changed_pix/number_of_total_pix)
success_prev, image_prev = vidcap.read()
frame_counter += 1
success_next, image_next = vidcap.read()
frame_counter += 1
vidcap.release()
print("Complete")
plt.plot(arrx, arry)
plt.xlabel('x: seconds in video')
plt.ylabel('y: percent of pixels changed')
plt.title('Slide Change Detection')
plt.show()
# Closes all the windows currently opened.
cv2.destroyAllWindows()