Is it possible to restart the nvargus-daemon from within a python script in case an error happens with the camera. So that the python script can keep running while capturing video, without having to be stopped and run sudo systemctl restart nvargus-daemon
separately?
The code here is probably illustrative (however it does not work fully as explained below):
import cv2
import os
import time
import subprocess
def restart_nvargus_daemon():
print("Restarting nvargus-daemon...")
result = subprocess.run(["sudo", "systemctl", "restart", "nvargus-daemon"], capture_output=True, text=True)
if result.returncode == 0:
print("nvargus-daemon restarted successfully.")
else:
print(f"Failed to restart nvargus-daemon. Return code: {result.returncode}")
print(f"Error output: {result.stderr}")
return False
# Wait for the daemon to become active
for _ in range(15): # Try for 10 seconds
result = subprocess.run(["systemctl", "is-active", "nvargus-daemon"], capture_output=True, text=True)
if "active" in result.stdout:
print("nvargus-daemon is active.")
time.sleep(5)
return True
time.sleep(3)
print("nvargus-daemon failed to become active.")
return False
def create_capture():
return cv2.VideoCapture(
'nvarguscamerasrc ! '
'video/x-raw(memory:NVMM) , width=1920, height=1080, format=NV12, framerate=30/1 ! '
'nvvidconv flip-method=2 ! '
'video/x-raw, width=1920, height=1080, format=BGRx ! '
'videoconvert ! '
'video/x-raw, format=BGR ! '
'appsink'
)
print("Creating capture pipeline...")
cap = create_capture()
while cap.isOpened():
ret_val, img = cap.read()
if not ret_val:
print("Failed to get the frame from the camera.")
cap.release()
if not restart_nvargus_daemon():
print("Failed to restart nvargus-daemon, exiting...")
break
cap = create_capture()
continue
# Display the frame (optional)
cv2.imshow('Frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If I run sudo systemctl stop nvargus-daemon
and then run the above script, it says that it successfully restarts the nvargus-daemon and that it is active, however the capture won’t open:
$ sudo systemctl stop nvargus-daemon
$ python script.py
(Argus) Error FileOperationFailed: Connecting to nvargus-daemon failed: Connection refused (in src/rpc/socket/client/SocketClientDispatch.cpp, function openSocketConnection(), line 204)
(Argus) Error FileOperationFailed: Cannot create camera provider (in src/rpc/socket/client/SocketClientDispatch.cpp, function createCameraProvider(), line 106)
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:746 Failed to create CameraProvider
[ WARN:0] global /home/ubuntu/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp (1100) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Failed to get the frame from the camera.
Restarting nvargus-daemon...
nvargus-daemon restarted successfully.
nvargus-daemon is active.
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:746 Failed to create CameraProvider
[ WARN:0] global /home/ubuntu/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp (1100) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Failed to get the frame from the camera.
Restarting nvargus-daemon...
nvargus-daemon restarted successfully.
nvargus-daemon is active.
However once I ctrl+c out of that run, and then immediately run the python script again, it works! So it seems that the nvargus-daemon is restarted, however its not useable within the current script.