I can't get librealsense working on my Raspberry Pi 3

Hi! I’m trying to get a Realsense D415 to work with a raspberry pi 3 for a project of mine.
I found this old post on the topic, but when I try to copy the dockerfile from the solution post it fails on the first RUN with the following error: /bin/sh: 1: Syntax error: end of file unexpected.
If I comment out that line, the RUN mkdir build && [...] && make install step throws a ton of warning: ISO C forbids conversion of object pointer to function pointer type then c++: internal compiler error: Killed (program cc1plus) , make[2]: *** [wrappers/python/CMakeFiles/pybackend2.dir/pybackend.cpp.o] Error 4 , make[1]: *** [wrappers/python/CMakeFiles/pybackend2.dir/all] Error 2, and make: *** [all] Error 2.
Can anyone help?

1 Like

@SamMidd welcome back to the balena forums!

could you please confirm the Dockerfile that you are using?

This is the dockerfile I’m using:

# Build image for Raspberry Pi 4 Base 64
FROM balenalib/aarch64-debian:stretch-build
LABEL io.balena.device-type="raspberrypi4-64"
RUN echo "deb http://archive.raspberrypi.org/debian stretch main ui" >>  /etc/apt/sources.list.d/raspi.list \
	&& apt-key adv --batch --keyserver ha.pool.sks-keyservers.net  --recv-key <key>

RUN apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y

RUN apt-get install -y python3 \
    python3-dev \
    python3-pip \
    python3-setuptools 

# Install and Build Librealsense (For D415 Intel RealSense Camera)
RUN apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS:bool=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make -j4 &&\
    make install

ENV LD_LIBRARY_PATH="/usr/local/lib"

ENV INITSYSTEM on

WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/

CMD ["python3", "/usr/src/app/test_server.py"]

Hello @SamMidd could you please confirm if you are using a Raspberry Pi 4 or a Raspberry Pi 3?

In case you are using a Pi 3, are you deploying this on a Raspberry Pi 3 - 64 bit device type fleet?

@mpous I’m using a Pi 3 on a Pi 4 fleet because I’m using a 7" Pi touchscreen and balena doesn’t work with the Pi 4 and touchscreen.

Changing to a Pi 3-64 fleet and correctly labeling the device type makes the first RUN work, but the build still fails when trying to build librealsense

Could you please share more details on the errors that you get when trying to build librealsense?

Here is a copy of the build logs:
buildError.txt (66.0 KB)

There are 60 occurrences of warning: ISO C forbids conversion of object pointer to function pointer type [-Wpedantic], then the first thing that seems to fail is this, starting at line 803:

[Build] [main] wrappers/python/CMakeFiles/pybackend2.dir/build.make:62: recipe for target 'wrappers/python/CMakeFiles/pybackend2.dir/pybackend.cpp.o' failed [Build] [main] c++: internal compiler error: Killed (program cc1plus) [Build] Please submit a full bug report, [Build] with preprocessed source if appropriate. [Build] See <file:///usr/share/doc/gcc-6/README.Bugs> for instructions. [Build] [Build] [main] make[2]: *** [wrappers/python/CMakeFiles/pybackend2.dir/pybackend.cpp.o] Error 4 [Build] make[2]: *** Waiting for unfinished jobs.... [Build] [Build] [main] [ 13%] Building C object tools/embed/CMakeFiles/rs-embed.dir/__/__/third-party/realsense-file/lz4/lz4.c.o [Build] [main] CMakeFiles/Makefile2:269: recipe for target 'wrappers/python/CMakeFiles/pybackend2.dir/all' failed [Build] [main] make[1]: *** [wrappers/python/CMakeFiles/pybackend2.dir/all] Error 2 [Build] [Build] [main] make[1]: *** Waiting for unfinished jobs....

The target that first fails started building on line 138.

Then, starting on line 874:

Build] [main] Makefile:127: recipe for target 'all' failed [Build] [main] make: *** [all] Error 2 [Build] [Build] [main] Removing intermediate container 71e3cf8e667b Some services failed to build: main: The command '/bin/sh -c mkdir build && cd build && cmake /librealsense/ -DBUILD_PYTHON_BINDINGS:bool=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release && make -j4 && make install' returned a non-zero code: 2

Hi @Sammidd,

I noticed multiple issues with your Dockerfile:

  • You are using Debian Stretch as a base image, however this version reached end of life in July: Debian -- News -- Debian 8 Long Term Support reaching end-of-life, I recommend going to Bullseye which is the latest LTS version,
  • You left a <key> placeholder in your first RUN instruction. I had to dig a little for this one but figured out the key to use: 82B129927FA3303E

Using these two modifications I could build for a Rasberrypi3-64 fleet without issues using this Dockerfile:

