(self)
return self.tracker
Perform a setup to set all settings using a GUI
		
		arguments
		None
		
		keyword arguments
		None
		
		returns
		None			--	returns nothing, but does fill in
						the self.settings dict
Perform a setup to set all settings using a GUI
		
		arguments
		None
		
		keyword arguments
		None
		
		returns
		None			--	returns nothing, but does fill in
						the self.settings dict
Perform a setup to set all settings using a GUI arguments None keyword arguments None returns None -- returns nothing but does fill in the self . settings dict
def run_GUI(self):
		
		"""Perform a setup to set all settings using a GUI
		
		arguments
		None
		
		keyword arguments
		None
		
		returns
		None			--	returns nothing, but does fill in
						the self.settings dict
		"""
		
		# # # # #
		# variables
		
		# general
		stage = 1 # stage is updated by handle_input functions
		
		# stage specific
		stagevars = {}
		
		stagevars[0] = {}
		stagevars[0]['show_threshimg'] = False # False for showing snapshots, True for showing thresholded snapshots
		stagevars[0]['use_prect'] = True # False for no pupil search limits, True for pupul rect

		stagevars[1] =  {}
		stagevars[1]['thresholdchange'] = None # None, 'up', or 'down'
		
		stagevars[2] = {}
		stagevars[2]['clickpos'] = 0,0 # becomes a (x,y) tuple, indicating click position within the webcam's snapshots (to determine pupil rect)
		stagevars[2]['prectsize'] = 100,50 # pupilrectsize
		stagevars[2]['prect'] = pygame.Rect(stagevars[2]['clickpos'][0],stagevars[2]['clickpos'][1],stagevars[2]['prectsize'][0],stagevars[2]['prectsize'][1]) # rect around pupil, in which the pupil is expected to be
		stagevars[2]['vprectchange'] = None  # None, 'up', or 'down'
		stagevars[2]['hprectchange'] = None  # None, 'right', or 'left'
		
		stagevars[3] = {}
		stagevars[3]['confirmed'] = False

		# set Booleans
		running = True			# turns False upon quiting the GUI
		
		# set image variables
		imgsize = self.img.get_size()
		blitpos = (self.dispsize[0]/2-imgsize[0]/2, self.dispsize[1]/2-imgsize[1]/2)
		
		# # # # #
		# run GUI
		while running:
			
			# # # # #
			# general
			
			# draw stage
			self.draw_stage(stagenr=stage)
			
			# get new snapshot, thresholded image, and pupil measures (only use pupil bounding rect after stage 1)
			useprect = stagevars[0]['use_prect'] and stage > 1
			self.img, self.thresholded, pupilpos, pupilsize, pupilbounds = self.tracker.give_me_all(pupilrect=useprect)
			
			# update settings
			self.settings = self.tracker.settings
	
			# check if the thresholded image button is active
			if stagevars[0]['show_threshimg']:
				# draw active button
				self.draw_button(self.buttons['t']['active']['img'], self.buttons['t']['active']['pos'])
			# if threshold button is not active, draw inactive button
			else:
				self.draw_button(self.buttons['t']['inactive']['img'], self.buttons['t']['inactive']['pos'])
	
			# check if the thresholded image button is active
			if stagevars[0]['use_prect']:
				# draw active button
				self.draw_button(self.buttons['r']['active']['img'], self.buttons['r']['active']['pos'])
			# if threshold button is not active, draw inactive button
			else:
				self.draw_button(self.buttons['r']['inactive']['img'], self.buttons['r']['inactive']['pos'])

			# check for input
			inp, inptype = self.check_input()
			
			# handle input, according to the stage (this changes the stagevars!)
			stage, stagevars = self.handle_input(inptype, inp, stage, stagevars)
			
			# # # # #
			# stage specific
			
			# stage 1: setting pupil threshold
			if stage == 1:
				# set camera threshold
				if stagevars[1]['thresholdchange'] != None:
					if stagevars[1]['thresholdchange'] == 'up' and self.settings['threshold'] < 255:
						self.settings['threshold'] += 1
					elif stagevars[1]['thresholdchange'] == 'down' and self.settings['threshold'] > 0:
						self.settings['threshold'] -= 1
					stagevars[1]['thresholdchange'] = None
			
			# stage 2: select eye by clicking on it
			if stage == 2:
				# check if input is a mouse click
				if type(inp) in [tuple,list]:
					# check if mouse position is in image
					mpos = pygame.mouse.get_pos()
					hposok = mpos[0] > blitpos[0] and mpos[0] < blitpos[0]+imgsize[0]
					vposok = mpos[1] > blitpos[1] and mpos[1] < blitpos[1]+imgsize[1]
					if hposok and vposok:
						# set pupil position
						stagevars[2]['clickpos'] = inp[0]-blitpos[0], inp[1]-blitpos[1]
						self.settings['pupilpos'] = stagevars[2]['clickpos'][:]
						# set pupil rect
						x = stagevars[2]['clickpos'][0] - stagevars[2]['prectsize'][0]/2
						y = stagevars[2]['clickpos'][1] - stagevars[2]['prectsize'][1]/2
						stagevars[2]['prect'] = pygame.Rect(x,y,stagevars[2]['prectsize'][0],stagevars[2]['prectsize'][1])
						self.settings['pupilrect'] = stagevars[2]['prect']
				
				# if input was a key or button press
				elif stagevars[2]['vprectchange'] or stagevars[2]['hprectchange']:
					# change pupil rect size
					if stagevars[2]['vprectchange'] != None:
						if stagevars[2]['vprectchange'] == 'up':
							stagevars[2]['prectsize'] = stagevars[2]['prectsize'][0], stagevars[2]['prectsize'][1] + 1
						elif stagevars[2]['vprectchange'] == 'down':
							stagevars[2]['prectsize'] = stagevars[2]['prectsize'][0], stagevars[2]['prectsize'][1] - 1
						stagevars[2]['vprectchange'] = None
					if stagevars[2]['hprectchange'] != None:
						if stagevars[2]['hprectchange'] == 'right':
							stagevars[2]['prectsize'] = stagevars[2]['prectsize'][0] + 1, stagevars[2]['prectsize'][1]
						elif stagevars[2]['hprectchange'] == 'left':
							stagevars[2]['prectsize'] = stagevars[2]['prectsize'][0] - 1, stagevars[2]['prectsize'][1]
						stagevars[2]['hprectchange'] = None	
					# set pupil rect
					x = self.settings['pupilrect'][0]
					y = self.settings['pupilrect'][1]
					stagevars[2]['prect'] = pygame.Rect(x,y,stagevars[2]['prectsize'][0],stagevars[2]['prectsize'][1])
					self.settings['pupilrect'] = stagevars[2]['prect']

				# draw pupil rect
				pygame.draw.rect(self.img, (0,0,255), self.settings['pupilrect'], 2)
				pygame.draw.rect(self.thresholded, (0,0,255), self.settings['pupilrect'], 2)
			
			# stage 3: confirmation
			if stage == 3:
				# set camera threshold
				if stagevars[1]['thresholdchange'] != None:
					if stagevars[1]['thresholdchange'] == 'up' and self.settings['threshold'] < 255:
						self.settings['threshold'] += 1
					elif stagevars[1]['thresholdchange'] == 'down' and self.settings['threshold'] > 0:
						self.settings['threshold'] -= 1
					stagevars[1]['thresholdchange'] = None
				# draw pupil center and pupilbounds in image
				try: pygame.draw.rect(self.img, (0,255,0),pupilbounds,1); pygame.draw.rect(self.thresholded, (0,255,0),pupilbounds,1)
				except: print("pupilbounds=%s" % pupilbounds)
				try: pygame.draw.circle(self.img, (255,0,0),pupilpos,3,0); pygame.draw.circle(self.thresholded, (255,0,0),pupilpos,3,0)
				except: print("pupilpos=%s" % pupilpos)
				# is settings are confirmed, stop running
				if stagevars[3]['confirmed']:
					running = False

			# draw values
			starty = self.dispsize[1]/2 - imgsize[1]/2
			vtx = self.dispsize[0]/2 - imgsize[0]/2 - 10 # 10 isa
			vals = ['pupil colour',str(self.settings['pupilcol']), 'threshold', str(self.settings['threshold']), 'pupil position', str(self.settings['pupilpos']), 'pupil rect', str(self.settings['pupilrect'])]
			for i in range(len(vals)):
				# draw title
				tsize = self.sfont.size(vals[i])
				tpos = vtx-tsize[0], starty+i*20
				tsurf = self.sfont.render(vals[i], True, self.fgc)
				self.disp.blit(tsurf,tpos)
			
			# draw new image
			if stagevars[0]['show_threshimg']:
				self.disp.blit(self.thresholded, blitpos)
			else:
				self.disp.blit(self.img, blitpos)

			# update display
			pygame.display.flip()

			# apply settings
			self.tracker.settings = self.settings
		
			# DEBUG #
			if DEBUG:
				self.savefile.write(pygame.image.tostring(self.disp,'RGB')+BUFFSEP)
			# # # # #
		
		# DEBUG #
		if DEBUG:
			# close savefile
			self.savefile.close()
			# message
			print("processing images...")
			# open savefile
			savefile = open('data/savefile.txt','r')
			# read ALL contents in once, then close file again
			raw = savefile.read()
			savefile.close()
			# split based on newlines (this leaves one empty entry, because of the final newline)
			raw = raw.split(BUFFSEP)
			# process strings and save image
			for framenr in range(len(raw)-1):
				img = pygame.image.fromstring(raw[framenr],self.dispsize,'RGB')
			pygame.image.save(img,'data/frame%d.png' % framenr)
		# # # # #
		
		return self.tracker
