Skip to content

Commit e1eceab

Browse files
authored
Azure Kinect build from source on Ubuntu 16.04 (#1160)
1 parent b751616 commit e1eceab

File tree

5 files changed

+192
-114
lines changed

5 files changed

+192
-114
lines changed

3rdparty/azure_kinect/azure_kinect.cmake

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,90 @@
1+
if (BUILD_AZURE_KINECT AND APPLE)
2+
message(WARNING "Azure Kinect is not supported on macOS, setting BUILD_AZURE_KINECT to OFF")
3+
set(BUILD_AZURE_KINECT OFF)
4+
set(BUILD_AZURE_KINECT OFF PARENT_SCOPE)
5+
endif()
6+
7+
function(find_k4a_with_ubuntu_1604_pip_package)
8+
find_program(LSB_RELEASE_EXEC lsb_release)
9+
if (LSB_RELEASE_EXEC)
10+
execute_process(COMMAND ${LSB_RELEASE_EXEC} -is
11+
OUTPUT_VARIABLE LSB_DISTRIBUTION
12+
OUTPUT_STRIP_TRAILING_WHITESPACE
13+
)
14+
execute_process(COMMAND ${LSB_RELEASE_EXEC} -cs
15+
OUTPUT_VARIABLE LSB_CODENAME
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
)
18+
if(LSB_DISTRIBUTION STREQUAL "Ubuntu" AND LSB_CODENAME STREQUAL "xenial")
19+
message(STATUS "Ubuntu 16.04 detected, trying to load from open3d-azure-kinect-ubuntu1604 pip package")
20+
if (NOT PYTHON_EXECUTABLE)
21+
find_program(PYTHON_IN_PATH "python")
22+
set(PYTHON_EXECUTABLE ${PYTHON_IN_PATH})
23+
endif()
24+
message(STATUS "Using Python executable: ${PYTHON_EXECUTABLE}")
25+
execute_process(
26+
COMMAND ${PYTHON_EXECUTABLE} -c "import imp; print(imp.find_module('open3d_azure_kinect_ubuntu1604_fix')[1])"
27+
OUTPUT_VARIABLE AZURE_PACKAGE_FIX_PATH
28+
OUTPUT_STRIP_TRAILING_WHITESPACE
29+
ERROR_QUIET
30+
)
31+
if (AZURE_PACKAGE_FIX_PATH)
32+
message(STATUS "Found open3d_azure_kinect_ubuntu1604_fix pip package: ${AZURE_PACKAGE_FIX_PATH}")
33+
set(k4a_INCLUDE_DIRS ${AZURE_PACKAGE_FIX_PATH}/include)
34+
set(k4a_FOUND TRUE)
35+
else()
36+
message(STATUS "Cannot find open3d_azure_kinect_ubuntu1604_fix pip pacakge")
37+
endif()
38+
else()
39+
message(STATUS "Not Ubuntu 16.04, skipping open3d-azure-kinect-ubuntu1604 pip package load")
40+
endif()
41+
else()
42+
message(STATUS "Cannot find lsb_release command")
43+
endif()
44+
45+
# Export variables to function caller
46+
set(k4a_INCLUDE_DIRS ${k4a_INCLUDE_DIRS} PARENT_SCOPE)
47+
set(k4a_FOUND ${k4a_FOUND} PARENT_SCOPE)
48+
endfunction()
49+
50+
151
if (BUILD_AZURE_KINECT)
252
# Conditionally include header files in Open3D.h
353
set(BUILD_AZURE_KINECT_COMMENT "")
454

555
# Export the following variables:
656
# - k4a_INCLUDE_DIRS
7-
# - k4a_LIBRARY_DIRS
8-
# - k4a_LIBRARIES
957
if (WIN32)
1058
# We assume k4a 1.2.0 is installed in the default directory
11-
set(k4a_INCLUDE_DIRS "C:\\Program Files\\Azure Kinect SDK v1.2.0\\sdk\\include")
12-
# On Windows, we need to
13-
# 1) link with k4a.lib, k4arecord.lib
14-
set(k4a_STATIC_LIBRARY_DIR
15-
"C:\\Program Files\\Azure Kinect SDK v1.2.0\\sdk\\windows-desktop\\amd64\\release\\lib"
16-
)
17-
# 2) copy depthengine_2_0.dll, k4a.dll, k4a.record.dll to executable location
18-
set(k4a_DYNAMIC_LIBRARY_DIR
19-
"C:\\Program Files\\Azure Kinect SDK v1.2.0\\sdk\\windows-desktop\\amd64\\release\\bin"
20-
)
21-
set(k4a_LIBRARY_DIRS ${k4a_STATIC_LIBRARY_DIR} ${k4a_DYNAMIC_LIBRARY_DIR})
59+
if (K4A_INCLUDE_DIR)
60+
set(k4a_INCLUDE_DIRS ${K4A_INCLUDE_DIR})
61+
else()
62+
set(k4a_INCLUDE_DIRS "C:\\Program Files\\Azure Kinect SDK v1.2.0\\sdk\\include")
63+
endif()
2264
else()
65+
# Attempt 1: system-wide installed K4a
2366
# The property names are tested with k4a 1.2, future versions might work
2467
find_package(k4a 1.2 QUIET)
2568
find_package(k4arecord 1.2 QUIET)
2669
if (k4a_FOUND)
27-
get_target_property(k4a_INCLUDE_DIRS k4a::k4a INTERFACE_INCLUDE_DIRECTORIES)
28-
get_target_property(k4a_LIBRARIES k4a::k4a IMPORTED_LOCATION_RELWITHDEBINFO)
29-
get_target_property(k4arecord_INCLUDE_DIRS k4a::k4arecord INTERFACE_INCLUDE_DIRECTORIES)
30-
get_target_property(k4arecord_LIBRARIES k4a::k4arecord IMPORTED_LOCATION_RELWITHDEBINFO)
31-
message(STATUS "k4a_INCLUDE_DIRS: ${k4a_INCLUDE_DIRS}")
32-
message(STATUS "k4a_LIBRARIES: ${k4a_LIBRARIES}")
33-
message(STATUS "k4arecord_INCLUDE_DIRS: ${k4arecord_INCLUDE_DIRS}")
34-
message(STATUS "k4arecord_LIBRARIES: ${k4arecord_LIBRARIES}")
70+
get_target_property(k4a_INCLUDE_DIRS k4a::k4a INTERFACE_INCLUDE_DIRECTORIES)
71+
endif()
3572

36-
# Alias target to be consistent with windows
37-
add_library(k4a INTERFACE)
38-
add_library(k4arecord INTERFACE)
73+
# Attempt 2: "open3d-azure-kinect-ubuntu1604"
74+
# User need to run `pip install open3d-azure-kinect-ubuntu1604` first.
75+
# The Python package will provide headers and pre-compiled libs for
76+
# building k4a
77+
if (NOT k4a_FOUND)
78+
find_k4a_with_ubuntu_1604_pip_package()
79+
endif()
3980

40-
set(k4a_INCLUDE_DIRS k4a_INCLUDE_DIRS)
41-
42-
# Assume libk4a and libk4arecord are both in k4a_LIBRARY_DIR
43-
get_filename_component(k4a_LIBRARY_DIR ${k4a_LIBRARIES} DIRECTORY)
44-
set(k4a_LIBRARY_DIRS ${k4a_LIBRARY_DIR})
45-
else ()
81+
if (k4a_FOUND)
82+
message(STATUS "k4a_INCLUDE_DIRS: ${k4a_INCLUDE_DIRS}")
83+
else()
4684
message(FATAL_ERROR "Kinect SDK NOT found. Please install according \
4785
to https://github.com/microsoft/Azure-Kinect-Sensor-SDK/blob/develop/docs/usage.md")
48-
endif ()
86+
endif()
4987
endif()
50-
51-
set(k4a_LIBRARIES k4a k4arecord)
5288
else()
5389
# Conditionally include header files in Open3D.h
5490
set(BUILD_AZURE_KINECT_COMMENT "//")

docs/tutorial/Basic/azure_kinect.rst

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ flag.
183183
Unofficial Ubuntu 16.04 workaround
184184
==================================
185185

186+
The Azure Kinect SDK is not officially supported on Ubuntu 16.04. We provide
187+
unofficial support for experimental purposes.
188+
189+
Using Open3D Python packages
190+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191+
186192
For Python Open3D, run
187193

188194
.. code-block:: sh
@@ -200,7 +206,27 @@ The ``open3d_azure_kinect_ubuntu1604_fix`` will preload the shared libs and set
200206
``LD_LIBRARY_PATH`` which are then used by ``dlopen`` when the Kinect library
201207
is loaded from the compiled module.
202208

203-
To compile Open3D from source, you'll need to build and install K4A SDK
204-
manually. However, at runtime, you'll still need to ensure
205-
the 18.04 copy of ``libstdc++.so`` and ``libdepthengine.so`` are visible from
206-
``LD_LIBRARY_PATH``.
209+
After installing ``open3d_azure_kinect_ubuntu1604_fix``, import Open3D as usual
210+
with ``import open3d``, Open3D will try to load the shared libraries at
211+
initialization time in ``__init__.py``.
212+
213+
Compiling Open3D from source
214+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215+
216+
First, install ``open3d_azure_kinect_ubuntu1604_fix`` package in your Python
217+
environment. This package contains the headers that are required for compiling
218+
Open3D with Azure Kinect support.
219+
220+
When building Open3D from source, set the flag ``-DBUILD_AZURE_KINECT=ON``
221+
at CMake configure time. CMake will then try to detect the location of the
222+
``open3d_azure_kinect_ubuntu1604_fix`` package using the ``Python`` executable
223+
available from the current ``PATH``. Therefore, when running CMake, make sure
224+
that the same Python environment where ``open3d_azure_kinect_ubuntu1604_fix``
225+
was installed is activated.
226+
227+
If you build a C++ binary, you'll still need to ensure that
228+
``LD_LIBRARY_PATH`` contains the directory which contains ``libstdc++.so`` and
229+
``libdepthengine.so`` at runtime. If you build the compiled Open3D Python
230+
module, Open3D's ``__init__.py`` will try to import
231+
``open3d_azure_kinect_ubuntu1604_fix`` to append the ``LD_LIBRARY_PATH``
232+
automatically.

src/Open3D/IO/Sensor/AzureKinect/AzureKinectSensorConfig.cpp

Lines changed: 58 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <json/json.h>
3030
#include <k4a/k4a.h>
31+
#include <map>
3132
#include <set>
3233
#include <string>
3334
#include <unordered_map>
@@ -38,105 +39,95 @@
3839
namespace open3d {
3940
namespace io {
4041

41-
static std::unordered_map<k4a_image_format_t, std::string>
42-
k4a_image_format_t_to_string({
43-
{K4A_IMAGE_FORMAT_COLOR_MJPG, "K4A_IMAGE_FORMAT_COLOR_MJPG"},
44-
{K4A_IMAGE_FORMAT_COLOR_NV12, "K4A_IMAGE_FORMAT_COLOR_NV12"},
45-
{K4A_IMAGE_FORMAT_COLOR_YUY2, "K4A_IMAGE_FORMAT_COLOR_YUY2"},
46-
{K4A_IMAGE_FORMAT_COLOR_BGRA32,
47-
"K4A_IMAGE_FORMAT_COLOR_BGRA32"},
48-
{K4A_IMAGE_FORMAT_DEPTH16, "K4A_IMAGE_FORMAT_DEPTH16"},
49-
{K4A_IMAGE_FORMAT_IR16, "K4A_IMAGE_FORMAT_IR16"},
50-
{K4A_IMAGE_FORMAT_CUSTOM, "K4A_IMAGE_FORMAT_CUSTOM"},
51-
});
52-
53-
static std::unordered_map<k4a_color_resolution_t, std::string>
54-
k4a_color_resolution_t_to_string({
42+
static std::map<k4a_image_format_t, std::string> k4a_image_format_t_to_string{
43+
{K4A_IMAGE_FORMAT_COLOR_MJPG, "K4A_IMAGE_FORMAT_COLOR_MJPG"},
44+
{K4A_IMAGE_FORMAT_COLOR_NV12, "K4A_IMAGE_FORMAT_COLOR_NV12"},
45+
{K4A_IMAGE_FORMAT_COLOR_YUY2, "K4A_IMAGE_FORMAT_COLOR_YUY2"},
46+
{K4A_IMAGE_FORMAT_COLOR_BGRA32, "K4A_IMAGE_FORMAT_COLOR_BGRA32"},
47+
{K4A_IMAGE_FORMAT_DEPTH16, "K4A_IMAGE_FORMAT_DEPTH16"},
48+
{K4A_IMAGE_FORMAT_IR16, "K4A_IMAGE_FORMAT_IR16"},
49+
{K4A_IMAGE_FORMAT_CUSTOM, "K4A_IMAGE_FORMAT_CUSTOM"},
50+
};
51+
52+
static std::map<k4a_color_resolution_t, std::string>
53+
k4a_color_resolution_t_to_string{
5554
{K4A_COLOR_RESOLUTION_OFF, "K4A_COLOR_RESOLUTION_OFF"},
5655
{K4A_COLOR_RESOLUTION_720P, "K4A_COLOR_RESOLUTION_720P"},
5756
{K4A_COLOR_RESOLUTION_1080P, "K4A_COLOR_RESOLUTION_1080P"},
5857
{K4A_COLOR_RESOLUTION_1440P, "K4A_COLOR_RESOLUTION_1440P"},
5958
{K4A_COLOR_RESOLUTION_1536P, "K4A_COLOR_RESOLUTION_1536P"},
6059
{K4A_COLOR_RESOLUTION_2160P, "K4A_COLOR_RESOLUTION_2160P"},
6160
{K4A_COLOR_RESOLUTION_3072P, "K4A_COLOR_RESOLUTION_3072P"},
62-
});
63-
64-
static std::unordered_map<k4a_depth_mode_t, std::string>
65-
k4a_depth_mode_t_to_string({
66-
{K4A_DEPTH_MODE_OFF, "K4A_DEPTH_MODE_OFF"},
67-
{K4A_DEPTH_MODE_NFOV_2X2BINNED,
68-
"K4A_DEPTH_MODE_NFOV_2X2BINNED"},
69-
{K4A_DEPTH_MODE_NFOV_UNBINNED, "K4A_DEPTH_MODE_NFOV_UNBINNED"},
70-
{K4A_DEPTH_MODE_WFOV_2X2BINNED,
71-
"K4A_DEPTH_MODE_WFOV_2X2BINNED"},
72-
{K4A_DEPTH_MODE_WFOV_UNBINNED, "K4A_DEPTH_MODE_WFOV_UNBINNED"},
73-
{K4A_DEPTH_MODE_PASSIVE_IR, "K4A_DEPTH_MODE_PASSIVE_IR"},
74-
});
75-
76-
static std::unordered_map<k4a_fps_t, std::string> k4a_fps_t_to_string({
61+
};
62+
63+
static std::map<k4a_depth_mode_t, std::string> k4a_depth_mode_t_to_string{
64+
{K4A_DEPTH_MODE_OFF, "K4A_DEPTH_MODE_OFF"},
65+
{K4A_DEPTH_MODE_NFOV_2X2BINNED, "K4A_DEPTH_MODE_NFOV_2X2BINNED"},
66+
{K4A_DEPTH_MODE_NFOV_UNBINNED, "K4A_DEPTH_MODE_NFOV_UNBINNED"},
67+
{K4A_DEPTH_MODE_WFOV_2X2BINNED, "K4A_DEPTH_MODE_WFOV_2X2BINNED"},
68+
{K4A_DEPTH_MODE_WFOV_UNBINNED, "K4A_DEPTH_MODE_WFOV_UNBINNED"},
69+
{K4A_DEPTH_MODE_PASSIVE_IR, "K4A_DEPTH_MODE_PASSIVE_IR"},
70+
};
71+
72+
static std::map<k4a_fps_t, std::string> k4a_fps_t_to_string{
7773
{K4A_FRAMES_PER_SECOND_5, "K4A_FRAMES_PER_SECOND_5"},
7874
{K4A_FRAMES_PER_SECOND_15, "K4A_FRAMES_PER_SECOND_15"},
7975
{K4A_FRAMES_PER_SECOND_30, "K4A_FRAMES_PER_SECOND_30"},
80-
});
76+
};
8177

82-
static std::unordered_map<k4a_wired_sync_mode_t, std::string>
83-
k4a_wired_sync_mode_t_to_string({
78+
static std::map<k4a_wired_sync_mode_t, std::string>
79+
k4a_wired_sync_mode_t_to_string{
8480
{K4A_WIRED_SYNC_MODE_STANDALONE,
8581
"K4A_WIRED_SYNC_MODE_STANDALONE"},
8682
{K4A_WIRED_SYNC_MODE_MASTER, "K4A_WIRED_SYNC_MODE_MASTER"},
8783
{K4A_WIRED_SYNC_MODE_SUBORDINATE,
8884
"K4A_WIRED_SYNC_MODE_SUBORDINATE"},
89-
});
90-
91-
static std::unordered_map<std::string, k4a_image_format_t>
92-
string_to_k4a_image_format_t({
93-
{"K4A_IMAGE_FORMAT_COLOR_MJPG", K4A_IMAGE_FORMAT_COLOR_MJPG},
94-
{"K4A_IMAGE_FORMAT_COLOR_NV12", K4A_IMAGE_FORMAT_COLOR_NV12},
95-
{"K4A_IMAGE_FORMAT_COLOR_YUY2", K4A_IMAGE_FORMAT_COLOR_YUY2},
96-
{"K4A_IMAGE_FORMAT_COLOR_BGRA32",
97-
K4A_IMAGE_FORMAT_COLOR_BGRA32},
98-
{"K4A_IMAGE_FORMAT_DEPTH16", K4A_IMAGE_FORMAT_DEPTH16},
99-
{"K4A_IMAGE_FORMAT_IR16", K4A_IMAGE_FORMAT_IR16},
100-
{"K4A_IMAGE_FORMAT_CUSTOM", K4A_IMAGE_FORMAT_CUSTOM},
101-
});
102-
103-
static std::unordered_map<std::string, k4a_color_resolution_t>
104-
string_to_k4a_color_resolution_t({
85+
};
86+
87+
static std::map<std::string, k4a_image_format_t> string_to_k4a_image_format_t({
88+
{"K4A_IMAGE_FORMAT_COLOR_MJPG", K4A_IMAGE_FORMAT_COLOR_MJPG},
89+
{"K4A_IMAGE_FORMAT_COLOR_NV12", K4A_IMAGE_FORMAT_COLOR_NV12},
90+
{"K4A_IMAGE_FORMAT_COLOR_YUY2", K4A_IMAGE_FORMAT_COLOR_YUY2},
91+
{"K4A_IMAGE_FORMAT_COLOR_BGRA32", K4A_IMAGE_FORMAT_COLOR_BGRA32},
92+
{"K4A_IMAGE_FORMAT_DEPTH16", K4A_IMAGE_FORMAT_DEPTH16},
93+
{"K4A_IMAGE_FORMAT_IR16", K4A_IMAGE_FORMAT_IR16},
94+
{"K4A_IMAGE_FORMAT_CUSTOM", K4A_IMAGE_FORMAT_CUSTOM},
95+
});
96+
97+
static std::map<std::string, k4a_color_resolution_t>
98+
string_to_k4a_color_resolution_t{
10599
{"K4A_COLOR_RESOLUTION_OFF", K4A_COLOR_RESOLUTION_OFF},
106100
{"K4A_COLOR_RESOLUTION_720P", K4A_COLOR_RESOLUTION_720P},
107101
{"K4A_COLOR_RESOLUTION_1080P", K4A_COLOR_RESOLUTION_1080P},
108102
{"K4A_COLOR_RESOLUTION_1440P", K4A_COLOR_RESOLUTION_1440P},
109103
{"K4A_COLOR_RESOLUTION_1536P", K4A_COLOR_RESOLUTION_1536P},
110104
{"K4A_COLOR_RESOLUTION_2160P", K4A_COLOR_RESOLUTION_2160P},
111105
{"K4A_COLOR_RESOLUTION_3072P", K4A_COLOR_RESOLUTION_3072P},
112-
});
113-
114-
static std::unordered_map<std::string, k4a_depth_mode_t>
115-
string_to_k4a_depth_mode_t({
116-
{"K4A_DEPTH_MODE_OFF", K4A_DEPTH_MODE_OFF},
117-
{"K4A_DEPTH_MODE_NFOV_2X2BINNED",
118-
K4A_DEPTH_MODE_NFOV_2X2BINNED},
119-
{"K4A_DEPTH_MODE_NFOV_UNBINNED", K4A_DEPTH_MODE_NFOV_UNBINNED},
120-
{"K4A_DEPTH_MODE_WFOV_2X2BINNED",
121-
K4A_DEPTH_MODE_WFOV_2X2BINNED},
122-
{"K4A_DEPTH_MODE_WFOV_UNBINNED", K4A_DEPTH_MODE_WFOV_UNBINNED},
123-
{"K4A_DEPTH_MODE_PASSIVE_IR", K4A_DEPTH_MODE_PASSIVE_IR},
124-
});
125-
126-
static std::unordered_map<std::string, k4a_fps_t> string_to_k4a_fps_t({
106+
};
107+
108+
static std::map<std::string, k4a_depth_mode_t> string_to_k4a_depth_mode_t{
109+
{"K4A_DEPTH_MODE_OFF", K4A_DEPTH_MODE_OFF},
110+
{"K4A_DEPTH_MODE_NFOV_2X2BINNED", K4A_DEPTH_MODE_NFOV_2X2BINNED},
111+
{"K4A_DEPTH_MODE_NFOV_UNBINNED", K4A_DEPTH_MODE_NFOV_UNBINNED},
112+
{"K4A_DEPTH_MODE_WFOV_2X2BINNED", K4A_DEPTH_MODE_WFOV_2X2BINNED},
113+
{"K4A_DEPTH_MODE_WFOV_UNBINNED", K4A_DEPTH_MODE_WFOV_UNBINNED},
114+
{"K4A_DEPTH_MODE_PASSIVE_IR", K4A_DEPTH_MODE_PASSIVE_IR},
115+
};
116+
117+
static std::map<std::string, k4a_fps_t> string_to_k4a_fps_t{
127118
{"K4A_FRAMES_PER_SECOND_5", K4A_FRAMES_PER_SECOND_5},
128119
{"K4A_FRAMES_PER_SECOND_15", K4A_FRAMES_PER_SECOND_15},
129120
{"K4A_FRAMES_PER_SECOND_30", K4A_FRAMES_PER_SECOND_30},
130-
});
121+
};
131122

132-
static std::unordered_map<std::string, k4a_wired_sync_mode_t>
133-
string_to_k4a_wired_sync_mode_t({
123+
static std::map<std::string, k4a_wired_sync_mode_t>
124+
string_to_k4a_wired_sync_mode_t{
134125
{"K4A_WIRED_SYNC_MODE_STANDALONE",
135126
K4A_WIRED_SYNC_MODE_STANDALONE},
136127
{"K4A_WIRED_SYNC_MODE_MASTER", K4A_WIRED_SYNC_MODE_MASTER},
137128
{"K4A_WIRED_SYNC_MODE_SUBORDINATE",
138129
K4A_WIRED_SYNC_MODE_SUBORDINATE},
139-
});
130+
};
140131

141132
static std::unordered_map<std::string, std::string> standard_config{
142133
{"color_format", "K4A_IMAGE_FORMAT_COLOR_MJPG"},

0 commit comments

Comments
 (0)