Camera working slowly with YoloV5

Hello,

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. I’m at the beginning of the project and open to any suggestions or solutions.

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.

Thanks in advance for your help! (Can you help? - @proventusnova , @EduardoSalazar96 )

Hello @onurrcifcii,

Have you considered using DeepStream?

It is NVIDIA’s framework for deploying optimized models for their platforms.
There is Marco Luciano’s Repo were you can find easy to run Yolo examples with DeepStream documented step by step. In fact, on the link I shared, you should find the instructions for running YoloV5.

I would suggest you follow the instructions and get it running so you can evaluate the results. If you like what you see, then we could start tinking about designing a custom app to integrate DeepStream on a custom solution.

Would you be opposed to give that a try?

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

Hi @onurrcifcii

Could you please be sure that you are running pytorch for jetson like found in the following line jetson-containers/packages/l4t/l4t-pytorch at master · dusty-nv/jetson-containers · GitHub?

It could be a possibility that the pipeline is not accelerated for that reason.

Please, let us know the results!

Eduardo Salazar
Embedded SW Engineer at RidgeRun

Contact us: support@ridgerun.com
Developers wiki: https://developer.ridgerun.com/
Website: www.ridgerun.com

Hi,

It’s more recommended to use Deepstream SDK since it has optimized for the Jetson device.
You should be able to find some examples from the community.

Thanks.

Hello @proventusnova ,

I’m trying to install DeepStream on my Jetson Orin Nano, and I encountered some issues during the installation process. I downloaded the package from this link: DeepStream Download. The specific file I downloaded is named deepstream-7.0_7.0.0-1_arm64.deb.

Here are the issues I encountered:

  1. After downloading the file, I ran the following command to install it:
sudo dpkg -i deepstream-7.0_7.0.0-1_arm64.deb
onur@ubuntu:~$ sudo dpkg -i ~/Downloads/deepstream-7.0_7.0.0-1_arm64.deb
Selecting previously unselected package deepstream-7.0.
(Reading database ... 207649 files and directories currently installed.)
Preparing to unpack .../deepstream-7.0_7.0.0-1_arm64.deb ...
Unpacking deepstream-7.0 (7.0.0-1) ...
dpkg: dependency problems prevent configuration of deepstream-7.0:
 deepstream-7.0 depends on cuda-cudart-12-2 | cuda-cudart-11-7; however:
  Package cuda-cudart-12-2 is not installed.
  Package cuda-cudart-11-7 is not installed.
 deepstream-7.0 depends on cuda-cudart-dev-12-2 | cuda-cudart-dev-11-7; however:
  Package cuda-cudart-dev-12-2 is not installed.
  Package cuda-cudart-dev-11-7 is not installed.
 deepstream-7.0 depends on libnpp-12-2 | libnpp-11-7; however:
  Package libnpp-12-2 is not installed.
  Package libnpp-11-7 is not installed.
 deepstream-7.0 depends on libnpp-dev-12-2 | libnpp-dev-11-7; however:
  Package libnpp-dev-12-2 is not installed.
  Package libnpp-dev-11-7 is not installed.
 deepstream-7.0 depends on libcufft-12-2 | libcufft-11-7; however:
  Package libcufft-12-2 is not installed.
  Package libcufft-11-7 is not installed.
 deepstream-7.0 depends on libnvinfer8 (>= 8.0.0); however:
  Package libnvinfer8 is not installed.
 deepstream-7.0 depends on libnvparsers8 (>= 8.0.0); however:
  Package libnvparsers8 is not installed.
 deepstream-7.0 depends on libnvparsers-dev (>= 8.0.0); however:
  Package libnvparsers-dev is not installed.
 deepstream-7.0 depends on libnvonnxparsers8 (>= 8.0.0); however:
  Package libnvonnxparsers8 is not installed.
 deepstream-7.0 depends on libnvinfer-plugin8 (>= 8.0.0); however:
  Package libnvinfer-plugin8 is not installed.
 deepstream-7.0 depends on libyaml-cpp-dev (>= 0.6.2); however:
  Package libyaml-cpp-dev is not installed.