# Build image for Raspberry Pi 4 Base 64
FROM balenalib/aarch64-debian:bullseye-build
LABEL io.balena.device-type="raspberrypi4-64"
RUN echo "deb http://archive.raspberrypi.org/debian bullseye main ui" >>  /etc/apt/sources.list.d/raspi.list &&\
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 82B129927FA3303E

RUN apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y

RUN apt-get install -y python3 \
    python3-dev \
    python3-pip \
    python3-setuptools 

# Install and Build Librealsense (For D415 Intel RealSense Camera)
RUN apt-get install -y git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev cmake libglfw3-dev build-essential
RUN git clone https://github.com/IntelRealSense/librealsense.git
RUN cd librealsense/ && ./scripts/setup_udev_rules.sh
RUN mkdir build &&\
    cd build &&\
    cmake /librealsense/ -DBUILD_PYTHON_BINDINGS:bool=true -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=false -DCMAKE_BUILD_TYPE=Release &&\
    make -j4 &&\
    make install

ENV LD_LIBRARY_PATH="/usr/local/lib"

ENV INITSYSTEM on

WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/

CMD ["python3", "/usr/src/app/test_server.py"]

Let us know if it fixes your issue,

Aurélien

I get quite a few warnings:
At the start of step 3, I get
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
Near the start of step 4 I get
W: Skipping acquire of configured file 'ui/binary-arm64/Packages' as repository 'http://archive.raspberrypi.org/debian bullseye InRelease' doesn't have the component 'ui' (component misspelt in sources.list?)
Later in step 4 & in steps 5 & 6 I get
debconf: delaying package configuration, since apt-utils is not installed
In step 8 I get
Failed to send reload request: No such file or directory
The first time I tried to build, step 9 hung at
[ 4%] Building CXX object wrappers/python/CMakeFiles/pybackend2.dir/pybackend_extras.cpp.o
When I try again, I get

[Build] [main] [ 3%] Building C object tools/embed/CMakeFiles/rs-embed.dir/__/__/third-party/realsense-file/lz4/lz4.c.o [Build] [main] c++: fatal error: Killed signal terminated program cc1plus [Build] compilation terminated.

And a whole bunch more errors
buildError2.txt (21.3 KB)
I tried again and it got to roughly the same point then seemed like it had actually built successfully despite skipping a bunch of steps

Yes, I was aware of the apt-key depreciation, it was just the legacy, and fastest way to do it. The clean way for step 3 would be:

RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/raspberrypiorg.gpg] http://archive.raspberrypi.org/debian bullseye main" >>  /etc/apt/sources.list.d/raspi.list &&\
    mkdir -p /etc/apt/keyrings &&\
    curl -fsSL http://archive.raspberrypi.org/debian/raspberrypi.gpg.key | gpg --dearmor -o /etc/apt/keyrings/raspberrypiorg.gpg

(I also removes the ui which generated a warning but was not used).

the ‘debconf’ part is probably due to the apt upgrade, I also get it but it doesn’t disturb the build.

For the other errors however, this is weird. I’ve re-run the build using the exact same script (only removing the app copy in the end to compile) and it worked using a RaspberryPi3-64 fleet on balenaCloud.

If you build in local mode, can you check your device SD-card? The final image is quite heavy (3.7GB), you might be running out of space, which breaks the build.

I was building in local mode, and switching local mode off fixed it, but there should be plenty of space on the SD card, it’s a Samsung Evo Plus 64GB…

