forked from SolarFramework/SolARModuleOpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolARDeviceDataLoader.cpp
More file actions
221 lines (190 loc) · 7.98 KB
/
SolARDeviceDataLoader.cpp
File metadata and controls
221 lines (190 loc) · 7.98 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* @copyright Copyright (c) 2017 B-com http://www.b-com.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SolARDeviceDataLoader.h"
#include "SolAROpenCVHelper.h"
#include "core/Log.h"
#include "xpcf/core/helpers.h"
namespace xpcf = org::bcom::xpcf;
XPCF_DEFINE_FACTORY_CREATE_INSTANCE(SolAR::MODULES::OPENCV::SolARDeviceDataLoader)
namespace SolAR {
using namespace datastructure;
namespace MODULES {
namespace OPENCV {
SolARDeviceDataLoader::SolARDeviceDataLoader(): ConfigurableBase(xpcf::toUUID<SolARDeviceDataLoader>())
{
declareInterface<api::input::devices::IARDevice>(this);
declareProperty<std::string>("calibrationFile", m_calibrationFile);
declareProperty<std::string>("pathToData", m_pathToData);
declareProperty<int>("delayTime", m_delayTime);
}
SolARDeviceDataLoader::~SolARDeviceDataLoader()
{
for (int id_camera = 0; id_camera < m_nbCameras; ++id_camera) {
if (m_cameras[id_camera].isOpened())
m_cameras[id_camera].release();
if (m_poseFiles[id_camera].is_open())
m_poseFiles[id_camera].close();
}
if (m_timestampFile.is_open()) {
m_timestampFile.close();
}
}
org::bcom::xpcf::XPCFErrorCode SolARDeviceDataLoader::onConfigured()
{
// Load multi camera calibration file
if (m_calibrationFile.empty()) {
LOG_ERROR("Camera Calibration file path is empty");
return xpcf::XPCFErrorCode::_FAIL;
}
cv::FileStorage fs(m_calibrationFile, cv::FileStorage::READ);
if (!fs.isOpened()) {
LOG_ERROR("Cannot open camera calibration file: {}", m_calibrationFile);
return xpcf::XPCFErrorCode::_FAIL;
}
cv::Mat intrinsic_parameters;
cv::Mat distortion_parameters;
int width, height;
std::string cameraName;
CameraParameters camParams;
fs["NbCameras"] >> m_nbCameras;
if (m_nbCameras > 0) {
m_poseFiles.resize(m_nbCameras);
m_cameras.resize(m_nbCameras);
}
for (int i = 0; i < m_nbCameras; ++i)
{
auto params = fs[std::to_string(i)];
params["name"] >> cameraName;
params["image_width"] >> width;
params["image_height"] >> height;
params["camera_matrix"] >> intrinsic_parameters;
params["distortion_coefficients"] >> distortion_parameters;
m_cameraNames.push_back(cameraName);
camParams.resolution.width = width;
camParams.resolution.height = height;
if (intrinsic_parameters.empty())
{
LOG_ERROR("No intrinsics found in calibration file");
return xpcf::XPCFErrorCode::_FAIL;
}
if (intrinsic_parameters.rows == camParams.intrinsic.rows() && intrinsic_parameters.cols == camParams.intrinsic.cols())
for (int i = 0; i < intrinsic_parameters.rows; i++)
for (int j = 0; j < intrinsic_parameters.cols; j++)
camParams.intrinsic(i, j) = (float)intrinsic_parameters.at<double>(i, j);
else
{
LOG_ERROR("Camera Calibration should be a 3x3 Matrix");
return xpcf::XPCFErrorCode::_FAIL;
}
if (distortion_parameters.empty())
{
LOG_ERROR("No distortion parameters found in calibration file");
return xpcf::XPCFErrorCode::_FAIL;
}
if (distortion_parameters.rows == camParams.distortion.rows() && distortion_parameters.cols == camParams.distortion.cols())
for (int i = 0; i < distortion_parameters.rows; i++)
for (int j = 0; j < distortion_parameters.cols; j++)
camParams.distortion(i, j) = distortion_parameters.at<double>(i, j);
else
{
LOG_ERROR("Camera distortion matrix should be a 5x1 Matrix");
return xpcf::XPCFErrorCode::_FAIL;
}
m_camParameters.push_back(camParams);
LOG_DEBUG("Loaded {} intrinsics", i);
}
return xpcf::XPCFErrorCode::_SUCCESS;
}
FrameworkReturnCode SolARDeviceDataLoader::start()
{
// Prepare loader for images, poses
for (int id_camera = 0; id_camera < m_nbCameras; ++id_camera) {
char index[4] = {};
std::sprintf(index, "%03d", id_camera);
// pose loader
std::string pathToPose = m_pathToData + "/pose_" + index + ".txt";
m_poseFiles[id_camera].open(pathToPose);
if (!m_poseFiles[id_camera].is_open()) {
LOG_ERROR("Cannot load pose file of camera {}", id_camera);
return FrameworkReturnCode::_ERROR_;
}
// camera loader
std::string pathToImages = m_pathToData + "/" + index + "/%08d.jpg";
m_cameras[id_camera].open(pathToImages);
if (!m_cameras[id_camera].isOpened()) {
LOG_ERROR("Cannot open images directory {}", pathToImages);
return FrameworkReturnCode::_ERROR_;
}
}
// Prepare loader timestamps
std::string pathToTimestamps = m_pathToData + "/timestamps.txt";
m_timestampFile.open(pathToTimestamps);
if (!m_timestampFile.is_open()) {
LOG_ERROR("Cannot open timestamps file {}", pathToTimestamps);
return FrameworkReturnCode::_ERROR_;
}
return FrameworkReturnCode::_SUCCESS;
}
FrameworkReturnCode SolARDeviceDataLoader::stop()
{
return FrameworkReturnCode();
}
int SolARDeviceDataLoader::getNbCameras()
{
return m_nbCameras;
}
FrameworkReturnCode SolARDeviceDataLoader::getData(std::vector<SRef<Image>>& images, std::vector<Transform3Df>& poses, ATTRIBUTE(maybe_unused) std::chrono::system_clock::time_point & timestamp)
{
std::this_thread::sleep_for(std::chrono::milliseconds(m_delayTime));
for (int id_camera = 0; id_camera < m_nbCameras; ++id_camera) {
// load images
cv::Mat cvFrame;
m_cameras[id_camera] >> cvFrame;
if (!cvFrame.data)
return FrameworkReturnCode::_ERROR_LOAD_IMAGE;
SRef<Image> img;
SolAROpenCVHelper::convertToSolar(cvFrame, img);
images.push_back(img);
// load poses
Transform3Df pose;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
m_poseFiles[id_camera] >> pose(i, j);
poses.push_back(pose);
}
// load timestamp
int64 time;
m_timestampFile >> time;
// Todo: convert timestamp to time_point
//auto now = std::chrono::system_clock::now();
//auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
//auto value = now_ms.time_since_epoch();
//long duration = value.count();
//std::chrono::milliseconds dur(duration);
//std::chrono::time_point<std::chrono::system_clock> dt(dur);
return FrameworkReturnCode();
}
const CameraParameters & SolARDeviceDataLoader::getParameters(const int & camera_id) const
{
return m_camParameters[camera_id];
}
void SolARDeviceDataLoader::setParameters(const int & camera_id, const CameraParameters & parameters)
{
m_camParameters[camera_id] = parameters;
}
}
}
}