0

I've written a Python script that collects video for a specific amount of time, while also showing a frame of the images being recorded. The script is intended to close at the conclusion of one video session and then immediately get restarted by a service file in /etc/systemd/system/videoPy.service.

[Unit]
Description = starts video recorder
After = multi-user.target

[Service]
Type = simple
ExecStart = /usr/bin/python3 /home/fred/videoPy.py
TimeoutStartSec = infinity
Restart = on-failure
RestartSec = 5
User = fred
Group = users

[Install]
WantedBy = multi-user.target

Python script is

#!/usr/bin/env python3
import cv2
import numpy as np
import time

# Create a VideoCapture object. Setting to "0" opens onboard camera. Using "2" connects to usb camera.
cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if (cap.isOpened() == False): 
  print("Unable to read camera feed")

# defines video length
capture_duration = 10

# Default resolutions of the frame are obtained.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

frame_per_sec = int('10')

# PROBLEM MAY BE BELOW. Makes an empty .avi, and that is it.
out = cv2.VideoWriter('/home/fred/videoPy_output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), (frame_per_sec), (frame_width,frame_height))

# Captures video for specified amount of time
start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
ret, frame = cap.read()
if ret==True:
    #frame = cv2.flip(frame,0)
    out.write(frame)
    cv2.imshow('frame',frame)
        # Press Q on keyboard to stop recording
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
else:
    break

# When everything done, release the video capture and video write objects
cap.release()
out.release()

# Closes all the frames
cv2.destroyAllWindows()

The Python script is in /home/fred/. When I open a terminal at that location and enter "python3 camera.py" it works great, a window pops up showing the camera image and a 10 second .avi file in /home/Fred/ is produced just as it should...however when I run "sudo systemctl enable cameraPy.service" followed by "service cameraPy start" there is no window popping up with the camera image, however .avi files are created every 10 seconds, but they has a size of zero and are malformed. Consequently, the service file does appears to be functioning.

Thoughts on this? I've reinstalled opencv-python, as well as time-python, and imutils via "pip install" with no luck. I suspect that the problem is related to when "cv2_videowriter" is called per comment above. Thanks in advance.

portsample
  • 1,986
  • 4
  • 19
  • 35
  • you forgot to tag `systemd`. I did that for you. I also removed OpenCV because this isn't an OpenCV issue. _maybe_ you need to make sure your script is allowed to access some peripherals (camera, display) -- consider that anything interacting with a graphical session **is not** a service but something else, which has to run under a graphical login. – Christoph Rackwitz Nov 30 '22 at 22:23
  • Th script runs fine when I open a terminal in /home/user/ and then do a python3 videoPy.py, however the script runs partially when being called from a /etc/systemd/system/videoPy.service. The .avi file is malformed and the frame with image does not show. – portsample Nov 30 '22 at 22:33
  • because services don't know what graphical session to interact with. that's not an OpenCV problem (debug it, it fails at the `imshow`) but an issue with how linux works fundamentally. you can't just "show stuff on the screen". you have to know what session to show it under... or talk to X directly, but even then I'm not sure you can just draw on the screen however you like, AND you'd need to have permissions to actually talk to X directly... from a service, which services are NEVER supposed to do. – Christoph Rackwitz Nov 30 '22 at 23:56
  • From the tag: systemd questions should be for *programming questions* using systemd or its libraries. Questions about *configuring the daemon* (including writing unit files) are better directed to Unix & Linux: https://unix.stackexchange.com. Please delete this. – Rob Dec 01 '22 at 12:53
  • This is already answered many times on both unix.stackexchange.com and superuser.com, with the summary being "services *by definition* have no GUI access and cannot display windows". – user1686 Dec 09 '22 at 13:48

0 Answers0