def run_GUI ( self ) : # # # # # # variables # general stage = 1 # stage is updated by handle_input functions # stage specific stagevars = { } stagevars [ 0 ] = { } stagevars [ 0 ] [ 'show_threshimg' ] = False # False for showing snapshots, True for showing thresholded snapshots stagevars [ 0 ] [ 'use_prect' ] = True # False for no pupil search limits, True for pupul rect stagevars [ 1 ] = { } stagevars [ 1 ] [ 'thresholdchange' ] = None # None, 'up', or 'down' stagevars [ 2 ] = { } stagevars [ 2 ] [ 'clickpos' ] = 0 , 0 # becomes a (x,y) tuple, indicating click position within the webcam's snapshots (to determine pupil rect) stagevars [ 2 ] [ 'prectsize' ] = 100 , 50 # pupilrectsize stagevars [ 2 ] [ 'prect' ] = pygame . Rect ( stagevars [ 2 ] [ 'clickpos' ] [ 0 ] , stagevars [ 2 ] [ 'clickpos' ] [ 1 ] , stagevars [ 2 ] [ 'prectsize' ] [ 0 ] , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] ) # rect around pupil, in which the pupil is expected to be stagevars [ 2 ] [ 'vprectchange' ] = None # None, 'up', or 'down' stagevars [ 2 ] [ 'hprectchange' ] = None # None, 'right', or 'left' stagevars [ 3 ] = { } stagevars [ 3 ] [ 'confirmed' ] = False # set Booleans running = True # turns False upon quiting the GUI # set image variables imgsize = self . img . get_size ( ) blitpos = ( self . dispsize [ 0 ] / 2 - imgsize [ 0 ] / 2 , self . dispsize [ 1 ] / 2 - imgsize [ 1 ] / 2 ) # # # # # # run GUI while running : # # # # # # general # draw stage self . draw_stage ( stagenr = stage ) # get new snapshot, thresholded image, and pupil measures (only use pupil bounding rect after stage 1) useprect = stagevars [ 0 ] [ 'use_prect' ] and stage > 1 self . img , self . thresholded , pupilpos , pupilsize , pupilbounds = self . tracker . give_me_all ( pupilrect = useprect ) # update settings self . settings = self . tracker . settings # check if the thresholded image button is active if stagevars [ 0 ] [ 'show_threshimg' ] : # draw active button self . draw_button ( self . buttons [ 't' ] [ 'active' ] [ 'img' ] , self . buttons [ 't' ] [ 'active' ] [ 'pos' ] ) # if threshold button is not active, draw inactive button else : self . draw_button ( self . buttons [ 't' ] [ 'inactive' ] [ 'img' ] , self . buttons [ 't' ] [ 'inactive' ] [ 'pos' ] ) # check if the thresholded image button is active if stagevars [ 0 ] [ 'use_prect' ] : # draw active button self . draw_button ( self . buttons [ 'r' ] [ 'active' ] [ 'img' ] , self . buttons [ 'r' ] [ 'active' ] [ 'pos' ] ) # if threshold button is not active, draw inactive button else : self . draw_button ( self . buttons [ 'r' ] [ 'inactive' ] [ 'img' ] , self . buttons [ 'r' ] [ 'inactive' ] [ 'pos' ] ) # check for input inp , inptype = self . check_input ( ) # handle input, according to the stage (this changes the stagevars!) stage , stagevars = self . handle_input ( inptype , inp , stage , stagevars ) # # # # # # stage specific # stage 1: setting pupil threshold if stage == 1 : # set camera threshold if stagevars [ 1 ] [ 'thresholdchange' ] != None : if stagevars [ 1 ] [ 'thresholdchange' ] == 'up' and self . settings [ 'threshold' ] < 255 : self . settings [ 'threshold' ] += 1 elif stagevars [ 1 ] [ 'thresholdchange' ] == 'down' and self . settings [ 'threshold' ] > 0 : self . settings [ 'threshold' ] -= 1 stagevars [ 1 ] [ 'thresholdchange' ] = None # stage 2: select eye by clicking on it if stage == 2 : # check if input is a mouse click if type ( inp ) in [ tuple , list ] : # check if mouse position is in image mpos = pygame . mouse . get_pos ( ) hposok = mpos [ 0 ] > blitpos [ 0 ] and mpos [ 0 ] < blitpos [ 0 ] + imgsize [ 0 ] vposok = mpos [ 1 ] > blitpos [ 1 ] and mpos [ 1 ] < blitpos [ 1 ] + imgsize [ 1 ] if hposok and vposok : # set pupil position stagevars [ 2 ] [ 'clickpos' ] = inp [ 0 ] - blitpos [ 0 ] , inp [ 1 ] - blitpos [ 1 ] self . settings [ 'pupilpos' ] = stagevars [ 2 ] [ 'clickpos' ] [ : ] # set pupil rect x = stagevars [ 2 ] [ 'clickpos' ] [ 0 ] - stagevars [ 2 ] [ 'prectsize' ] [ 0 ] / 2 y = stagevars [ 2 ] [ 'clickpos' ] [ 1 ] - stagevars [ 2 ] [ 'prectsize' ] [ 1 ] / 2 stagevars [ 2 ] [ 'prect' ] = pygame . Rect ( x , y , stagevars [ 2 ] [ 'prectsize' ] [ 0 ] , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] ) self . settings [ 'pupilrect' ] = stagevars [ 2 ] [ 'prect' ] # if input was a key or button press elif stagevars [ 2 ] [ 'vprectchange' ] or stagevars [ 2 ] [ 'hprectchange' ] : # change pupil rect size if stagevars [ 2 ] [ 'vprectchange' ] != None : if stagevars [ 2 ] [ 'vprectchange' ] == 'up' : stagevars [ 2 ] [ 'prectsize' ] = stagevars [ 2 ] [ 'prectsize' ] [ 0 ] , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] + 1 elif stagevars [ 2 ] [ 'vprectchange' ] == 'down' : stagevars [ 2 ] [ 'prectsize' ] = stagevars [ 2 ] [ 'prectsize' ] [ 0 ] , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] - 1 stagevars [ 2 ] [ 'vprectchange' ] = None if stagevars [ 2 ] [ 'hprectchange' ] != None : if stagevars [ 2 ] [ 'hprectchange' ] == 'right' : stagevars [ 2 ] [ 'prectsize' ] = stagevars [ 2 ] [ 'prectsize' ] [ 0 ] + 1 , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] elif stagevars [ 2 ] [ 'hprectchange' ] == 'left' : stagevars [ 2 ] [ 'prectsize' ] = stagevars [ 2 ] [ 'prectsize' ] [ 0 ] - 1 , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] stagevars [ 2 ] [ 'hprectchange' ] = None # set pupil rect x = self . settings [ 'pupilrect' ] [ 0 ] y = self . settings [ 'pupilrect' ] [ 1 ] stagevars [ 2 ] [ 'prect' ] = pygame . Rect ( x , y , stagevars [ 2 ] [ 'prectsize' ] [ 0 ] , stagevars [ 2 ] [ 'prectsize' ] [ 1 ] ) self . settings [ 'pupilrect' ] = stagevars [ 2 ] [ 'prect' ] # draw pupil rect pygame . draw . rect ( self . img , ( 0 , 0 , 255 ) , self . settings [ 'pupilrect' ] , 2 ) pygame . draw . rect ( self . thresholded , ( 0 , 0 , 255 ) , self . settings [ 'pupilrect' ] , 2 ) # stage 3: confirmation if stage == 3 : # set camera threshold if stagevars [ 1 ] [ 'thresholdchange' ] != None : if stagevars [ 1 ] [ 'thresholdchange' ] == 'up' and self . settings [ 'threshold' ] < 255 : self . settings [ 'threshold' ] += 1 elif stagevars [ 1 ] [ 'thresholdchange' ] == 'down' and self . settings [ 'threshold' ] > 0 : self . settings [ 'threshold' ] -= 1 stagevars [ 1 ] [ 'thresholdchange' ] = None # draw pupil center and pupilbounds in image try : pygame . draw . rect ( self . img , ( 0 , 255 , 0 ) , pupilbounds , 1 ) pygame . draw . rect ( self . thresholded , ( 0 , 255 , 0 ) , pupilbounds , 1 ) except : print ( "pupilbounds=%s" % pupilbounds ) try : pygame . draw . circle ( self . img , ( 255 , 0 , 0 ) , pupilpos , 3 , 0 ) pygame . draw . circle ( self . thresholded , ( 255 , 0 , 0 ) , pupilpos , 3 , 0 ) except : print ( "pupilpos=%s" % pupilpos ) # is settings are confirmed, stop running if stagevars [ 3 ] [ 'confirmed' ] : running = False # draw values starty = self . dispsize [ 1 ] / 2 - imgsize [ 1 ] / 2 vtx = self . dispsize [ 0 ] / 2 - imgsize [ 0 ] / 2 - 10 # 10 isa vals = [ 'pupil colour' , str ( self . settings [ 'pupilcol' ] ) , 'threshold' , str ( self . settings [ 'threshold' ] ) , 'pupil position' , str ( self . settings [ 'pupilpos' ] ) , 'pupil rect' , str ( self . settings [ 'pupilrect' ] ) ] for i in range ( len ( vals ) ) : # draw title tsize = self . sfont . size ( vals [ i ] ) tpos = vtx - tsize [ 0 ] , starty + i * 20 tsurf = self . sfont . render ( vals [ i ] , True , self . fgc ) self . disp . blit ( tsurf , tpos ) # draw new image if stagevars [ 0 ] [ 'show_threshimg' ] : self . disp . blit ( self . thresholded , blitpos ) else : self . disp . blit ( self . img , blitpos ) # update display pygame . display . flip ( ) # apply settings self . tracker . settings = self . settings # DEBUG # if DEBUG : self . savefile . write ( pygame . image . tostring ( self . disp , 'RGB' ) + BUFFSEP ) # # # # # # DEBUG # if DEBUG : # close savefile self . savefile . close ( ) # message print ( "processing images..." ) # open savefile savefile = open ( 'data/savefile.txt' , 'r' ) # read ALL contents in once, then close file again raw = savefile . read ( ) savefile . close ( ) # split based on newlines (this leaves one empty entry, because of the final newline) raw = raw . split ( BUFFSEP ) # process strings and save image for framenr in range ( len ( raw ) - 1 ) : img = pygame . image . fromstring ( raw [ framenr ] , self . dispsize , 'RGB' ) pygame . image . save ( img , 'data/frame%d.png' % framenr ) # # # # # return self . tracker