-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyFrameExtract.py
More file actions
57 lines (47 loc) · 1.67 KB
/
KeyFrameExtract.py
File metadata and controls
57 lines (47 loc) · 1.67 KB
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
import cv2
import numpy as np
import os
import glob
import shutil
if not os.path.exists('KeyFrames'):
os.makedirs('KeyFrames')
localCounter = 0
for name in glob.glob("/home/aman/Desktop/Mini-Project/FrameFolder*"):
print("Processing in KeyFrameExtract.py- Processing for FrameFolder: ", name)
numOfFrames = len([f for f in os.listdir(name)])
sumOfDiffArray = [0]*numOfFrames
count = 0
mean = 0
deviation = 0
i = 0;
while i < numOfFrames-2:
FirstImage = name+"/frame"+str(i)+'.jpg'
im1 = cv2.imread(FirstImage, cv2.IMREAD_COLOR)
im1 = cv2.cvtColor(im1, cv2.COLOR_RGB2GRAY)
SecondImage=name+"/frame"+str(i+1)+'.jpg'
im2 = cv2.imread(SecondImage, cv2.IMREAD_COLOR)
im2 = cv2.cvtColor(im2, cv2.COLOR_RGB2GRAY)
FirstArray = np.array(im1).astype(np.float32)
SecondArray = np.array(im2).astype(np.float32)
Difference = abs(FirstArray - SecondArray)
Sum = np.sum(Difference)
sumOfDiffArray[i] = Sum
i = i + 1
mean = np.mean(sumOfDiffArray)
deviation = np.std(sumOfDiffArray)
if numOfFrames < 20:
constant = 0.1
else:
constant = 1.5
th = constant*mean + deviation
i = 0
while i < numOfFrames-1:
l = int(sumOfDiffArray[i])
if float(l) > th:
path = name + "/frame" + str(i + 1) + '.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
cv2.imwrite("/home/aman/Desktop/Mini-Project/KeyFrames/frame{0}.jpg".format(localCounter), img)
localCounter += 1
i = i + 1
shutil.rmtree(name)
print("Processing in FrameExtract.py- Total Number of KeyFrames obtained: ", localCounter)