import PIL.Image import PIL.ImageTk import tkinter as tk from constants import PROJECT_VIEW_NAME, FONT, PROJECTS_VIEW_NAME from gestures.gesture_recognition import MyVideoCapture from views.abstract_view import AbstractView class ProjectView(tk.Frame, AbstractView): def __init__(self, parent, controller, main_view_controller): tk.Frame.__init__(self, parent) self.token = '' self.window = controller self.main_view_controller = main_view_controller self.delay = 20 self.vid = None self.canvas = None self.index_label = None self.middle_label = None self.ring_label = None self.pinky_label = None self.back_button = None @staticmethod def get_view_name() -> str: return PROJECT_VIEW_NAME def start(self): self.vid = MyVideoCapture() self.canvas = tk.Canvas(self, width=self.vid.width, height=self.vid.height) self.canvas.pack() self.index_label = tk.Label(self, font=FONT) self.index_label.pack(anchor=tk.CENTER, expand=True) self.middle_label = tk.Label(self, font=FONT) self.middle_label.pack(anchor=tk.CENTER, expand=True) self.ring_label = tk.Label(self, font=FONT) self.ring_label.pack(anchor=tk.CENTER, expand=True) self.pinky_label = tk.Label(self, font=FONT) self.pinky_label.pack(anchor=tk.CENTER, expand=True) self.back_button = tk.Button(self, text="Back", font=FONT, command=lambda: self.back_to_projects_view()) self.back_button.pack() self.update() def update(self): # Get a frame from the video source if self.vid is not None: fingers, frame, success = self.vid.get_frame() self.index_label['text'] = fingers['index'] self.middle_label['text'] = fingers['middle'] self.ring_label['text'] = fingers['ring'] self.pinky_label['text'] = fingers['pinky'] if success: self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) self.window.after(self.delay, self.update) def back_to_projects_view(self): self.main_view_controller.show_frame(PROJECTS_VIEW_NAME) self.vid.release() self.destroy_components() def destroy_components(self): self.vid = None self.canvas.destroy() self.index_label.destroy() self.middle_label.destroy() self.ring_label.destroy() self.pinky_label.destroy() self.back_button.destroy()