-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader_multiple.py
199 lines (159 loc) · 9.58 KB
/
data_loader_multiple.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import torch
import json
from config.classes import EVENT_DICTIONARY
##############################################################
# #
# DO NOT MAKE ANY CHANGES HERE #
# #
##############################################################
def label2vectormerge_multiplesplits(folder_path, splits, num_views):
num_classes_action = 8
num_classes_offence_severity = 4
not_taking_allsplits = []
labels_action = []
labels_offence_severity= []
number_of_actions = []
total_distribution = torch.zeros(num_classes_offence_severity, num_classes_action)
distribution_action = torch.zeros(1, num_classes_action)
distribution_offence_severity = torch.zeros(1, num_classes_offence_severity)
for split in splits:
not_taking = []
path_annotations = os.path.join(folder_path, split)
path_annotations = os.path.join(path_annotations, "annotations.json")
dictionary_action = EVENT_DICTIONARY['action_class']
if os.path.exists(path_annotations):
with open(path_annotations) as f:
train_annotations_data = json.load(f)
else:
print("PATH DOES NOT EXISTS")
exit()
for actions in train_annotations_data['Actions']:
action_class = train_annotations_data['Actions'][actions]['Action class']
offence_class = train_annotations_data['Actions'][actions]['Offence']
severity_class = train_annotations_data['Actions'][actions]['Severity']
if action_class == '' or action_class == 'Dont know':
not_taking.append(actions)
continue
if (offence_class == '' or offence_class == 'Between') and action_class != 'Dive':
not_taking.append(actions)
continue
if (severity_class == '' or severity_class == '2.0' or severity_class == '4.0') and action_class != 'Dive' and offence_class != 'No offence' and offence_class != 'No Offence':
not_taking.append(actions)
continue
if offence_class == '' or offence_class == 'Between':
offence_class = 'Offence'
if severity_class == '' or severity_class == '2.0' or severity_class == '4.0':
severity_class = '1.0'
if num_views == 1:
for i in range(len(train_annotations_data['Actions'][actions]['Clips'])):
if offence_class == 'No Offence' or offence_class == 'No offence':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][0] = 1
distribution_offence_severity[0][0] += 1
off_index = 0
elif offence_class == 'Offence' and severity_class == '1.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][1] = 1
distribution_offence_severity[0][1] += 1
off_index = 1
elif offence_class == 'Offence' and severity_class == '3.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][2] = 1
distribution_offence_severity[0][2] += 1
off_index = 2
elif offence_class == 'Offence' and severity_class == '5.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][3] = 1
distribution_offence_severity[0][3] += 1
off_index = 3
else:
not_taking.append(actions)
continue
labels_action.append(torch.zeros(1, num_classes_action))
labels_action[len(labels_action)-1][0][dictionary_action[action_class]] = 1
distribution_action[0][dictionary_action[action_class]] += 1
total_distribution[off_index][dictionary_action[action_class]] += 1
else:
if offence_class == 'No Offence' or offence_class == 'No offence':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][0] = 1
distribution_offence_severity[0][0] += 1
index = 0
elif offence_class == 'Offence' and severity_class == '1.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][1] = 1
distribution_offence_severity[0][1] += 1
index = 1
elif offence_class == 'Offence' and severity_class == '3.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][2] = 1
distribution_offence_severity[0][2] += 1
index = 2
elif offence_class == 'Offence' and severity_class == '5.0':
labels_offence_severity.append(torch.zeros(1, num_classes_offence_severity))
labels_offence_severity[len(labels_offence_severity)-1][0][3] = 1
distribution_offence_severity[0][3] += 1
index = 3
else:
not_taking.append(actions)
continue
number_of_actions.append(actions)
labels_action.append(torch.zeros(1, num_classes_action))
labels_action[len(labels_action)-1][0][dictionary_action[action_class]] = 1
distribution_action[0][dictionary_action[action_class]] += 1
total_distribution[index][dictionary_action[action_class]] += 1
not_taking_allsplits.append(not_taking)
return labels_offence_severity, labels_action, distribution_offence_severity[0], distribution_action[0], not_taking_allsplits, number_of_actions
# Function to load the path to the clips
def clips2vectormerge_multiplesplits(folder_path, splits, num_views, not_taking_per_split):
clips = []
for split_count, split in enumerate(splits):
if(len(not_taking_per_split)>0):
not_taking = not_taking_per_split[split_count]
else:
not_taking = []
path_clips = os.path.join(folder_path, split)
if os.path.exists(path_clips):
folders = 0
for _, dirnames, _ in os.walk(path_clips):
folders += len(dirnames)
for i in range(folders):
if str(i) in not_taking:
continue
if num_views == 1:
path_clip = os.path.join(path_clips, "action_" + str(i))
path_clip_0 = os.path.join(path_clip, "clip_0.mp4")
clips_all_view = []
clips_all_view.append(path_clip_0)
clips.append(clips_all_view)
clips_all_view = []
path_clip_1 = os.path.join(path_clip, "clip_1.mp4")
clips_all_view.append(path_clip_1)
clips.append(clips_all_view)
clips_all_view = []
if os.path.exists(os.path.join(path_clip, "clip_2.mp4")):
path_clip_2 = os.path.join(path_clip, "clip_2.mp4")
clips_all_view.append(path_clip_2)
clips.append(clips_all_view)
clips_all_view = []
if os.path.exists(os.path.join(path_clip, "clip_3.mp4")):
path_clip_3 = os.path.join(path_clip, "clip_3.mp4")
clips_all_view.append(path_clip_3)
clips.append(clips_all_view)
clips_all_view = []
else:
path_clip = os.path.join(path_clips, "action_" + str(i))
clips_all_view = []
path_clip_0 = os.path.join(path_clip, "clip_0.mp4")
clips_all_view.append(path_clip_0)
path_clip_1 = os.path.join(path_clip, "clip_1.mp4")
clips_all_view.append(path_clip_1)
if os.path.exists(os.path.join(path_clip, "clip_2.mp4")):
path_clip_2 = os.path.join(path_clip, "clip_2.mp4")
clips_all_view.append(path_clip_2)
if os.path.exists(os.path.join(path_clip, "clip_3.mp4")):
path_clip_3 = os.path.join(path_clip, "clip_3.mp4")
clips_all_view.append(path_clip_3)
clips.append(clips_all_view)
return clips