[英]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.