[英]Fail to automatically start an opencv program on raspberry pi 3
我在树莓派B +上自动启动C ++脚本时遇到问题。 我为此项目使用了树莓派相机。 我的脚本包括使用openCv库在第一阶段进行面部检测并在第二阶段进行视频录制。 我将视频录制阶段置于线程中。 我按照这篇文章中的说明自动启动了我的程序: https : //www.raspberrypi.org/forums/viewtopic.php?t=138861并且该程序可以正常工作,直到达到视频录制阶段为止,然后从该错误中获取此错误从systemctl状态日志中:
raspberrypi RASPCAMERA [2720]:无法初始化服务器:无法连接:连接被拒绝。 raspberrypi RASPCAMERA [2720]:(my_detection:2720):Gtk-警告**:无法打开显示:raspberrypi系统[1]:startCam.service:主进程已退出,代码退出,状态为1 / FAILURE raspberrypi系统[1]单元startCam.service进入失败状态。
即使面部检测阶段起作用,其响应还是很慢的。 我也尝试过rc.local或init.d方法,但是它们都没有。 感谢您的帮助。
这是我的系统单位:
[Service]
Type=simple
WorkingDirectory= /home/pi/C_code/Raspi_Cam/build
ExecStart= /home/pi/C_code/Raspi_Cam/build/Raspi_cam
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=RASPCAMERA
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
这是我的C ++代码脚本:
// FaceDetection.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <pthread.h>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <ctime> // localtime
#include <sstream> // stringstream
#include <iomanip> // put_time
#include <string> // string
using namespace std;
using namespace cv;
int displayAndDetect(Mat);
void* threadVideoRecord(void *);
string fileNameface = "haarcascade_frontalface_alt.xml";
string fileNameeyes = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_class;
CascadeClassifier eye_class;
string window_name = "my_face_detection";
VideoCapture capture(0);
int FrameWidth = capture.get(CAP_PROP_FRAME_WIDTH);
int FrameHeight = capture.get(CAP_PROP_FRAME_HEIGHT);
std::string return_current_time_and_date() {
std::string s = date::format("%F %T", std::chrono::system_clock::now());
for (std::string::iterator it = s.begin(); it != s.end(); it++)
if (*it == '.')
{
*it = ',';
break;
}
else if (*it == ':')
*it = ';';
return s;
}
int main(int argc, const char** argv)
{
system("modprobe bcm2835-v4l2");
Mat frame;
int check;
if (!face_class.load(fileNameface)) { cout << "unable to load face classifier" << endl; }
if (!eye_class.load(fileNameeyes)) { cout << "unable to load eyes classifier" << endl; }
if (!capture.isOpened()) { cout << "unable to initiallize camera" << endl; }
else
{
auto start = std::chrono::steady_clock::now();
while (true)
{
capture >> frame;
if ((check = displayAndDetect(frame)) != 0)
{
cout << "hit!! " << endl;
auto duration = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start);
if (duration.count() >= 3)
break;
}
else
{
cout << "missed" << endl;
start = std::chrono::steady_clock::now();
}
int c = waitKey(1);
if (c == 27)
{
break;
}
}
std::string outputName = return_current_time_and_date() + ".avi";
cout << outputName << endl;
VideoWriter *videoWrite = new VideoWriter(outputName, -1, 10,
Size(FrameWidth, FrameHeight), true);
pthread_t id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&id, &attr, threadVideoRecord, videoWrite);
pthread_join(id, NULL);
}
return 0;
}
int displayAndDetect(Mat frame)
{
//.....Face Detection Function......//
}
void* threadVideoRecord(void *f)
{
Mat frame,frameGray;
VideoWriter *vid = (VideoWriter *) f;
int c;
while (true)
{
capture >> frame;
c = waitKey(10);
if (c == 27)
break;
cvtColor(frame, frameGray, CV_BGR2GRAY);
equalizeHist(frameGray, frameGray);
(*vid).write(frameGray);
imshow(window_name, frameGray);
}
videoWrite.release();
return NULL;
}
像它的前辈一样,systemd可以启动后台服务。 实际上这是正常的功能。 顾名思义,后台服务没有Windows。 如果将前台应用程序配置为后台服务,则只要前台应用程序尝试顶部打开其窗口,您就会收到一条错误消息-正是您所看到的。
如果要运行带有可见窗口的应用程序,则必须有一个登录用户。 如果您已经有一个登录用户,那么该用户的登录脚本必须已经运行。 这为您提供了从哪里开始前台应用程序的明显位置:在该登录脚本中。 Raspberry Pi支持许多不同的Linux发行版,因此请检查您正在运行的版本,以便了解Google。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.