Tkinter and Raspberry Pi Camera Module 3 Causing Pi5 to Lag After Some Time

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.

New contributor

Alex StAubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật