ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
AzureKinectSensorConfig.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Logging.h>
11 #include <json/json.h>
12 #include <k4a/k4a.h>
13 
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <unordered_map>
18 
20 
21 namespace cloudViewer {
22 namespace io {
23 
24 static std::map<k4a_image_format_t, std::string> k4a_image_format_t_to_string{
25  {K4A_IMAGE_FORMAT_COLOR_MJPG, "K4A_IMAGE_FORMAT_COLOR_MJPG"},
26  {K4A_IMAGE_FORMAT_COLOR_NV12, "K4A_IMAGE_FORMAT_COLOR_NV12"},
27  {K4A_IMAGE_FORMAT_COLOR_YUY2, "K4A_IMAGE_FORMAT_COLOR_YUY2"},
28  {K4A_IMAGE_FORMAT_COLOR_BGRA32, "K4A_IMAGE_FORMAT_COLOR_BGRA32"},
29  {K4A_IMAGE_FORMAT_DEPTH16, "K4A_IMAGE_FORMAT_DEPTH16"},
30  {K4A_IMAGE_FORMAT_IR16, "K4A_IMAGE_FORMAT_IR16"},
31  {K4A_IMAGE_FORMAT_CUSTOM, "K4A_IMAGE_FORMAT_CUSTOM"},
32 };
33 
34 static std::map<k4a_color_resolution_t, std::string>
36  {K4A_COLOR_RESOLUTION_OFF, "K4A_COLOR_RESOLUTION_OFF"},
37  {K4A_COLOR_RESOLUTION_720P, "K4A_COLOR_RESOLUTION_720P"},
38  {K4A_COLOR_RESOLUTION_1080P, "K4A_COLOR_RESOLUTION_1080P"},
39  {K4A_COLOR_RESOLUTION_1440P, "K4A_COLOR_RESOLUTION_1440P"},
40  {K4A_COLOR_RESOLUTION_1536P, "K4A_COLOR_RESOLUTION_1536P"},
41  {K4A_COLOR_RESOLUTION_2160P, "K4A_COLOR_RESOLUTION_2160P"},
42  {K4A_COLOR_RESOLUTION_3072P, "K4A_COLOR_RESOLUTION_3072P"},
43  };
44 
45 static std::map<k4a_depth_mode_t, std::string> k4a_depth_mode_t_to_string{
46  {K4A_DEPTH_MODE_OFF, "K4A_DEPTH_MODE_OFF"},
47  {K4A_DEPTH_MODE_NFOV_2X2BINNED, "K4A_DEPTH_MODE_NFOV_2X2BINNED"},
48  {K4A_DEPTH_MODE_NFOV_UNBINNED, "K4A_DEPTH_MODE_NFOV_UNBINNED"},
49  {K4A_DEPTH_MODE_WFOV_2X2BINNED, "K4A_DEPTH_MODE_WFOV_2X2BINNED"},
50  {K4A_DEPTH_MODE_WFOV_UNBINNED, "K4A_DEPTH_MODE_WFOV_UNBINNED"},
51  {K4A_DEPTH_MODE_PASSIVE_IR, "K4A_DEPTH_MODE_PASSIVE_IR"},
52 };
53 
54 static std::map<k4a_fps_t, std::string> k4a_fps_t_to_string{
55  {K4A_FRAMES_PER_SECOND_5, "K4A_FRAMES_PER_SECOND_5"},
56  {K4A_FRAMES_PER_SECOND_15, "K4A_FRAMES_PER_SECOND_15"},
57  {K4A_FRAMES_PER_SECOND_30, "K4A_FRAMES_PER_SECOND_30"},
58 };
59 
60 static std::map<k4a_wired_sync_mode_t, std::string>
62  {K4A_WIRED_SYNC_MODE_STANDALONE,
63  "K4A_WIRED_SYNC_MODE_STANDALONE"},
64  {K4A_WIRED_SYNC_MODE_MASTER, "K4A_WIRED_SYNC_MODE_MASTER"},
65  {K4A_WIRED_SYNC_MODE_SUBORDINATE,
66  "K4A_WIRED_SYNC_MODE_SUBORDINATE"},
67  };
68 
69 static std::map<std::string, k4a_image_format_t> string_to_k4a_image_format_t({
70  {"K4A_IMAGE_FORMAT_COLOR_MJPG", K4A_IMAGE_FORMAT_COLOR_MJPG},
71  {"K4A_IMAGE_FORMAT_COLOR_NV12", K4A_IMAGE_FORMAT_COLOR_NV12},
72  {"K4A_IMAGE_FORMAT_COLOR_YUY2", K4A_IMAGE_FORMAT_COLOR_YUY2},
73  {"K4A_IMAGE_FORMAT_COLOR_BGRA32", K4A_IMAGE_FORMAT_COLOR_BGRA32},
74  {"K4A_IMAGE_FORMAT_DEPTH16", K4A_IMAGE_FORMAT_DEPTH16},
75  {"K4A_IMAGE_FORMAT_IR16", K4A_IMAGE_FORMAT_IR16},
76  {"K4A_IMAGE_FORMAT_CUSTOM", K4A_IMAGE_FORMAT_CUSTOM},
77 });
78 
79 static std::map<std::string, k4a_color_resolution_t>
81  {"K4A_COLOR_RESOLUTION_OFF", K4A_COLOR_RESOLUTION_OFF},
82  {"K4A_COLOR_RESOLUTION_720P", K4A_COLOR_RESOLUTION_720P},
83  {"K4A_COLOR_RESOLUTION_1080P", K4A_COLOR_RESOLUTION_1080P},
84  {"K4A_COLOR_RESOLUTION_1440P", K4A_COLOR_RESOLUTION_1440P},
85  {"K4A_COLOR_RESOLUTION_1536P", K4A_COLOR_RESOLUTION_1536P},
86  {"K4A_COLOR_RESOLUTION_2160P", K4A_COLOR_RESOLUTION_2160P},
87  {"K4A_COLOR_RESOLUTION_3072P", K4A_COLOR_RESOLUTION_3072P},
88  };
89 
90 static std::map<std::string, k4a_depth_mode_t> string_to_k4a_depth_mode_t{
91  {"K4A_DEPTH_MODE_OFF", K4A_DEPTH_MODE_OFF},
92  {"K4A_DEPTH_MODE_NFOV_2X2BINNED", K4A_DEPTH_MODE_NFOV_2X2BINNED},
93  {"K4A_DEPTH_MODE_NFOV_UNBINNED", K4A_DEPTH_MODE_NFOV_UNBINNED},
94  {"K4A_DEPTH_MODE_WFOV_2X2BINNED", K4A_DEPTH_MODE_WFOV_2X2BINNED},
95  {"K4A_DEPTH_MODE_WFOV_UNBINNED", K4A_DEPTH_MODE_WFOV_UNBINNED},
96  {"K4A_DEPTH_MODE_PASSIVE_IR", K4A_DEPTH_MODE_PASSIVE_IR},
97 };
98 
99 static std::map<std::string, k4a_fps_t> string_to_k4a_fps_t{
100  {"K4A_FRAMES_PER_SECOND_5", K4A_FRAMES_PER_SECOND_5},
101  {"K4A_FRAMES_PER_SECOND_15", K4A_FRAMES_PER_SECOND_15},
102  {"K4A_FRAMES_PER_SECOND_30", K4A_FRAMES_PER_SECOND_30},
103 };
104 
105 static std::map<std::string, k4a_wired_sync_mode_t>
107  {"K4A_WIRED_SYNC_MODE_STANDALONE",
108  K4A_WIRED_SYNC_MODE_STANDALONE},
109  {"K4A_WIRED_SYNC_MODE_MASTER", K4A_WIRED_SYNC_MODE_MASTER},
110  {"K4A_WIRED_SYNC_MODE_SUBORDINATE",
111  K4A_WIRED_SYNC_MODE_SUBORDINATE},
112  };
113 
114 static std::unordered_map<std::string, std::string> standard_config{
115  {"color_format", "K4A_IMAGE_FORMAT_COLOR_MJPG"},
116  {"color_resolution", "K4A_COLOR_RESOLUTION_720P"},
117  {"depth_mode", "K4A_DEPTH_MODE_WFOV_2X2BINNED"},
118  {"camera_fps", "K4A_FRAMES_PER_SECOND_30"},
119  {"synchronized_images_only", "false"},
120  {"depth_delay_off_color_usec", "0"},
121  {"wired_sync_mode", "K4A_WIRED_SYNC_MODE_STANDALONE"},
122  {"subordinate_delay_off_master_usec", "0"},
123  {"disable_streaming_indicator", "false"},
124 };
125 
128 }
129 
131  const std::unordered_map<std::string, std::string> &config,
132  bool verbose) {
133  bool rc = true;
134 
135  // No extra keys
136  for (const auto &it : config) {
137  if (standard_config.find(it.first) == standard_config.end()) {
138  rc = false;
139  utility::LogWarning("IsValidConfig: Unrecognized key {}", it.first);
140  }
141  }
142 
143  if (config.count("color_format") != 0 &&
144  string_to_k4a_image_format_t.count(config.at("color_format")) == 0) {
145  rc = false;
146  if (verbose) {
147  utility::LogWarning("IsValidConfig: color_format invalid");
148  }
149  }
150 
151  if (config.count("color_resolution") != 0 &&
152  string_to_k4a_color_resolution_t.count(config.at("color_resolution")) ==
153  0) {
154  rc = false;
155  if (verbose) {
156  utility::LogWarning("IsValidConfig: color_resolution invalid");
157  }
158  }
159 
160  if (config.count("depth_mode") != 0 &&
161  string_to_k4a_depth_mode_t.count(config.at("depth_mode")) == 0) {
162  rc = false;
163  if (verbose) {
164  utility::LogWarning("IsValidConfig: depth_mode invalid");
165  }
166  }
167 
168  if (config.count("camera_fps") != 0 &&
169  string_to_k4a_fps_t.count(config.at("camera_fps")) == 0) {
170  rc = false;
171  if (verbose) {
172  utility::LogWarning("IsValidConfig: camera_fps invalid");
173  }
174  } else {
175  if (config.count("camera_fps") != 0 &&
176  config.count("color_resolution") != 0 &&
177  config.at("camera_fps") == "K4A_FRAMES_PER_SECOND_30" &&
178  config.at("color_resolution") == "K4A_COLOR_RESOLUTION_3072P") {
179  rc = false;
180  if (verbose) {
182  "K4A_COLOR_RESOLUTION_3072P does not support 30 FPS");
183  }
184  }
185  }
186 
187  if (config.count("synchronized_images_only") != 0 &&
188  config.at("synchronized_images_only") != "true" &&
189  config.at("synchronized_images_only") != "false") {
190  rc = false;
191  if (verbose) {
193  "IsValidConfig: synchronized_images_only invalid");
194  }
195  }
196 
197  if (config.count("wired_sync_mode") != 0 &&
198  string_to_k4a_wired_sync_mode_t.count(config.at("wired_sync_mode")) ==
199  0) {
200  rc = false;
201  if (verbose) {
202  utility::LogWarning("IsValidConfig: wired_sync_mode invalid");
203  }
204  }
205 
206  if (config.count("disable_streaming_indicator") != 0 &&
207  config.at("disable_streaming_indicator") != "true" &&
208  config.at("disable_streaming_indicator") != "false") {
209  rc = false;
210  if (verbose) {
212  "IsValidConfig: disable_streaming_indicator invalid");
213  }
214  }
215 
216  return rc;
217 }
218 
220  const std::unordered_map<std::string, std::string> &config)
222  if (!IsValidConfig(config)) {
223  utility::LogWarning("Invalid configs, default configs will be used.");
224  return;
225  }
226  for (const auto &it : config) {
227  config_[it.first] = it.second;
228  }
229 }
230 
231 bool AzureKinectSensorConfig::ConvertToJsonValue(Json::Value &value) const {
232  // clang-format off
233  value["color_format"] = config_.at("color_format");
234  value["color_resolution"] = config_.at("color_resolution");
235  value["depth_mode"] = config_.at("depth_mode");
236  value["camera_fps"] = config_.at("camera_fps");
237  value["synchronized_images_only"] = config_.at("synchronized_images_only");
238  value["depth_delay_off_color_usec"] = config_.at("depth_delay_off_color_usec");
239  value["wired_sync_mode"] = config_.at("wired_sync_mode");
240  value["subordinate_delay_off_master_usec"] = config_.at("subordinate_delay_off_master_usec");
241  value["disable_streaming_indicator"] = config_.at("disable_streaming_indicator");
242  // clang-format on
243  return true;
244 }
245 
246 bool AzureKinectSensorConfig::ConvertFromJsonValue(const Json::Value &value) {
247  // clang-format off
248  config_["color_format"] = value["color_format"].asString();
249  config_["color_resolution"] = value["color_resolution"].asString();
250  config_["depth_mode"] = value["depth_mode"].asString();
251  config_["camera_fps"] = value["camera_fps"].asString();
252  config_["synchronized_images_only"] = value["synchronized_images_only"].asString();
253  config_["depth_delay_off_color_usec"] = value["depth_delay_off_color_usec"].asString();
254  config_["wired_sync_mode"] = value["wired_sync_mode"].asString();
255  config_["subordinate_delay_off_master_usec"] = value["subordinate_delay_off_master_usec"].asString();
256  config_["disable_streaming_indicator"] = value["disable_streaming_indicator"].asString();
257  // clang-format on
258 
259  return IsValidConfig(config_, true);
260 }
261 
263  const k4a_device_configuration_t &k4a_config) {
264  // clang-format off
265  config_["color_format"] = k4a_image_format_t_to_string.at(k4a_config.color_format);
266  config_["color_resolution"] = k4a_color_resolution_t_to_string.at(k4a_config.color_resolution);
267  config_["depth_mode"] = k4a_depth_mode_t_to_string.at(k4a_config.depth_mode);
268  config_["camera_fps"] = k4a_fps_t_to_string.at(k4a_config.camera_fps);
269  config_["synchronized_images_only"] = k4a_config.synchronized_images_only ? "true" : "false";
270  config_["depth_delay_off_color_usec"] = std::to_string(k4a_config.depth_delay_off_color_usec);
271  config_["wired_sync_mode"] = k4a_wired_sync_mode_t_to_string.at(k4a_config.wired_sync_mode);
272  config_["subordinate_delay_off_master_usec"] = std::to_string(k4a_config.subordinate_delay_off_master_usec);
273  config_["disable_streaming_indicator"] = k4a_config.disable_streaming_indicator ? "true" : "false";
274  // clang-format on
275  if (!IsValidConfig(config_)) {
277  "Internal error, invalid configs. Maybe SDK version is "
278  "mismatched.");
279  }
280 }
281 
283  const {
284  k4a_device_configuration_t k4a_config;
285  // clang-format off
286  k4a_config.color_format = string_to_k4a_image_format_t.at(config_.at("color_format"));
287  k4a_config.color_resolution = string_to_k4a_color_resolution_t.at(config_.at("color_resolution"));
288  k4a_config.depth_mode = string_to_k4a_depth_mode_t.at(config_.at("depth_mode"));
289  k4a_config.camera_fps = string_to_k4a_fps_t.at(config_.at("camera_fps"));
290  k4a_config.synchronized_images_only = config_.at("synchronized_images_only") == "true";
291  k4a_config.depth_delay_off_color_usec = static_cast<int32_t>(std::stoi(config_.at("depth_delay_off_color_usec")));
292  k4a_config.wired_sync_mode = string_to_k4a_wired_sync_mode_t.at(config_.at("wired_sync_mode"));
293  k4a_config.subordinate_delay_off_master_usec = static_cast<uint32_t>(std::stoi(config_.at("subordinate_delay_off_master_usec")));
294  k4a_config.disable_streaming_indicator = config_.at("disable_streaming_indicator") == "true";
295  // clang-format on
296  return k4a_config;
297 }
298 
299 } // namespace io
300 } // namespace cloudViewer
_k4a_device_configuration_t ConvertToNativeConfig() const
bool ConvertToJsonValue(Json::Value &value) const override
std::unordered_map< std::string, std::string > config_
void ConvertFromNativeConfig(const _k4a_device_configuration_t &k4a_config)
bool ConvertFromJsonValue(const Json::Value &value) override
AzureKinectSensorConfig()
Default constructor, default configs will be used.
static bool IsValidConfig(const std::unordered_map< std::string, std::string > &config, bool verbose=true)
#define LogWarning(...)
Definition: Logging.h:72
#define LogError(...)
Definition: Logging.h:60
static std::map< k4a_image_format_t, std::string > k4a_image_format_t_to_string
static std::map< std::string, k4a_fps_t > string_to_k4a_fps_t
static std::unordered_map< std::string, std::string > standard_config
static std::map< k4a_depth_mode_t, std::string > k4a_depth_mode_t_to_string
static std::map< std::string, k4a_wired_sync_mode_t > string_to_k4a_wired_sync_mode_t
static std::map< std::string, k4a_depth_mode_t > string_to_k4a_depth_mode_t
static std::map< k4a_color_resolution_t, std::string > k4a_color_resolution_t_to_string
static std::map< std::string, k4a_color_resolution_t > string_to_k4a_color_resolution_t
static std::map< k4a_wired_sync_mode_t, std::string > k4a_wired_sync_mode_t_to_string
static std::map< k4a_fps_t, std::string > k4a_fps_t_to_string
static std::map< std::string, k4a_image_format_t > string_to_k4a_image_format_t({ {"K4A_IMAGE_FORMAT_COLOR_MJPG", K4A_IMAGE_FORMAT_COLOR_MJPG}, {"K4A_IMAGE_FORMAT_COLOR_NV12", K4A_IMAGE_FORMAT_COLOR_NV12}, {"K4A_IMAGE_FORMAT_COLOR_YUY2", K4A_IMAGE_FORMAT_COLOR_YUY2}, {"K4A_IMAGE_FORMAT_COLOR_BGRA32", K4A_IMAGE_FORMAT_COLOR_BGRA32}, {"K4A_IMAGE_FORMAT_DEPTH16", K4A_IMAGE_FORMAT_DEPTH16}, {"K4A_IMAGE_FORMAT_IR16", K4A_IMAGE_FORMAT_IR16}, {"K4A_IMAGE_FORMAT_CUSTOM", K4A_IMAGE_FORMAT_CUSTOM}, })
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20