I am attempting to monitor a Pi Camera Module 3 with tkinter. When I start running my program it runs fine, but after a few hours of running the camera and entire Raspberry Pi 5 system begin to lag heavily. The second I terminate the program the system returns back to normal operation. Can someone please help me figure out what is causing the system to lag so this program can run indefinitely without lagging? I notice that Xwayland CPU usage begins to creep up to 100% as the program runs. I have monitored memory/cpu/interrupts/context switches none seem to be raise as the program runs. I have tried changing the nice value and priority of the program to -20 and neither seem to do anything. I have pasted my code below (app has a camera has a video capture). Any help would be much appreciated.
App.py:
import tkinter
from Camera import Camera
class App:
def __init__(self, window, window_title, video_name):
# window for app
self.window = window
self.window.title(window_title)
# add cameras
self.source = Camera(self.window, video_name, 1024, 768)
self.source.grid(row=0, column=0, sticky=tkinter.N)
# start window
self.window.protocol("WM_DELETE_WINDOW", self.delete)
self.window.mainloop()
def delete(self):
print('[App] stoping threads')
self.source.vid.running = False
print('[App] exit')
self.window.destroy()
if __name__ == '__main__':
App(tkinter.Tk(), "DAC Gas Loader Cameras", 'PI Camera')
Camera.py:
import tkinter as tk
import PIL.Image, PIL.ImageTk
from VideoCapture import *
class Camera(tk.Frame):
def __init__(self, window, name="", width=400, height=300, fps=24):
super().__init__(window)
self.window = window
# add video source
self.vid = PicamVideoCapture(width, height)
# webcam window
self.canvas = tk.Canvas(self, width=width, height=height)
self.canvas.pack()
# calculate delay for fps
self.fps = fps
self.delay = int(1000/self.fps)
print('[Camera] source:', 'fps:', self.fps, 'delay:', self.delay)
# image must persist in memory
self.image = None
self.imageID = None
# start thread for camera
self.running = True
target=self.update_frame()
def update_frame(self):
# get a frame
ret, frame = self.vid.get_frame()
# add frame to canvas
if ret:
self.image = PIL.Image.fromarray(frame)
self.photo = PIL.ImageTk.PhotoImage(image=self.image)
if self.imageID != None: self.canvas.delete(self.imageID)
self.imageID = self.canvas.create_image(0, 0, image=self.photo, anchor='nw')
# recurse on function after delay
time.sleep(0.01)
if self.running:
self.window.after(self.delay, self.update_frame)
def __del__(self):
# stop the camera
self.running = False
VideoCapture.py:
import time
import os
from picamera2 import Picamera2
class PicamVideoCapture:
def __init__(self, width=400, height=300):
self.vid = Picamera2()
self.vid.configure(self.vid.create_preview_configuration(main={"size": (width, height)}))
self.vid.start()
self.ret = False
self.frame = None
def get_frame(self):
return True, self.vid.capture_array()
def take_picture(self):
os.makedirs(os.path.dirname("./images/"), exist_ok=True)
self.vid.capture_file(time.strftime("images/picam-%d-%m-%Y-%H-%M-%S.jpg"))
Monitoring processes, noticed app stays around 35% cpu and 1.5% mem/Xwayland climbs from 15% cpu and 1% mem to 100% cpu and 1% mem. Changing priorities does nothing. Had threading so multiple cameras don’t interfere with one another but removed that, issue persists.
Alex StAubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.