-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModalityWriter.cpp
194 lines (157 loc) · 4.85 KB
/
ModalityWriter.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
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
//
// ModalityWriter.cpp
// segmenthreetion
//
// Created by Cristina Palmero Cantariño on 10/03/14.
//
//
#include "ModalityWriter.h"
#include <sys/stat.h>
#include <string>
#include <fstream>
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::filesystem;
using namespace std;
ModalityWriter::ModalityWriter()
{ }
ModalityWriter::ModalityWriter(string dataPath) : m_DataPath(dataPath)
{
setDataPath(m_DataPath);
}
void ModalityWriter::setDataPath(string dataPath)
{
m_DataPath = dataPath;
const char* path = m_DataPath.c_str();
if( exists( path ) )
{
directory_iterator end;
directory_iterator iter(path);
for( ; iter != end ; ++iter )
{
if ( is_directory( *iter ) && iter->path().string().find("Scene") != std::string::npos)
{
string scenePath = iter->path().string();
m_ScenesPaths.push_back(scenePath);
cout << "Scene found: " << scenePath << endl;
}
}
}
else
{
cerr << "Data path is not containing any scene(s)!" << endl;
}
}
void ModalityWriter::write(string modality, ModalityData& md)
{
for (int i = 0; i < m_ScenesPaths.size(); i++)
{
saveMats(m_ScenesPaths[i] + "/Masks/" + modality + "/",
".png",
*md.getPredictedMasksInScene(i),
*md.getFramesIndicesInScene(i));
saveMats(m_ScenesPaths[i] + "/GroundTruth/" + modality + "/",
".png",
*md.getGroundTruthMasksInScene(i),
*md.getFramesIndicesInScene(i));
saveBoundingRects(m_ScenesPaths[i] + "/Masks/" + modality + ".yml",
*md.getPredictedBoundingRectsInScene(i),
*md.getTagsInScene(i));
}
}
void ModalityWriter::saveMats(string dir, const char *format, vector<cv::Mat> frames, vector<string> filenames)
{
std::vector<int> qualityType;
qualityType.push_back(CV_IMWRITE_PNG_COMPRESSION);
qualityType.push_back(3);
for(int f = 0; f < frames.size(); f++)
{
stringstream fn;
fn << dir << filenames[f] << format;
string fileName = fn.str();
imwrite(fileName,frames[f],qualityType);
}
}
template<typename T>
void ModalityWriter::serialize(vector<vector<T> > m, vector<T>& s)
{
s.clear();
for (int i = 0; i < m.size(); i++)
{
s.reserve(s.size() + m[i].size());
s.insert(s.end(), m[i].begin(), m[i].end());
}
}
void ModalityWriter::saveValidBoundingBoxes(string file, vector<vector<int> > validBbs)
{
cv::FileStorage fs(file, cv::FileStorage::WRITE);
//vector<int> srlValidBbs;
//serialize(validBbs, srlValidBbs);
//fs << "validBbs" << validBbs;
fs << "num_frames" << static_cast<int>(validBbs.size());
for(unsigned int i=0;i<validBbs.size();i++)
{
fs << ("frame_" + to_string(i)) << validBbs.at(i);
}
fs.release();
}
void ModalityWriter::loadValidBoundingBoxes(string file, vector<vector<int> >& validBbs)
{
cv::FileStorage fs(file, cv::FileStorage::READ);
int n;
fs["num_frames"] >> n;
validBbs.resize(n);
for(unsigned int i=0;i<validBbs.size();i++)
{
fs["frame_" + to_string(i)] >> validBbs[i];
}
fs.release();
}
void ModalityWriter::loadValidBoundingBoxes(string file, vector<int> & validBbs)
{
vector<vector<int> > _validBbs;
loadValidBoundingBoxes(file, _validBbs);
serialize(_validBbs, validBbs);
}
void ModalityWriter::saveBoundingRects(string file, vector<vector<cv::Rect> > rects, vector<vector<int> > tags)
{
vector<vector<int> > rectsInt(rects.size());
this->boundRectsToInt(rects, rectsInt);
cv::FileStorage fs(file, cv::FileStorage::WRITE);
fs << "num_frames" << static_cast<int>(rectsInt.size());
for(unsigned int i=0;i<rectsInt.size();i++)
{
stringstream ss;
string s;
ss << i;
s = ss.str();
string a = "coords_";
a+=s;
fs << a << rectsInt.at(i);
}
for(unsigned int i=0;i<tags.size();i++)
{
stringstream ss;
string s;
ss << i;
s = ss.str();
string a = "tags_";
a+=s;
fs << a << tags.at(i);
}
fs.release();
}
void ModalityWriter::boundRectsToInt(vector<vector<cv::Rect> > bbModal, vector<vector<int> >& bbModalInt) {
for(unsigned int d = 0; d < bbModal.size() ; d++) //frames
{
for(unsigned int r = 0; r < bbModal[d].size(); r++)
{
bbModalInt[d].push_back(bbModal[d][r].tl().x);
bbModalInt[d].push_back(bbModal[d][r].tl().y);
bbModalInt[d].push_back(bbModal[d][r].br().x);
bbModalInt[d].push_back(bbModal[d][r].br().y);
}
}
}