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…:) )