-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_helper_files.cpp
146 lines (129 loc) · 3.51 KB
/
create_helper_files.cpp
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
#include <bits/stdc++.h>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
// Number of frames per action used in training
#define SAMPLE_SIZE 10
map<int,int> action_to_verb;
void load_action_verb_map() {
// open csv file for reading
ifstream in;
in.open("vu17_charades/Charades_v1_mapping.txt", ifstream::in);
string s1, s2, s3;
while(in >> s1 >> s2 >> s3) {
action_to_verb[stoi(s1.substr(1, s1.size()))] = stoi(s3.substr(1, s3.size()));
}
in.close();
}
// Parse Charades' csv files and create lists of samples for each class
// csv_filename: name of the input file
// preffix: preffix for the output files ("prefix%d.txt")
void create_helper_files(string csv_filename, string preffix) {
// specify what csv columns to use
// check http://vuchallenge.org/README-charades.txt for details
vector<int> items;
items.push_back(0); // id
items.push_back(9); // actions
// open csv file for reading
ifstream in;
in.open(csv_filename, ifstream::in);
string s;
getline(in,s);
while(getline(in,s)) {
// parse selected columns from an input line
vector<string> v;
for(int k=0; k < items.size(); k++) {
int i = items[k];
string out="";
int j=0, p=0;
while(j < i) {
while(p < s.size() && s[p] != ',') {
if(s[p] == '"') {
p++;
while(p < s.size() && s[p] != '"')
p++;
}
p++;
}
p++;
j++;
}
if(j==i) {
while(p < s.size() && s[p] != ',') {
out += s[p];
p++;
}
}
v.push_back(out);
}
// discover the number of available frames for the current video
DIR *dir;
struct dirent *ent;
string dirname = "Charades_v1_features_flow/";
dirname += v[0]+"/";
dir = opendir(dirname.c_str());
s="";
while((ent = readdir(dir)) != NULL) {
string tmp = ent->d_name;
if(tmp > s)
s = tmp;
}
closedir(dir);
int numframes = stoi(s.substr(s.find("-")+1, s.find(".")), nullptr, 10);
// parse actions
s = v[1];
while(s.size()) {
// get next action
string tmp;
int pos = s.find(";");
if(pos >= 0) {
tmp = s.substr(0, pos);
s = s.substr(pos+1, s.size());
}
else {
tmp = s;
s = "";
}
stringstream stream(tmp.substr(1, tmp.size()));
int action; // class number
double start, end; // time interval in seconds
stream >> action >> start >> end;
// output filename
string outfile = preffix;
outfile += to_string(action_to_verb[action]);
outfile += ".txt";
// check if time interval is valid
if(end <= start)
continue;
// convert time interval to frame number (24 fps)
start = 1.0+start*24.0;
end = 1.0+end*24.0;
int i, j;
for(i=1; i < start; i+=4);
if(i >= numframes)
continue;
for(j=i; j+4 < end && j+4 <= numframes; j+=4);
vector<int> vi;
for(int k=i; k <= j; k+=4)
vi.push_back(k);
// pick SAMPLE_SIZE frames and save their names
ofstream fp;
fp.open(outfile, ofstream::out | ofstream::app);
for(int k=0; k < SAMPLE_SIZE; k++)
fp << "Charades_v1_features_flow/" << v[0] << "/" << v[0] << "-" << setfill('0') << setw(6) << vi[((vi.size()-1)*k) / (SAMPLE_SIZE-1)] << ".txt" << (k==(SAMPLE_SIZE-1)?"":";");
fp << endl;
fp.close();
};
}
in.close();
}
// Main program
int main(int argc, char **argv) {
// Create mapping between actions and verbs
load_action_verb_map();
// Create training helper files
create_helper_files("vu17_charades/Charades_vu17_train.csv", "helper_files/train_");
// Create validation helper files
create_helper_files("vu17_charades/Charades_vu17_validation.csv", "helper_files/val_");
return 0;
}