-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
129 lines (106 loc) · 4.95 KB
/
preprocessing.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from natsort import natsorted
import cv2
def create_segments_and_labels (paths, args):
segments = {'train': [], 'val': [] }
labels = {'train': [], 'val': [] }
old_root = ""
train_val = ['train', 'val']
events = ['Fight', 'NonFight']
for tv in train_val:
for event_type in events:
label = 1 if (event_type == 'Fight') else 0
for root, dirs, files in os.walk(paths.jpg_frames+"/"+tv+"/"+event_type):
new_root = root
for file_name in natsorted(files):
path_to_image = os.path.join(root, file_name)
# add to the list which should has length args.clip_frames
if (old_root == new_root):
new_segment.append(path_to_image)
if (len(new_segment) == args.clip_frames):
segments[tv].append(new_segment)
labels[tv].append(label)
new_segment = []
# add the a new list a new images
else:
new_segment = []
new_segment.append(path_to_image)
old_root = new_root
return segments, labels
def create_window_segments_and_labels (paths, args):
segments = {'train': [], 'val': [] }
labels = {'train': [], 'val': [] }
old_root = ""
train_val = ['train', 'val']
events = ['Fight', 'NonFight']
window_shift = 0
for tv in train_val:
for event_type in events:
label = 1 if (event_type == 'Fight') else 0
# Loop over all the folders with videos frames
for root, dirs, files in os.walk(paths.jpg_frames+"/"+tv+"/"+event_type):
new_root = root
# Iterate among the frames of that video
idx = 0
files = natsorted(files)
while (idx < len(files)):
if (idx % args.interval == 0):
file_name = files[idx]
path_to_image = os.path.join(root, file_name)
# add to the list which should has length args.clip_frames
# if we are still inside the same video...
if (old_root == new_root):
new_segment.append(path_to_image)
# If the length of the segment is over, just append it to the bigger list
if (len(new_segment) == args.clip_frames):
segments[tv].append(new_segment)
labels[tv].append(label)
new_segment = []
idx = idx - window_shift
# Otherwise, generate a new_segment and that frame append to it
else:
new_segment = []
new_segment.append(path_to_image)
old_root = new_root
idx += 1
return segments, labels
def create_window_segments_and_labels_UCFCrime (paths, args):
segments = {'train': [], 'val': [] }
old_root = ""
train_val = ['train', 'val']
window_shift = 0
for tv in train_val:
# Loop over all the folders with videos frames
for root, dirs, files in os.walk(paths.jpg_frames+"/"+tv):
new_root = root
# Iterate among the frames of that video
idx = 0
files = natsorted(files)
while (idx < len(files)):
if (idx % args.interval == 0):
file_name = files[idx]
path_to_image = os.path.join(root, file_name)
# add to the list which should has length args.clip_frames
# if we are still inside the same video...
if (old_root == new_root):
new_segment.append(path_to_image)
# If the length of the segment is over, just append it to the bigger list
if (len(new_segment) == args.clip_frames):
segments[tv].append(new_segment)
new_segment = []
idx = idx - window_shift
# Otherwise, generate a new_segment and that frame append to it
else:
new_segment = []
new_segment.append(path_to_image)
old_root = new_root
idx += 1
return segments
def generate_flow (prev, next):
# Convert to gray scale
prvs = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
# Capture another frame and convert to gray scale
next = cv2.cvtColor(next, cv2.COLOR_BGR2GRAY)
# Optical flow is now calculated
flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
return flow