looping kiosk tkinter app with cv2

asked 2020-04-13 11:16:34 -0600

m.do gravatar image

updated 2020-04-13 21:53:17 -0600

supra56 gravatar image

I made this app to save cam snapshots whenever the snap shot button is pressed however when i circle back to the welcome page and then to the cam page , i get this error:

cv2image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGBA)
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

i researched everywhere but couldnt find a way todo this. is it a limitation in opencv or tkinter?

i suspect the error could be because the previous snapshot is not cleared from cv2image but i dont know. what is the best way to make the app reset when welcome screen is called?

Attached is a working demonstration of the code the issue appears when the frontpage is recalled i get an error or sometimes just with the previous snapshot stuck on the frame screen.

how to make the app continue to work without exiting and reentering the app?

import PIL
import tkinter as tk
from PIL import Image, ImageTk
import cv2
import time

class FR_GUI(tk.Tk):
    """this is the app
    it inherits from tk.Tk
    """
    def __init__(self):
        super().__init__()
        #self.title = "Self Registration Kiosk"
        self.geometry("700x1050")
        # self.attributes("-fullscreen", True)
        # self.wm_attributes("-topmost", 1)

        self.home = WelcomeScreen(self)
        self.home.pack(expand=True,fill=tk.BOTH)
        self.front = FrontPage(self)
        self.second = SecondPage(self)

    def welcome_screen(self):
        self.front.pack_forget()
        self.second.pack_forget()
        self.home.pack(expand=True,fill=tk.BOTH)

    def show_first(self):
        self.home.pack_forget()
        self.second.pack_forget()
        self.front.pack(expand=True, fill=tk.BOTH)
        self.front.show_frame()

    def show_second(self):
        self.front.cap.release()
        self.front.pack_forget()
        self.home.pack_forget()
        self.second.pack(expand=True, fill=tk.BOTH)

class WelcomeScreen (tk.Frame):
    def __init__(self, master):
        self.master = master
        super().__init__(self.master)
        #self.input = tk.StringVar()

        self.mainframe = tk.Frame(self)
        self.mainframe.pack(side=tk.TOP,padx=5,pady=80)#expand=True, fill=tk.BOTH)

        self.name_label = tk.Label(
            self.mainframe,
            text="Welcome to the self-registration portal ",
            font=("Courier", 18),
        )

        self.name_label.pack()

        self.ent_btn = tk.Button(
            self.mainframe,
            text="Enter",
            font=("Courier", 15),
            command=self.close_welcome)

        self.ent_btn.pack()

    def close_welcome(self):
        self.master.show_first()

class FrontPage(tk.Frame):
    """This is a class to make the front page
    it inherits from tk.Frame
    """

    def __init__(self, master):
        self.master = master
        super().__init__(self.master)

        self.mainframe = tk.Frame(self)
        self.mainframe.pack(side=tk.TOP,padx=5,pady=20,expand=True, fill=tk.BOTH)

        self.name_label = tk.Label(
            self.mainframe,
            text="Press Snapshot, picture will be taken in: ",
            font=("Courier", 13),
        )
        self.name_label.pack()

        self.label_count = tk.Label(
            self.mainframe,
            )
        self.label_count.pack()

        self.lmain = tk.Label(
            self.mainframe,
            anchor=tk.CENTER
            #text='cam frame'
        )
        self.lmain.pack()

        self.ent_btn = tk.Button(
            self.mainframe,
            text="Take Snapshot",
            font=("Courier", 15),
            command=self.onClick)#self.start_cam)

        self.ent_btn.pack(side = 'bottom')

        self.video_source = 0
        self.cap = cv2.VideoCapture(self.video_source)

        #get cam width & height
        self.width ...
(more)
edit retag flag offensive close merge delete