dpkg: error processing package deepstream-7.0 (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 deepstream-7.0

  1. During installation, I received dependency errors, specifically stating that packages such as cuda-cudart-12-2, cuda-cudart-dev-12-2, libnpp-12-2, libcufft-12-2, and various libnvinfer related packages were missing. These dependencies prevented the DeepStream installation from being configured properly.
  2. I attempted to install these dependencies using apt-get:
sudo apt-get install cuda-cudart-12-2 cuda-cudart-dev-12-2 libnpp-12-2 libnpp-dev-12-2 libcufft-12-2 libnvinfer8 libnvparsers8 libnvparsers-dev libnvonnxparsers8 libnvinfer-plugin8 libyaml-cpp-dev

However, I received errors stating that these packages are either missing or have no installation candidate.
4. I also tried to add the NVIDIA repository key using the following command:

sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64/3bf863cc.pub

But this resulted in a “No data” error.
5. Subsequently, I attempted to manually add the repository to /etc/apt/sources.list.d/nvidia-cuda.list with the following entry:

deb [signed-by=/usr/share/keyrings/nvidia-archive-keyring.gpg] https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64 /

Despite this, the missing dependency issue persists when trying to install the required packages.

onur@ubuntu:~$ dpkg -l | grep deepstream
iU  deepstream-7.0                               7.0.0-1                                           arm64        Nvidia DeepStreamSDK runtime libraries, development files and samples

I would greatly appreciate any guidance on how to proceed to resolve these dependency issues and complete the installation of DeepStream on my Jetson Orin Nano. Is there an updated repository or another approach I should consider for these dependencies?

Thank you for your assistance.

Hi @onurrcifcii

I recommend you to use the SDKManager. You can select the DeepStream installation from the GUI

In step two, you can enable the SDK Components, which will install the CUDA and other dependencies needed over an ethernet connection.

Could you please give it a try?

Please, let us know your results!

Regards.

Hello @onurrcifcii,

Eduardo is right, using SDK Manager to install DeepStream is the easiest way of sourcing the libraries.

To expand a bit more on it, you don’t need to fully re-flash the board, in fact, the board does not need to be in recovery mode for this. You can simply use SDK Manager to install the DeepStream package, as well as the Jetson SDK Components and Jetson Platform Services. You can uncheck the other items so you avoid fully re-flashing your board and loosing progress or important data.

If you select the proper items on SDK Manager, it will copy and install the required packages through ssh into your board.

Please let us know how it goes. Or if you are having issues with SDK Manager, please feel free to paste pictures of the result here, we might be able to help. Also, if SDK Manager is not an option, let us know so we can provide you with instructions on how to install the required libraries without it.

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

Hi @EduardoSalazar96 and @proventusnova ,

When i try, there was a error, Can you help me?

Hello @onurrcifcii,

That is strange, the board seems to be having issues with verifying APT Repos Availability.

What happens if you run the command SDK Manager is trying to run on your board?

sudo apt update && sudo apt check: sudo dpkg --audit

It might also be that during your tries of installing DeepStream yourself, something might have broke.

Let’s try running the commands and checking what is going on. If we are not able to go past the issue, there is always the chance to reinstall the complete system from scratch.

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

@proventusnova

Output is that;

onur@ubuntu:~$ sudo apt update && sudo apt check: sudo dpkg --audit
[sudo] password for onur: 
Hit:1 http://ports.ubuntu.com/ubuntu-ports jammy InRelease
Hit:2 https://packages.microsoft.com/repos/code stable InRelease               
Hit:3 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease             
Hit:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64  InRelease
Hit:5 http://ports.ubuntu.com/ubuntu-ports jammy-backports InRelease
Hit:6 https://download.docker.com/linux/ubuntu jammy InRelease      
Hit:7 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease 
Hit:8 https://repo.download.nvidia.com/jetson/common r36.4 InRelease
Hit:9 https://repo.download.nvidia.com/jetson/t234 r36.4 InRelease             
Ign:10 https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64  InRelease
Hit:11 https://repo.download.nvidia.com/jetson/ffmpeg r36.4 InRelease
Ign:12 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64  InRelease
Err:13 https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64  Release
  404  Not Found [IP: 152.199.20.126 443]
Hit:14 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64  Release
Reading package lists... Done
W: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
E: The repository 'https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64  Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/Release.gpg: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

Thanks,

Lets try then hitting skip on the Very System Readiness step on SDK Manager when it throws the error you are currently seeing.

Keep me posted.

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

@proventusnova
It finished in 1 second.

@onurrcifcii,

Looks good.

Can you try verifying DeepStream is installed?

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

@proventusnova I guess no, I think it said finish in 1 second without any loading.

onur@ubuntu:~$ dpkg -l | grep deepstream
iU  deepstream-7.0                               7.0.0-1                                           arm64        Nvidia DeepStreamSDK runtime libraries, development files and samples


@onurrcifcii,

Installing those packages is usually very fast.
SDK Manager already has everything downloaded on your system and all it needs to do is copy it over to your device.

What is the output of:

ls /opt/nvidia/deepstream/deepstream-7.0/samples/configs/deepstream-app

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

@proventusnova I didn’t wait anywhere while installing or setting up.

Here is the output;

onur@ubuntu:~$ ls /opt/nvidia/deepstream/deepstream-7.0/samples/configs/deepstream-app
config_infer_primary.txt
config_infer_primary.yml
config_infer_secondary_vehiclemake.txt
config_infer_secondary_vehiclemake.yml
config_infer_secondary_vehicletypes.txt
config_infer_secondary_vehicletypes.yml
config_inferserver_primary.txt
config_mux_source30.txt
config_mux_source4.txt
config_preprocess_sgie.txt
config_preprocess.txt
config_tracker_IOU.yml
config_tracker_NvDCF_accuracy.yml
config_tracker_NvDCF_max_perf.yml
config_tracker_NvDCF_perf.yml
config_tracker_NvDeepSORT.yml
config_tracker_NvSORT.yml
source1_csi_dec_infer_resnet_int8.txt
source1_usb_dec_infer_resnet_int8.txt
source2_1080p_dec_infer-resnet_demux_int8.txt
source2_csi_usb_dec_infer_resnet_int8.txt
source2_dewarper_test.txt
source30_1080p_dec_infer-resnet_tiled_display_int8.txt
source30_1080p_dec_infer-resnet_tiled_display_int8.yml
source30_1080p_dec_preprocess_infer-resnet_tiled_display_int8.txt
source4_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8.txt
source4_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8.yml
source4_1080p_dec_preprocess_infer-resnet_preprocess_sgie_tiled_display_int8.txt
source6_csi_dec_infer_resnet_int8.txt
sources_30.csv
sources_4.csv

And here is the log file;
SDKM_logs_JetPack_6.1_Linux_for_Jetson_Orin_Nano_modules_2024-10-15_19-00-45.zip (310.6 KB)

Perhaps a summary of what I have done before will help you:

I tried before to install DeepStream on my NVIDIA Jetson Orin Nano but encountered issues with missing dependencies and possibly outdated repository keys.Here’s a summary of what I tried regarding updating the CUDA Linux GPG Repository Key:

  1. I attempted to download the GPG key from NVIDIA’s repository using the following command:
wget https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64/3bf863cc.pub -O /usr/share/keyrings/nvidia-cuda-keyring.gpg

However, I received a 404 Not Found error, which indicates that the key might have been moved or no longer exists at that URL.
2. I also tried adding the key using apt-key , but it gave me a warning that apt-key is deprecated and the key could not be fetched.

sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/jetson/ubuntu2004/x86_64/3bf863cc.pub

Result: No data error.
3. I edited the /etc/apt/sources.list.d/nvidia-cuda.list to include the repository, but still faced issues with missing GPG keys when attempting to install CUDA-related packages.
I appreciate any help you can provide. @proventusnova @EduardoSalazar96

Now, A warning appeared above and said to write this, I wrote it and now I think it has been completely deleted :/

I tried the SDK manager again but it still didn’t work.

@onurrcifcii,

I believe you might already have successfully installed DeepStream.

Can you try running a test?

Are you following the git repo that I shared before?

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/

Hello @proventusnova @EduardoSalazar96

When I run the following command to compile the custom YOLO implementation in REPO:

make -C nvdsinfer_custom_impl_Yolo clean && make -C nvdsinfer_custom_impl_Yolo

I get this error:

make: Entering directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'
g++ -c  -o nvdsinfer_yolo_engine.o -Wall -std=c++11 -shared -fPIC -Wno-error=deprecated-declarations -I/opt/nvidia/deepstream/deepstream/sources/includes -I/usr/local/cuda-12.2/include nvdsinfer_yolo_engine.cpp
nvdsinfer_yolo_engine.cpp:28:10: fatal error: nvdsinfer_custom_impl.h: No such file or directory
   28 | #include "nvdsinfer_custom_impl.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:81: nvdsinfer_yolo_engine.o] Error 1
make: Leaving directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'

All terminal:

onur@ubuntu:~/Desktop/Project/Deep/yolov5/DeepStream-Yolo$ make -C nvdsinfer_custom_impl_Yolo clean && make -C nvdsinfer_custom_impl_Yolo
make: Entering directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'
rm -rf libnvdsinfer_custom_impl_Yolo.so
rm -rf nvdsinfer_yolo_engine.o nvdsinitinputlayers_Yolo.o nvdsparsebbox_Yolo.o utils.o yolo.o yoloPlugins.o layers/activation_layer.o layers/batchnorm_layer.o layers/channels_layer.o layers/convolutional_layer.o layers/deconvolutional_layer.o layers/implicit_layer.o layers/pooling_layer.o layers/reorg_layer.o layers/route_layer.o layers/sam_layer.o layers/shortcut_layer.o layers/slice_layer.o layers/upsample_layer.o nvdsparsebbox_Yolo_cuda.o yoloForward.o yoloForward_nc.o yoloForward_v2.o
make: Leaving directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'
make: Entering directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'
g++ -c  -o nvdsinfer_yolo_engine.o -Wall -std=c++11 -shared -fPIC -Wno-error=deprecated-declarations -I/opt/nvidia/deepstream/deepstream/sources/includes -I/usr/local/cuda-12.2/include nvdsinfer_yolo_engine.cpp
nvdsinfer_yolo_engine.cpp:28:10: fatal error: nvdsinfer_custom_impl.h: No such file or directory
   28 | #include "nvdsinfer_custom_impl.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:81: nvdsinfer_yolo_engine.o] Error 1
make: Leaving directory '/home/onur/Desktop/Project/Deep/yolov5/DeepStream-Yolo/nvdsinfer_custom_impl_Yolo'

Currently, I don’t have a deepstream folder in /opt/nvidia. I’ve checked, but the directory does not exist. Any suggestions on what might be wrong or how I can proceed to solve this issue?

If we can’t seem to fix it, I can prepare to reset the Orin nano, if you tell me how to do it. I hope I don’t have APT problems while resetting Orin Nano.

Hello @onurrcifcii,

Thanks for sharing the logs.

That is very interesting, given that a couple of comments ago you were able ls a folder inside /op/nvidia/deepstream, here:

https://forums.developer.nvidia.com/t/camera-working-slowly-with-yolov5/309377/17

Can you try re-running the same command and check the results?

ls /opt/nvidia/deepstream/deepstream-7.0/samples/configs/deepstream-app

Then can you also please run:

ls /opt/nvidia/deepstream/deepstream/sources/includes/

If those folders are not there, then we can try reseting the board, otherwise we can continue trying to use the DeepStream installation you might have.

regards,
Andrew
Embedded Software Engineer at ProventusNova
https://proventusnova.com/embedded-software-engineering-support/