-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoReader.py
89 lines (67 loc) · 2.04 KB
/
videoReader.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
import numpy as np
import cv2
import threading
vid = None
mask_indices = []
data = None
# At which percentage intervals does the program output its percent completion
PERCENT_NOTIFICATION = 10
_notif = PERCENT_NOTIFICATION / 100
def processFrame(fr):
dataList = []
tube = 0
# for every tube (mask) that exists...
for indices in mask_indices:
fr = fr[indices]
try:
fr = np.reshape(fr, (-1, 3))
except:
print(fr.shape)
print('indicies', indices[0].shape)
pass
#print(fr.shape)
dataList.append(fr)
tube += 1
return dataList
def setup(file, msk):
global vid
global mask_indices
# saves and resets the global variables to begin processing
vid = cv2.VideoCapture(file)
vid.set(cv2.CAP_PROP_POS_FRAMES, 0)
indices = np.nonzero(msk)
mask_indices = []
mask_indices.append(indices)
def parse(name):
global data
data = None
# count the total frames to give completion estimates
total = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
invTotal = 1 / total
vid.set(cv2.CAP_PROP_POS_FRAMES, 0)
success, frame = vid.read()
data = []
while success:
# append the da
# ta from the next frame to the data
# data.append(processFrame(frame))
fr = frame.copy()
parseThread = threading.Thread(target=(lambda f, d: (d.append(processFrame(f)))), args=(fr, data))
parseThread.start()
if ((len(data) / total) % _notif) < invTotal:
print(int((len(data) * 100) / total), "% done!")
success, frame = vid.read()
parseThread.join()
vid.release()
print("100% done!")
data = np.asarray(data)
data = np.moveaxis(data, 0, 1)
print("video data successfully parsed and collected!")
np.save(name+".npy", data)
return data
if __name__ == "__main__":
vidfile = "data/def.mov"
import boxSelector
msks = boxSelector.selectTubes(vidfile)
setup(vidfile, msks)
parse()