Fps problem with Jetson Nano Gstreamer

I was trying to make a GUI for camera with jetson nano,

import cv2
import tkinter
from PIL import Image, ImageTk
import datetime, time
import shutil
import os, sys
import subprocess
import customtkinter
from tkinter import messagebox

customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark







cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
#cap = cv2.VideoCapture(0)

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

size = (frame_width, frame_height)



camon = False
relayon = False
recon = True
relay = 7 # relay pin
global result, startrec
startrec = False


window = customtkinter.CTk()
window.geometry("1150x830")
window.resizable(0,0)
window.title("My Camera Viewer")
img = tkinter.PhotoImage(file='mylogo.png')
window.tk.call('wm', 'iconphoto', window._w, img)

frame = tkinter.Frame(window, bg = "#84868a")
frame.place(x = 0, y = 0, width = 1150, height = 830) 


formTitle = tkinter.Label(window, text="My Camera Viewer",font=("Arial Baltic",28), bg = "#84868a")
formTitle.place(x = 140, y = 0, width = 850, height = 62)



def gstreamer_pipeline(
    capture_width=1280,
    capture_height=720,
    display_width=1280,
    display_height=720,
    framerate=30,
    flip_method=0,
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, 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"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )
def relayon():
    global relayon, recon
    if relayon:
        print("relayon")
        #GPIO.output(relay, GPIO.HIGH)
        onoff.config(bg="green")
        relayon = False
    else:
        if recon:
            print("relayoff")
            #GPIO.output(relay, GPIO.LOW)
            onoff.config(bg="red")
            relayon = True
        else:
             messagebox.showerror("Record error", "Record is still On Can't turn /n off camera at this point")

def reconoff():
    global recon, result, startrec
    if recon:
        if not relayon:
            print("record Starts")
            print(".....")
            now=datetime.datetime.now()
            print(".....")
            print('time',now)
            filename=str('./shots/vid_{}.avi'.format(str(now).replace(":",'')))
            print('filename',filename)
            result = cv2.VideoWriter(filename, cv2.VideoWriter_fourcc(*'XVID'),30, size)
            startrec = True
            print('result',result)
            recon = False
            rec.config(bg="green")
        else:
             messagebox.showerror("Camera error", "Camera is off Please turn on camera first")
    else:
        startrec = False
        print("record ends")
        #result.release()
        recon = True
        rec.config(bg="red")
        

def folder():
    print("folder opened")
    subprocess.Popen(r'explorer /select,".\shots\"')

def wipe():
    shutil.rmtree("./shots")
    os.mkdir('./shots')
    print("folder deleted")

   


def camera():
    global recon, result, startrec
    
    #window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
    #print("Video running")
    #print(size)
    # Window
    #while(1):
    ret_val, frame = cap.read()
    #cv2.imshow("CSI Camera", img)
    #result.write(frame)
    cv2image= cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    img = Image.fromarray(cv2image)
            # Convert image to PhotoImage
    imgtk = ImageTk.PhotoImage(image = img)
    vid.imgtk = imgtk
    vid.configure(image=imgtk)
            # Repeat after an interval to capture continiously
    vid.after(1, camera)
    #print(startrec)
    if startrec:
        result.write(frame)
        
        
        

    






vid = tkinter.Label(window, text = "Serching for video",font=("Arial",21),fg = "#ff0000",bg = "#000000", cursor = "circle", state = "normal")
vid.place(x = 30, y = 70, width = 1090, height = 602)

onoff = tkinter.Button(window, text = "ON/OFF", bg = onoffval, cursor = "arrow", state = "normal", command = relayon)
onoff.place(x = 50, y = 690, width = 230, height = 52)

rec = tkinter.Button(window, text = "REC", bg = recval,  cursor = "arrow", state = "normal", command = reconoff)
rec.place(x = 380, y = 690, width = 360, height = 52)

folder = tkinter.Button(window, text = "Folder", bg = "#ffffff",  cursor = "arrow", state = "normal", command = folder)
folder.place(x = 850, y = 690, width = 250, height = 52)

wipe = tkinter.Button(window, text = "Wipe Videos", bg = "#ffffff",  cursor = "arrow", state = "normal", command = wipe)
wipe.place(x = 470, y = 760, width = 200, height = 42)




camera()
window.mainloop()

The code is not finished, i’m facing a problem with fps…
i need 30 fps video, but the video i’m getting is so fast…
i don’t know if this is a logic problem or something else…
if anymore details needed… i can give…(first time…:) )

Moving this topic to the Jetson Nano category for better visibility.

Hi,
Since hardware converter does not support BGR, we would need software converter for converting BGRx to BGR. Please run sudo nvpmodel -m 0 and sudo jetson_clocks. The commands run CPU cores at maximum clock and bring optimal performance.

For comparison, can try gst-launch-1.0 command to check if fps is identical to OpenCV code:

gst-launch-1.0 -v nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)NV12, framerate=(fraction)30/1' ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! fpsdisplaysink text-overlay=0 video-sink=fakesink sync=0

I tried that command to run CPU cores at maximum clock speed… but that didn’t help with my code…
Do i need a powerful CPU for this small code???

Hi,
Do you see 30fps in gst-launch-1.0 command? On Jetson Nano, it should be able to achieve 30fps for 1280x720.

I’m getting smooth fps for streaming the frames directly from the camera, but low fps for recording the video… i try to save the 720p video with cv2.VideoWriter in 30fps, but video is so fast (not in realtime).

I tried this code with a USB webcam in my pc… It works perfectly…
so i’m guessing…
camera is giving 30fps, but jetson nano can’t able to process and save all the 30 frames in a second… maybe it can only process 10 frames in a second… so its just takes the 10 + 10 + 10 frames from 3 seconds and make it as a 30fps video… so a 3 second video will save as only 1 second…
is it???

Hi,
It is possible and performance may be capped by CPU capability. Please execute sudo tegrastats to check the system loading. See if CPU usage is close to 100% in running the sample.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.