Raspberry Pi - 自动启动 OpenCv 脚本 - cv::imshow() 错误 [英] Raspberry Pi - Autostart OpenCv-Script - Error with cv::imshow()

查看:26
本文介绍了Raspberry Pi - 自动启动 OpenCv 脚本 - cv::imshow() 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短说明:

我想在启动后通过 systemd 服务脚本自动启动一个可执行文件(opencv 二进制文件,通过 C++ 生成),但我没有成功.

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

我将错误范围缩小到代码语句cv::imshow(....)",它打开一个窗口并显示一个图像.此时代码抛出错误:QXcbConnection:Could not connect to display"

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"

但是,如果我手动执行 sh-script 或二进制文件,两者都可以正常工作.我在 stackoverflow 上搜索了最常见的错误,并尝试修复我能找到的所有错误.我很确定:

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. 我的服务文件实际上在启动时运行(直到发生错误)
  2. 手动执行二进制文件工作正常
  3. 手动执行 .sh 脚本工作正常
  4. 我没有运行时链接错误(请参阅 .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 :)

.

我的系统:

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

.

我在/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

.

我的 shell 脚本(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

.

我的源代码的最小示例:(可执行)

#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;
}

.

附注:

我知道有一个类似的线程(运行 OpenCV 脚本从 imshow 开始).但是由于这个问题没有解决方案,而且我有更多的信息要分享,我认为开始一个新线程会更合适.

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:

问题出在我的服务脚本的配置中.我确实知道我需要一个 DISPLAY 变量来显示 X 显示器的位置,但我不知道它也需要授权这一事实.这个线程帮助我弄清楚了:

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

简而言之:

将这些添加到/etc/serviced/service 中 test.service 的行中:

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

Environment=XAUTHORITY=/home/pi/.Xauthority
Environment=DISPLAY=:0.0

这篇关于Raspberry Pi - 自动启动 OpenCv 脚本 - cv::imshow() 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