I am having a problem with starting my C++ script automatically on my raspberry pi B+. I use a raspberry pi camera for this project. My script involves a facial detection on the first stage and a video recording on the second stage using openCv library. I put the video recording stage into a thread. I followed the instruction from this post to start my program automatically: https://www.raspberrypi.org/forums/viewtopic.php?t=138861 and the program does work until it reaches the video recording stage from which I get this error from the systemctl status log:
raspberrypi RASPCAMERA[2720]: Unable to init server: could not connect: connection refused. raspberrypi RASPCAMERA[2720]: (my_detection: 2720): Gtk-WARNING **: cannot open display: raspberrypi system[1]: startCam.service: main process exited, code-exited, status-1/FAILURE raspberrypi system[1] Unit startCam.service entered failed state.
Even though the face detection stage works, its response was quite slow. I have also tried rc.local or init.d method but none of them worked as well. I appreciate any helps.
This is my systemd unit:
[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
This is my C++ code script:
// 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, like its predecessors, can start background services. This is in fact the normal functionality. Background services, as the name suggests, do not have windows. If you configure a foreground application as if it's a background service, you get an error message as soon as the foreground application tries top open its window - exactly what you see.
If you want to run an application with a visible window, there has to be a logged-in user. If you've got a logged-in user, then the login script for that user must have run. This gives you the obvious place from where to start foreground applications: in that login script. The Raspberry Pi supports a number of different Linux distributions, so check what you're running so you know what to google.
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.