-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.py
104 lines (70 loc) · 2.65 KB
/
getData.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'''
This script will get data from cloud sources or process images and videos
into a standard format
'''
from os import error
import cv2
import numpy as np
from glob import glob
from skimage.registration import phase_cross_correlation as pcc
from cocoDataStructure.utilities import dirMaker, printProgressBar
def sigFrame(err, errStore, minFrameDist):
'''
Determine if an error value is a statistically signficant change
'''
if len(errStore) < minFrameDist:
return(False)
diff = np.diff(errStore)
std = np.std(diff)
me = np.mean(diff)
errDiff = err - errStore[-1]
if errDiff > me + std * 2 or errDiff < me - std * 2:
return(True)
else:
return(False)
def extractFrames(vidPath, dest = None, minFrameDist = 10):
'''
From a video, extract frames which are sufficiently different
'''
vidName = vidPath.split("/")[-1]
cap = cv2.VideoCapture(vidPath)
if (cap.isOpened()== False):
print("Error opening video stream or file")
return
if dest is None:
dest = f'{"/".join(vidPath.split("/")[:-1])}/extract/'
dirMaker(dest)
x = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
y = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
frame0 = (np.random.random([300,300]) * 255).astype(np.uint8)
n = 0
errStore = []
# save frames which are significantly different from other frames
while(cap.isOpened()):
printProgressBar(n, frames, "Frames processed", length=20)
ret, frame = cap.read()
# downsample and convert to grayscale to speed up the correlation comparsions
frame1 = cv2.blur(cv2.resize(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (300, 300)), (5, 5))
if ret == True:
shift, err, phasedif = pcc(frame0, frame1, upsample_factor=0.2)
sig = sigFrame(err, errStore, minFrameDist)
# if there is a signficant difference in frames re-assign the
# frame store
if (sig or err > 0.2) and len(errStore) > minFrameDist: # (err > 0.2 or np.isnan(err)) and np.sum(frame)/frame.size > 20:
cv2.imwrite(f"{dest}{vidName}_{n}.jpg", frame)
errStore = []
# cv2.imshow("imgs", np.hstack([frame0, frame1])); cv2.waitKey(0)
frame0 = frame1
else:
errStore.append(err)
else:
break
n += 1
cap.release()
if __name__ == "__main__":
src = "/media/boxfish/USB/KINGSTON/testVids/"
videos = sorted(glob(src + "*.mp4"))
for v in videos:
extractFrames(v)