class App: def __init__(self, window, window_title, video_source=0): self.window = window self.window.title(window_title) self.window.bind('<Escape>', lambda e: self.quit()) self.video_source = video_source # open video stream self.vid = VideoStream(self.video_source) # create canvas self.canvas = tkinter.Canvas(window, width=self.vid.width, height=self.vid.height) self.canvas.pack() # radio buttons # self.display_mode = IntVar() # Radiobutton(self.window, text="Main", variable=self.display_mode, value=0).pack(anchor=S) # Radiobutton(self.window, text="Depth", variable=self.display_mode, value=1).pack(anchor=S) # Radiobutton(self.window, text="Bird's-eye view", variable=self.display_mode, value=2).pack(anchor=S) # info elements # self.danger_scale_img = PIL.Image.open("assets/images/danger_scale_1.png") # self.danger_scale_img = PIL.ImageTk.PhotoImage(self.danger_scale_img) # self.danger_scale = Label(self.window, image=self.danger_scale_img) # self.danger_scale.pack(anchor=S) # build models # print("[INFO] loading activity model...") # self.activity_model = load_mobilenet_v2_distill() # variables self.cnt = 0 # self.classes = load_classes() # self.COUNTER = 0 # self.ALARM_ON = False # self.ear_states = deque(maxlen=50) # self.activity_states = deque(maxlen=10) # self.danger_scale_value_ear = 1 # self.danger_scale_value_activity = 1 # self.engine = pyttsx3.init() # self.engine.setProperty('volume', 1.0) # settings self.delay = 5 self.update() self.window.protocol("WM_DELETE_WINDOW", self.quit) self.window.mainloop() def update(self): ret, frame = self.vid.get_frame() if not ret: return # danger scale # danger_scale_value = self.danger_scale_value_ear # self.danger_scale_img = PIL.Image.open("assets/images/danger_scale_{}.png".format(danger_scale_value)) # self.danger_scale_img = PIL.ImageTk.PhotoImage(self.danger_scale_img) # self.danger_scale.configure(image=self.danger_scale_img) # show self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW) self.cnt += 1 self.window.after(self.delay, self.update) def sound_alarm(self, path): playsound.playsound(path) self.engine.say("Please! Watch the road!") self.engine.runAndWait() playsound.playsound(path) def quit(self): if self.vid: del self.vid print("[INFO] Stream closed") self.window.destroy() self.engine.stop()
class App: def __init__(self, window, window_title, activity_model_, face_model, video_source=0): # video stream self.window = window self.window.title(window_title) self.window.bind('<Escape>', lambda e: self.quit()) self.video_source = video_source self.vid = VideoStream(self.video_source) # ui elements self.canvas = tkinter.Canvas(window, width=self.vid.width, height=self.vid.height) self.canvas.pack() self.activity_label_text_var = StringVar() self.activity_label_template = "Activity: {}" self.activity_label_text_var.set( self.activity_label_template.format(' ')) self.activity_label = Label(self.window, textvariable=self.activity_label_text_var) self.activity_label.pack(anchor=SW) self.drowsiness_label_text_var = StringVar() self.drowsiness_label_template = "Eye ratio: {}%" self.drowsiness_label_text_var.set( self.drowsiness_label_template.format(' ')) self.drowsiness_label = Label( self.window, textvariable=self.drowsiness_label_text_var) self.drowsiness_label.pack(anchor=SW) self.danger_scale_img = PIL.Image.open( "assets/images/danger_scale_1.png") self.danger_scale_img = PIL.ImageTk.PhotoImage(self.danger_scale_img) self.danger_scale = Label(self.window, image=self.danger_scale_img) self.danger_scale.pack(anchor=S) # system self.driver_monitor = DriverMonitor({ 'activity_model_': activity_model_, 'face_model': face_model }) # settings self.delay = 5 self.update() self.window.protocol("WM_DELETE_WINDOW", self.quit) self.window.mainloop() def update(self): # get frame ret, frame = self.vid.get_frame() if not ret: return # run monitor result = self.driver_monitor.process(frame) danger_scale_value = result['danger_scale_value'] frame = result['frame'] self.drowsiness_label_text_var.set( self.drowsiness_label_template.format(result['ear'])) self.activity_label_text_var.set( self.activity_label_template.format(result['activity_class'])) # danger scale self.danger_scale_img = PIL.Image.open( "assets/images/danger_scale_{}.png".format(danger_scale_value)) self.danger_scale_img = PIL.ImageTk.PhotoImage(self.danger_scale_img) self.danger_scale.configure(image=self.danger_scale_img) # show self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW) self.window.after(self.delay, self.update) def quit(self): if self.vid: del self.vid print("[INFO] Stream closed") self.window.destroy() self.driver_monitor.quit()