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

查看:271
本文介绍了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:无法连接到显示器"

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脚本或二进制文件,则两者都可以正常工作.我在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/(测试服务)中的脚本:

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

.

PS:

我知道有一个类似的线程(运行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:

问题出在我的服务脚本的配置中.我确实知道我需要在X Display的位置使用DISPLAY变量,但是我不知道它也需要授权.该线程帮助我弄清楚了:

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天全站免登陆