简体   繁体   中英

Raspberry Pi - Autostart OpenCv-Script - Error with cv::imshow()

Short Description:

I want to auto-start an executable (opencv binary file, generate via c++) via a systemd service-script after booting, but I am unsuccessful.

I narrowed down the error to the code statement "cv::imshow(....)" which opens a window and displays an image. At this point, the code throws the error: "QXcbConnection: Could not connect to display"

However, if I manually execute the sh-script or the binary, both work fine. I searched around stackoverflow for the most common errors, and I tried to fix all I could found. I am quite sure, that:

  1. My service file actually runs at start (until the error occurred)
  2. Manually execution of the binary file works fine
  3. Manually execution of the .sh-script works fine
  4. I do not have runtime-linking errors (see .sh-script)

I would appreciate any help. Please help me fix the error, and please explain to me, why this error even occurs in the first place. Thanks a lot :)

.

My system:

Machine: Raspberry Pi 3 Model B
Architecture: arm32 / ARMv7
OS: NOOBS

.

My script in /etc/systemd/system/ (test.service):

[Unit]
Description=lalala

[Service]
Type=oneshot
ExecStart=/bin/bash "/home/pi/Desktop/test.sh" start
ExecStop=/bin/bash "/home/pi/Desktop/test.sh" stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Moreover, I did execute the following commands:

sudo chmod u+rwx /etc/systemd/system/test.service

sudo systemctl enable test

And if I start the service manually, it runs with the same error output as while autostarting during the boot process:

sudo systemctl enable test

.

My shell script (test.sh):

#!/bin/sh -e

exec 2> /tmp/test.sh.log       # send stderr to a log file
exec 1>&2                      # send stdout to the same log file
set -x                         # tell sh to display commands before execution


echo "in script"


start() 
{
    echo "in start"

    sleep 30

    LD_LIBRARY_PATH=/usr/local/OpenCV/lib:/usr/local/SFML/lib:/usr/local/curl/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH

    /home/pi/Desktop/test/main -e &
}


# THE OTHER CASES, NOT PUT IN HERE (stop, status)


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status 
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac

exit 0

.

Minimal example of my source code: (executable)

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main()
{
    cv::Mat frame;
    cv::namedWindow("result", cv::WINDOW_NORMAL);

    ## CRASH

    return 0;
}

.

PS:

I am aware that there is a similar thread like this ( Run OpenCV script on start with imshow ). But as there is no solution for this question, and as I have more information to share, I thought it would be more appropriate to start a new thread.

Luckily, I solved the problem:

The problem was in the configuration of my serviced-script. I did know that I need a DISPLAY variable to the location of the X Display, but I was not aware of the fact that it needs authorization as well. This thread helped me figure it out:

https://unix.stackexchange.com/questions/85244/setting-display-in-systemd-service-file

In short:

Add these to lines to test.service in /etc/serviced/service:

Environment=XAUTHORITY=/home/pi/.Xauthority
Environment=DISPLAY=:0.0
[Unit]
Description=lalala

[Service]
Type=oneshot
ExecStart=/bin/bash "/home/pi/Desktop/test.sh" start
ExecStop=/bin/bash "/home/pi/Desktop/test.sh" stop
RemainAfterExit=yes
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment=DISPLAY=:0.0

[Install]
WantedBy=multi-user.target

Full code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM