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
|
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <assert.h>
#include <k4a/k4a.h>
#include <math.h>
#include <atomic>
#include <csignal>
#include <ctime>
#include <iostream>
#include "open3d/Open3D.h"
using namespace open3d;
void PrintHelp() {
using namespace open3d;
PrintOpen3DVersion();
// clang-format off
utility::LogInfo("Usage:");
utility::LogInfo(" > AzureKinectRecord [options]");
utility::LogInfo("Basic options:");
utility::LogInfo(" --help, -h : Print help information.");
utility::LogInfo(" --config : Config .json file (default: none)");
utility::LogInfo(" --list : List the currently connected K4A devices");
utility::LogInfo(" --device : Specify the device index to use (default: 0)");
utility::LogInfo(" --output : Output mkv file name (default: current_time.mkv)");
utility::LogInfo(" -a : Align depth with color image (default: disabled)");
// clang-format on
utility::LogInfo("");
}
int main(int argc, char **argv) {
if (argc < 1 ||
utility::ProgramOptionExistsAny(argc, argv, {"-h", "--help"})) {
PrintHelp();
return 1;
}
if (utility::ProgramOptionExists(argc, argv, "--list")) {
io::AzureKinectSensor::ListDevices();
return 0;
}
io::AzureKinectSensorConfig sensor_config;
if (utility::ProgramOptionExists(argc, argv, "--config")) {
auto config_filename =
utility::GetProgramOptionAsString(argc, argv, "--config", "");
bool success = io::ReadIJsonConvertibleFromJSON(config_filename,
sensor_config);
if (!success) {
utility::LogInfo("Invalid sensor config");
return 1;
}
} else {
utility::LogInfo("Use default sensor config");
}
int sensor_index =
utility::GetProgramOptionAsInt(argc, argv, "--device", 0);
if (sensor_index < 0 || sensor_index > 255) {
utility::LogWarning("Sensor index must between [0, 255]: {}",
sensor_index);
return 1;
}
bool enable_align_depth_to_color =
utility::ProgramOptionExists(argc, argv, "-a");
std::string recording_filename = utility::GetProgramOptionAsString(
argc, argv, "--output", utility::GetCurrentTimeStamp() + ".mkv");
utility::LogInfo("Prepare writing to {}", recording_filename);
// Init recorder
io::AzureKinectRecorder recorder(sensor_config, sensor_index);
if (!recorder.InitSensor()) {
utility::LogWarning("Failed to connect to sensor, abort.");
return 1;
}
bool flag_record = false;
bool flag_exit = false;
bool is_geometry_added = false;
visualization::VisualizerWithKeyCallback vis;
vis.RegisterKeyCallback(
GLFW_KEY_SPACE, [&](visualization::Visualizer *vis) {
if (flag_record) {
utility::LogInfo(
"Recording paused. "
"Press [SPACE] to continue. "
"Press [ESC] to save and exit.");
flag_record = false;
} else if (!recorder.IsRecordCreated()) {
if (recorder.OpenRecord(recording_filename)) {
utility::LogInfo(
"Recording started. "
"Press [SPACE] to pause. "
"Press [ESC] to save and exit.");
flag_record = true;
} // else flag_record keeps false
} else {
utility::LogInfo(
"Recording resumed, video may be discontinuous. "
"Press [SPACE] to pause. "
"Press [ESC] to save and exit.");
flag_record = true;
}
return false;
});
vis.RegisterKeyCallback(
GLFW_KEY_ESCAPE, [&](visualization::Visualizer *vis) {
flag_exit = true;
if (recorder.IsRecordCreated()) {
utility::LogInfo("Recording finished.");
} else {
utility::LogInfo("Nothing has been recorded.");
}
return false;
});
utility::LogInfo(
"In the visualizer window, "
"press [SPACE] to start recording, "
"press [ESC] to exit.");
vis.CreateVisualizerWindow("Open3D Azure Kinect Recorder", 1920, 540);
do {
auto im_rgbd =
recorder.RecordFrame(flag_record, enable_align_depth_to_color);
if (im_rgbd == nullptr) {
utility::LogDebug("Invalid capture, skipping this frame");
continue;
}
if (!is_geometry_added) {
vis.AddGeometry(im_rgbd);
is_geometry_added = true;
}
// Update visualizer
vis.UpdateGeometry();
vis.PollEvents();
vis.UpdateRender();
} while (!flag_exit);
recorder.CloseRecord();
return 0;
}
|