Skip to content

Azure Kinect

Taisei Hanyu edited this page Aug 30, 2024 · 15 revisions

Set up Azure Kinect on Ubuntu 20.04

Install Azure Kinect SDK on Ubuntu 20.04

Configure Microsoft's Package Repository

First, you'll need to configure Microsoft's Package Repository, following the instructions here.

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

Modify /etc/apt/sources.list (sudo required). At the bottom of the file, add:

deb [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main
# deb-src [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main

Install Azure Kinect SDK

Install Azure Kinect SDK (Option 1)

Next, you'll need to install the Azure Kinect SDK. You can do this by running the following commands:

sudo apt install k4a-tools
sudo apt install libk4a1.4-dev

If the above commands fail, you may follow the below steps to install the SDK.

Install Azure Kinect SDK (Option 2)

Refer to issue:1263 Even though OS is for Ubuntu 18.04, it works for Ubuntu 20.04 as well.

Make sure to check the version[^1] of Azure Kinect SDK and Body Tracking SDK. You can check an option of libk4abt.-dev versions here And also libk4a.-dev here.

k4aVERSION=1.4
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/ubuntu/18.04/prod
curl -sSL https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft-prod.list
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-get update
sudo apt install libk4a{$k4aVERSION}-dev
# sudo apt install libk4abt${BodyTranckVERSION}-dev # you may not need this
sudo apt install k4a-tools=$k4aVERSION

[^1]: My version is 1.4.1 and 1.1 for SDK and Body Tracking SDK respectively.

Test Azure Kinect SDK

sudo k4aviewer

Linux Device Setup

To use Azure Kinect without sudo, you'll need to set up a udev rule. You can follow this page or do this by running the following command:

DO NOT COPY github.com link. USE raw.githubusercontent.com

# sudo mkdir -p '/etc/udev/rules.d/'
wget -P '/etc/udev/rules.d/' https://raw.githubusercontent.com/microsoft/Azure-Kinect-Sensor-SDK/develop/scripts/99-k4a.rules

Then, reattach Azure Kinect devices if attached during this process.

Hopefully, you should be able to run the following command without sudo:

k4aviewer

Use Azure Kinect in Python

Install pyKinectAzure

This is a Python wrapper for Azure Kinect SDK. You should be able to just follow the instructions on the GitHub page, but I'll repeat them here for convenience.

You have two options to install pyKinectAzure.

  1. Simple ( if no need to change the source code)
pip install pykinect_azure
  1. Advanced ( if you want to change the source code)
mkdir -p $YOUR_WORK_REPO/third_party
cd $YOUR_WORK_REPO/third_party
git clone https://github.com/ibaiGorordo/pyKinectAzure.git
cd pyKinectAzure
pip install -e .

Test pyKinectAzure

pyKinectAzure has banch of exsamples. You can run them to test the installation.

python exsamples/exampleColorImage.py

Use Azure Kinect in ROS

Writing... rosrun tf2_ros static_transform_publisher 1 0 0 0 0 0 rgb_camera_link map


Docker

Click to view the example of Dockerfile
# Stage 1: Build stage
FROM ubuntu:20.04 as azurekinect_base

# Install necessary dependencies
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    gnupg2 \
    lsb-release \
    software-properties-common

# Add Microsoft's package repository and install the Azure Kinect SDK
RUN wget -qO - https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main"

RUN apt-get update

# Set noninteractive frontend and accept EULA
RUN DEBIAN_FRONTEND=noninteractive ACCEPT_EULA=Y apt-get install -y \
    k4a-tools \
    libk4a1.4 \
    libk4a1.4-dev

# Copy the necessary runtime libraries to a temporary location
RUN mkdir /kinect-runtime && \
    cp -avL /usr/lib/x86_64-linux-gnu/libk4a.so /kinect-runtime/ && \
    cp -av /usr/lib/x86_64-linux-gnu/libk4a1.4/ /kinect-runtime/ && \
    cp -avL /usr/lib/x86_64-linux-gnu/libk4arecord.so /kinect-runtime/ && \
    ldd /usr/lib/x86_64-linux-gnu/libk4a.so | grep "=>" | awk '{print $3}' | xargs -I '{}' cp -v '{}' /kinect-runtime/ && \
    ldd /usr/bin/k4aviewer | grep "=>" | awk '{print $3}' | xargs -I '{}' cp -v '{}' /kinect-runtime/

# Stage 2: Final stage
FROM ubuntu:20.04

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
libusb-1.0-0 \
libsoundio1 \
libgl1-mesa-glx

# Copy necessary Kinect runtime libraries from the build stage
COPY --from=azurekinect_base /kinect-runtime/ /usr/lib/x86_64-linux-gnu/
COPY --from=azurekinect_base /usr/bin/k4aviewer /usr/bin/

# Set the environment variable to specify the library path
ENV LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

# Set the entry point for your application or container
ENTRYPOINT ["/bin/bash"]

You can pre-build the base image using the following command: docker build --target azurekinect_base -t azurekinect_base:latest -f AzureKinect.Dockerfile . In the final stage of the build, you need to include these commands:

# Copy necessary Kinect runtime libraries from the build stage
COPY --from=builder /kinect-runtime/ /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/bin/k4aviewer /usr/bin/

and install the required dependencies:

RUN apt-get update && apt-get install -y \
    libusb-1.0-0 \
    libsoundio1 \
    libgl1-mesa-glx
Some additional dependencies may be necessary for full functionality.
# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    libusb-1.0-0 \
    libglx-mesa0 \
    libgl1-mesa-dri \
    libgl1-mesa-glx \
    mesa-utils \
    libsoundio1 \
    libgl1 \
    libjack0 \
    libpulse0 \
    libasound2 \
    libudev1 \
    libdbus-1-3 \
    libsystemd0 \
    libwrap0 \
    libsndfile1 \
    libasyncns0 \
    libapparmor1 \
    libbsd0 \
    liblzma5 \
    liblz4-1 \
    libgcrypt20 \
    libflac8 \
    libogg0 \
    libvorbis0a \
    libvorbisenc2 \
    libgpg-error0
If you have any questions or find errors, please let me know. Thanks.