So the image builds now, but when I put print(dir(rs)) at the end of test_server.py I get
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__'],
which doesn’t include any of the bindings needed to actually use a realsense camera.
for reference, running dir(rs) on my desktop returns
['BufData', 'D400', 'DEPTH', 'L500', 'SR300', 'STAEControl', 'STAFactor', 'STCensusRadius', 'STColorControl', 'STColorCorrection', 'STDepthControlGroup', 'STDepthTableControl', 'STHdad', 'STRauColorThresholdsControl', 'STRauSupportVectorControl', 'STRsm', 'STSloColorThresholdsControl', 'STSloPenaltyControl', 'T200', 'TRACKING', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'align', 'any', 'any_intel', 'auto_calibrated_device', 'calib_target_type', 'calibrated_sensor', 'calibration_change_device', 'calibration_status', 'calibration_type', 'camera_info', 'color_sensor', 'colorizer', 'composite_frame', 'config', 'context', 'debug_protocol', 'debug_stream_sensor', 'decimation_filter', 'depth_frame', 'depth_huffman_decoder', 'depth_sensor', 'depth_stereo_sensor', 'device', 'device_calibration', 'device_list', 'disparity_frame', 'disparity_transform', 'distortion', 'dsm_params', 'enable_rolling_log_file', 'event_information', 'extension', 'extrinsics', 'filter', 'filter_interface', 'firmware_log_message', 'firmware_log_parsed_message', 'firmware_logger', 'fisheye_sensor', 'format', 'frame', 'frame_metadata_value', 'frame_queue', 'frame_source', 'hdr_merge', 'hole_filling_filter', 'intrinsics', 'l500_visual_preset', 'log', 'log_message', 'log_severity', 'log_to_callback', 'log_to_console', 'log_to_file', 'max_usable_range_sensor', 'motion_device_intrinsic', 'motion_frame', 'motion_sensor', 'motion_stream', 'motion_stream_profile', 'non_intel', 'notification', 'notification_category', 'option', 'option_range', 'options', 'pipeline', 'pipeline_profile', 'pipeline_wrapper', 'playback', 'playback_status', 'pointcloud', 'points', 'pose', 'pose_frame', 'pose_sensor', 'pose_stream', 'pose_stream_profile', 'processing_block', 'product_line', 'pyrealsense2', 'quaternion', 'recorder', 'region_of_interest', 'reset_logger', 'roi_sensor', 'rs2_deproject_pixel_to_point', 'rs2_fov', 'rs2_project_color_pixel_to_depth_pixel', 'rs2_project_point_to_pixel', 'rs2_transform_point_to_point', 'rs400_advanced_mode', 'save_single_frameset', 'save_to_ply', 'sensor', 'sequence_id_filter', 'serializable_device', 'software_device', 'software_motion_frame', 'software_notification', 'software_pose_frame', 'software_sensor', 'software_video_frame', 'spatial_filter', 'stream', 'stream_profile', 'syncer', 'temporal_filter', 'terminal_parser', 'texture_coordinate', 'threshold_filter', 'timestamp_domain', 'tm2', 'units_transform', 'updatable', 'update_device', 'vector', 'vertex', 'video_frame', 'video_stream', 'video_stream_profile', 'wheel_odometer', 'yuy_decoder', 'zero_order_invalidation']

@Sammidd, glad to see the build worked,

I also tried to build in local mode without success. From what I’ve seem the limiting factor was the RAM. Even turning down parallel building to 1 thread (make -j1 instead of make -j4) the build couldn’t finish on my Pi3 with a 64bits OS, which correlates with the doc on Raspberry Pi: librealsense/installation_raspbian.md at master · IntelRealSense/librealsense · GitHub .

About using realsense library, I suppose the library is not installed in a proper python or library search path. I think you can check this route.
You can always replace the CMD of your dockerfile to run sh and then, once deployed connect to the container with balena ssh (SSH access - Balena Documentation) to debug the paths or iterate your tests without having to rebuild for every test.

Maybe other community members have more experience with that part, I don’t have experience with this library and how it is installed or should behave.

I figured it out! (I think)

I found the following files in /usr/lib/python3/dist-packages/pyrealsense2/:

pybackend2.cpython-39-aarch64-linux-gnu.so pyrealsense2.cpython-39-aarch64-linux-gnu.so pybackend2.cpython-39-aarch64-linux-gnu.so.2 pyrealsense2.cpython-39-aarch64-linux-gnu.so.2.51 pybackend2.cpython-39-aarch64-linux-gnu.so.2.51.1 pyrealsense2.cpython-39-aarch64-linux-gnu.so.2.51.1

The module would import properly only while in the folder, despite /usr/lib/python3/dist-packages being in sys.path. Adding an empty __init__.py to /usr/lib/python3/dist-packages/pyrealsense2 allowed it to be properly imported anywhere.

Apparently that was a fever dream, because now when I touch /usr/lib/python3/dist-packages/pyrealsense2/__init__.py it just adds __builtins__ and __cached__ to dir(rs)

I realized that I had added /usr/lib/python3/dist-packages/pyrealsense2 to PYTHONPATH when it worked the first time.

By looking at my local install of pyrealsense2, I worked out that it needed an __init__.py that read from .pyrealsense2 import *

Hi @Sammidd,
Thanks for the update.

So if I understand correctly, you found a way to fix you issue by updating your environment (e.g. with a bash script updating the PYTHONPATH before running your python script) and eventually copying a new __init__.py file with the right content to /usr/lib/python3/dist-packages/pyrealsense2/__init__.py during the image build in your Dockerfile.

Am I correct? (just checking the procedure, as it might be useful for others to be able to reproduce for their own projects)

Thanks

That’s right. Once I have verified that I can use the module, I’ll post the dockerfile and associated files.

I’ve gotten the pyrealsense example script that produces ASCII depth images running on the Pi 3 and sending the images to the log on the dashboard. Here are the files required:
Dockerfile.txt (1.4 KB)
__init__.py.txt (87 Bytes)
python-tutorial-1-depth.py.txt (1.9 KB)
I had to append .txt to the names to upload them.
If I want further assistance with getting images from the camera to the touchscreen, should I start a new thread?

1 Like