From ProventusNova DeveloperWiki
Revision as of 02:40, 14 July 2025 by Admin (talk | contribs) (Add Pre-requisites and Python Script sections)

About

This document shows how to capture images from the Nvidia Jetson Nano using Python

Pre-requisites

Before proceeding, the Nvidia Jetson Nano must be set up. To set up the Nvidia Jetson Nano, see Nvidia Jetson Nano developer setup

The following pre-requisites must be present:

Install the required Python and GStreamer packages in the host machine

Install the necessary Python and GStreamer packages on the Jetson Nano

sudo apt install python3-opencv python3-pip
pip3 install numpy
sudo apt install python3-gi gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0

Camera Module

A compatible camera with CSI (Camera Serial Interface) is required to connect to the port on the Jetson Nano.

Testing the Camera Module

To test if the camera module is working or not, execute the following command:

gst-launch-1.0 nvarguscamerasrc ! nvvidconv ! ximagesink

A window should pop up and show the output of the camera

Run Python Script

The script initializes a GStreamer pipeline to capture frames from the Jetson camera module, listens for new frames, and prints their resolution. See the script below.

import gi
import sys
import signal

gi.require_version('Gst', '1.0')
gi.require_version('GstApp', '1.0')
gi.require_version('GObject', '2.0')
from gi.repository import Gst, GObject, GLib

Gst.init(None)

# Graceful exit using GMainLoop
loop = GLib.MainLoop()

def signal_handler(sig, frame):
    print("Ctrl+C detected. Exiting...")
    loop.quit()

signal.signal(signal.SIGINT, signal_handler)

def on_new_sample(sink, data):
    sample = sink.emit("pull-sample")
    if sample:
        buf = sample.get_buffer()
        caps = sample.get_caps()
        width = caps.get_structure(0).get_value('width')
        height = caps.get_structure(0).get_value('height')
        print(f"Received frame of resolution: {width}x{height}")

        result, mapinfo = buf.map(Gst.MapFlags.READ)
        if result:
            frame_data = mapinfo.data
            # Optional: convert to numpy array
            # import numpy as np
            # np_frame = np.frombuffer(frame_data, dtype=np.uint8).reshape((height, width, 3))
            buf.unmap(mapinfo)
        return Gst.FlowReturn.OK
    return Gst.FlowReturn.ERROR

def main():
    pipeline_str = (
        "nvarguscamerasrc ! "
        "nvvidconv ! "
        "appsink name=appsink emit-signals=true max-buffers=1 drop=true"
    )

    pipeline = Gst.parse_launch(pipeline_str)
    appsink = pipeline.get_by_name("appsink")
    appsink.connect("new-sample", on_new_sample, None)

    ret = pipeline.set_state(Gst.State.PLAYING)
    if ret == Gst.StateChangeReturn.FAILURE:
        print("Unable to set the pipeline to the playing state.", file=sys.stderr)
        sys.exit(1)

    print("Running... Press Ctrl+C to stop.")
    try:
        loop.run()
    finally:
        pipeline.set_state(Gst.State.NULL)
        print("Pipeline stopped.")

if __name__ == '__main__':
    main()

Need Further Support?

📞 Book Consultation Call: Show Calendar!

📩 Contact Via Email: support@proventusnova.com

🌐 Visit Our Website: ProventusNova.com