def start(self, video_thread): """Create GUI""" # Store camera thread self.cameraThread = video_thread logging.info( "Link to thread that delivers video frames was stored in GUI thread" ) # Create Window self.main_window = MainWindow(self, self.cameraThread, self.root) logging.info('Main window was created') # If we are under windows, we need an OpenCV window so that waitKey() works... if platform == "win32": img = np.zeros((150, 600, 3), np.uint8) cv2.putText(img, 'Please do not close this window', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.putText(img, ' (minimizing is okay!) ', (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.namedWindow("win") cv2.imshow("win", img) # Start Tkinter thread if settings.determine_if_under_testing() is False: logging.info('Starting TkInter main loop') self.root.mainloop() # If using tests, quit the Tkinter thread after some time so that the mainloop is halted else: self.root.after(2000, self.root.quit) self.root.mainloop()
def __init__(self): # Define variables for function filterWaveform() self.value_last_running_max = -np.inf self.counter_running_max = 0 self.time_diff = None self.max_val_list = np.array([]) # Define variables for function estimate_trigger() self.delta_times = np.zeros(30) # Get time for trigger algorithm self.curr_time = datetime.datetime.now() # Create serial interface thread: if settings.determine_if_under_testing(): self.serial_interface = serial_interface.SerialInterface('') else: self.serial_interface = serial_interface.SerialInterface( '/dev/ttyUSB0') # Start serial interface thread self.serial_interface.start()
def __quit(self): # End program logging.info("User pressed ''quit'' button - now halting threads") # Close threads running for signal display and processing self.signalDisplayInstance.closeThreads() logging.info("Signal display thread was closed") # If camera connection is active, close it self.cameraInstance.close_camera_thread() logging.info("Camera capture thread was closed") # Close GUI self.root.quit() logging.info("Tk mainloop() was halted") # Debug: Store all still running threads logging.debug(threading.enumerate()) # Exit program if settings.determine_if_under_testing() is False: logging.info("Program will halt now...") sys.exit()
def __create_gui(self): """Create GUI elements and add them to root widget""" # Create frame that contains all following elements self.button_frame = Tk.Frame(root, width=500, height=100) self.button_frame.pack(side=Tk.BOTTOM) # Add Checkbutton to decide whether to use Viola-Jones algorithm or manual ROI definition curr_settings, _ = settings.get_parameters() self.check_button_1 = Tk.Checkbutton( master=self.button_frame, text="Face Detection", command=lambda: self.__enable_or_disable_viola_jones_algorithm()) self.check_button_1.pack(side=Tk.LEFT) # Add empty box # Todo: use a dynamic solution self.label_x0 = Tk.Label(self.button_frame, text=" ") self.label_x0.pack(side=Tk.LEFT) # Fill list with available cameras and add to menu self.label_color_channels = Tk.Label(self.button_frame, text="Color channel:") self.label_color_channels.pack(side=Tk.LEFT) list_color_channels = [' ', 'R', 'G', 'B'] list_color_channels.pop(0) self.list_color_channelsStr = Tk.StringVar() self.dropDownListColorChannel = Tk.OptionMenu( self.button_frame, self.list_color_channelsStr, *list_color_channels) self.list_color_channelsStr.set(list_color_channels[0]) self.dropDownListColorChannel.pack(side=Tk.LEFT) # Add empty box self.label_x0 = Tk.Label(self.button_frame, text=" ") self.label_x0.pack(side=Tk.LEFT) # Add text boxes for ROI definition self.label_x1 = Tk.Label(self.button_frame, text="X Begin:") self.label_x1.pack(side=Tk.LEFT) self.textbox_x1 = Tk.Text(self.button_frame, width=6, height=1) self.textbox_x1.pack(side=Tk.LEFT) if settings.determine_if_under_testing() is False: self.textbox_x1.insert(Tk.END, self.x_min) self.label_x2 = Tk.Label(self.button_frame, text="X End:") self.label_x2.pack(side=Tk.LEFT) self.textbox_x2 = Tk.Text(self.button_frame, width=6, height=1) self.textbox_x2.pack(side=Tk.LEFT) if settings.determine_if_under_testing() is False: self.textbox_x2.insert(Tk.END, self.x_max) self.label_y1 = Tk.Label(self.button_frame, text="Y Begin:") self.label_y1.pack(side=Tk.LEFT) self.textbox_y1 = Tk.Text(self.button_frame, width=6, height=1) self.textbox_y1.pack(side=Tk.LEFT) if settings.determine_if_under_testing() is False: self.textbox_y1.insert(Tk.END, self.y_min) self.label_y2 = Tk.Label(self.button_frame, text="Y End:") self.label_y2.pack(side=Tk.LEFT) self.textbox_y2 = Tk.Text(self.button_frame, width=6, height=1) self.textbox_y2.pack(side=Tk.LEFT) if settings.determine_if_under_testing() is False: self.textbox_y2.insert(Tk.END, self.y_max) # Add empty box self.label_x0 = Tk.Label(self.button_frame, text=" ") self.label_x0.pack(side=Tk.LEFT) # Add button for option menu self.button_options = Tk.Button(self.button_frame, text="Options", width=5, command=self.__open_options_menu) self.button_options.pack(side=Tk.LEFT) # Disable text boxes when Viola-Jones algorithm is active if curr_settings[IDX_FACE]: self.check_button_1.toggle() self.textbox_x1.config(bg='lightgray') self.textbox_x2.config(bg='lightgray') self.textbox_y1.config(bg='lightgray') self.textbox_y2.config(bg='lightgray')
def __show_image(self): """Get frame from camera and display it""" # Get current settings self.curr_settings, _ = settings.get_parameters() # Get current frame self.isTrueFrame, self.frame = self.cameraInstance.get_frame() # Check if first frame is received if self.isTrueFrame & self.first_frame: # Disable RGB selection button self.roiToolbarInstance.disable_color_channel_selection_and_options( ) # If first frame from camera is received store dimensions x_max = np.size(self.frame, 0) y_max = np.size(self.frame, 1) self.roiToolbarInstance.set_roi(0, x_max, 0, y_max) self.first_frame = False self.frameCounter += 1 logging.info( "First frame from webcam was received and ROI was adjusted") # If first frame from camera is received and the user wants to store frames, create folder if self.curr_settings[IDX_FRAMES]: self.directory = os.path.join( os.getcwd(), 'data', datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S')) # for window compatibility, colon has been replaced by dot os.makedirs(self.directory) logging.info('Folder was created for storing frames') # Process all following frames elif self.isTrueFrame: # Increase frame counter self.frameCounter += 1 # Use Viola Jones Algorithm for Face Detection if self.curr_settings[IDX_FACE]: frame_bw = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY) faces = self.faceCascade.detectMultiScale( frame_bw, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) for (x, y, w, h) in faces: cv2.rectangle(self.frame, (x, y), (x + w, y + h), (0, 255, 0), 2) self.roiToolbarInstance.set_roi(y, y + h, x, x + w) # Otherwise: Use manual ROI input else: x_min, x_max, y_min, y_max = self.roiToolbarInstance.get_roi() cv2.rectangle(self.frame, (y_min, x_min), (y_max, x_max), (0, 255, 0), 2) # Store frame on hard disk if self.curr_settings[IDX_FRAMES]: file_name = "frame%d.jpg" % self.frameCounter cv2.imwrite(os.path.join(self.directory, file_name), cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)) # Display icon current_location = os.path.dirname(os.path.realpath(__file__)) + os.sep # Heart icon if self.curr_settings[IDX_ALGORITHM] in (0, 2): # Add heart icon heart_location = current_location + 'data/heart.png' self.frame = self.__add_figure_to_frame(self.frame, heart_location) # Add text that displays Heart Rate cv2.putText(self.frame, self.HeartRateText, (25, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) # Heartbeat icon elif self.curr_settings[IDX_ALGORITHM] == 1: # If signal processing algorithm set trigger event if self.eventShowTrigger.is_set(): # Counter used to decide how long icon is shown self.counterShownTriggerSymbol += 1 # Add heart icon heart_location = current_location + 'data/heartbeat.png' self.frame = self.__add_figure_to_frame( self.frame, heart_location) # Clear event if symbol has been shown for approx 1/3 sec if self.counterShownTriggerSymbol >= self.FPS / 3: self.counterShownTriggerSymbol = 0 self.eventShowTrigger.clear() # Display frame if settings.determine_if_under_testing() is False: self.frameConverted = Image.fromarray(self.frame) self.imgTK = ImageTk.PhotoImage(image=self.frameConverted) self.lmain.imgtk = self.imgTK self.lmain.configure(image=self.imgTK) # Update values in statusbar self.statusbarInstance.set_frame_counter(self.get_frame_counter()) self.statusbarInstance.set_fps_counter(self.FPS) # Repeat thread immediately self.video_frame.after(1, lambda: self.__show_image())