...

How to Build a Real-Time Video Classifier on NVIDIA Jetson Nano with Python

jetson nano real time image classification

Jetson Nano Real-Time Image Classification with OpenCV and GoogLeNet

Introduction

This tutorial shows how to build a real-time image classification pipeline on NVIDIA Jetson Nano.
You will use OpenCV for video capture and display, and Jetson Inference for accelerated deep learning inference.
The model used is GoogLeNet trained on ImageNet, enabling fast and accurate labeling of frames from a video file.
By the end, you will be able to read frames, move them to GPU memory, classify them, and overlay the predicted class on the video.

Here is a video for Jetson Nano Real Time Image Classification:

The link for the video : https://youtu.be/AgOdXB34zaA

You can find more Nvidia Jetson Nano tutorials here : https://eranfeit.net/how-to-classify-objects-using-jetson-nano-inference-and-opencv/

Full Code for Build a Real-Time Video Classifier on NVIDIA Jetson Nano with Python

### Import OpenCV for video I/O, frame processing, and on-screen drawing. import cv2 ### Import Jetson Inference high-level APIs for classification models. import jetson.inference ### Import Jetson Utils for CUDA memory interop with NumPy images. import jetson.utils  ### Load the video file using OpenCV’s VideoCapture. Replace the path with your local video if needed. cap = cv2.VideoCapture('/home/feitdemo/github/Jetson-Nano-Python/Wildlife.mp4') ### Set capture width to 1280 pixels for a 720p workflow with good real-time performance on Jetson Nano. cap.set(3,1280) ### Set capture height to 720 pixels to match the desired display resolution. cap.set(4,720)  ### Load a pretrained image classification network. "googlenet" maps to an ImageNet-trained model on Jetson. net = jetson.inference.imageNet("googlenet")  ### Keep processing frames while the capture is open. while cap.isOpened():     ### Read the next frame from the video. 're' is the success flag and 'img' is the frame array.     re, img = cap.read()      ### Convert the BGR frame from OpenCV to RGBA, which is expected before moving into CUDA memory.     frame_rgba = cv2.cvtColor(img,cv2.COLOR_BGR2RGBA)     ### Move the NumPy frame into GPU memory so Jetson Inference can run classification on the device.     cuda_frame = jetson.utils.cudaFromNumpy(frame_rgba)      ### Run classification on the current frame. Returns the predicted class ID and a confidence score.     class_id , confidence = net.Classify(cuda_frame)      ### Retrieve the human-readable class description for the predicted class ID.     class_desc = net.GetClassDesc(class_id)      ### If the prediction confidence is above 0.40, overlay the label text on the frame for the viewer.     if confidence > 0.4 :          cv2.putText(img, class_desc, (30,80), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0),3 )      ### Show the annotated frame in a window titled 'img'.     cv2.imshow('img',img)     ### Position the window at the top-left corner of the screen for convenience.     cv2.moveWindow('img',0,0)      ### Check for a 'q' key press to allow the user to quit the loop gracefully.     if cv2.waitKey(10) & 0xFF == ord('q'):         break  ### Release the video capture to free camera or file resources. cap.release() ### Destroy OpenCV windows to clean up GUI resources before exit. cv2.destroyAllWindows() 

You can find the full code here : https://ko-fi.com/s/7a72f61abe

Summary for Jetson Nano Real Time Image Classification :

You opened a video stream and standardized its resolution for consistent throughput.
You converted each frame to RGBA and transferred it into CUDA memory for GPU inference.
You ran GoogLeNet classification with Jetson Inference and drew the top label when confidence passed a threshold.
You displayed the live results and implemented a clean shutdown on user request.


Connect :

☕ Buy me a coffee — https://ko-fi.com/eranfeit

🖥️ Email : feitgemel@gmail.com

🌐 https://eranfeit.net

🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb

Enjoy,

Eran

error: Content is protected !!
Eran Feit