I’m working with a Jetson Orin Nano and a Raspberry Pi Camera Module V2, aiming to run YOLOv5 for real-time image processing. I’ve tried using the following code, but the camera feed is extremely slow and laggy, which I didn’t expect from this setup. How to improve this?
Here is my code:
import cv2
import torch
import numpy as np
# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# GStreamer pipeline for CSI camera
def gstreamer_pipeline(
capture_width=1920,
capture_height=1080,
display_width=960,
display_height=540,
framerate=30,
flip_method=2,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink drop=True"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
# Live video capture
cap = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
if not cap.isOpened():
print("Unable to open camera. Please check the connection.")
exit()
while cap.isOpened():
cv2.namedWindow("Detect", cv2.WINDOW_AUTOSIZE)
ret, frame = cap.read()
if not ret:
print("Unable to retrieve frame.")
break
# Make predictions using YOLOv5
results = model(frame)
cv2.imshow('YOLO', np.squeeze(results.render()))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Interestingly, when I use the following pipeline, the camera runs much faster:
import cv2
gst_str = "nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=1920, height=1080, framerate=30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink"
cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
if not cap.isOpened():
print("Unable to open camera")
else:
print("Camera opened")
while True:
ret, frame = cap.read()
if not ret:
print("Unable to retrieve frame")
break
cv2.imshow("CSI Camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I want to use YOLO for real-time image processing and eventually build a training algorithm. I’m open to suggestions for optimizing the current setup or switching to a different YOLO variant or any other approach suitable for the Jetson Orin Nano.