-
Notifications
You must be signed in to change notification settings - Fork 1
/
group_video_segment.py
118 lines (106 loc) · 4.77 KB
/
group_video_segment.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
import pandas as pd
import os
class GroupSegment:
def __init__(self, config, setname, test_mode=None, participant_id=None):
self.cfg = config
self.n_segments = self.cfg.n_segments
self.setname = setname
# Reading Annotation file
if not self.setname == "test":
self.df = pd.read_csv(self.cfg.anno_path)
elif test_mode == "seen":
self.df = pd.read_csv(self.cfg.anno_path_seen)
elif test_mode == "unseen":
self.df = pd.read_csv(self.cfg.anno_path_unseen)
# Accumulating data index relevent for setname mode
self.items = []
self.verb_labels = []
self.noun_labels = []
for idx, item in self.df.iterrows():
if self.setname == "val-unseen":
if int(item.participant_id[1:]) >= 26:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
elif self.setname == "train-unseen":
if int(item.participant_id[1:]) < 26:
if participant_id:
if item.participant_id == participant_id:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
else:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
elif self.setname == "val-seen":
if item["video_id"] in self.cfg.val_seen_vids:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
elif self.setname == "train-seen":
if item["video_id"] not in self.cfg.val_seen_vids:
if participant_id:
if item.participant_id == participant_id:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
else:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
elif self.setname == "whole-train":
if participant_id:
if item.participant_id == participant_id:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
else:
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
elif self.setname == "test":
self.items.append(idx)
self.verb_labels.append(int(item.verb_class))
self.noun_labels.append(int(item.noun_class))
if len(self.items) == 0:
self.haveData = False
else:
self.haveData = True
def _process_snapshot_(self, idx):
# Getting global Index of dataframe
idx = self.items[idx]
self.dataFrame = self.df.iloc[idx]
if self.setname == "test":
self.uid = self.dataFrame["uid"]
else:
# Extracting Verb and Noun annotation
self.ann_verb = self.dataFrame["verb_class"]
self.ann_noun = self.dataFrame["noun_class"]
# Extracting Frame #
self.low_idx, self.high_idx = (
int(self.dataFrame["start_frame"]),
int(self.dataFrame["stop_frame"]),
)
self.segment_len = int((self.high_idx - self.low_idx) / self.n_segments)
# Image folder path for given participant id and video id
self.img_path_global = (
self.cfg.data_path
+ self.dataFrame["participant_id"]
+ "/"
+ self.dataFrame["video_id"]
+ "/"
)
def _get_image_path_(self, segment_indx):
""" Given a segment index, lower_index_for_instance and segment_length we
evaluate index of the frame to consider. From each segments center frame
is considered for training and validation"""
path = os.path.join(
self.img_path_global,
self.cfg.image_tmpl.format(
self.low_idx
+ (segment_indx * self.segment_len)
+ int(self.segment_len / 2)
),
)
return path