Example #1
0
    def test_ASEFEyeLocalization(self):
        '''FilterEyeLocator: Scrapshots Both10 rate == 0.4800...............'''
        ilog = None
        if 'ilog' in globals().keys():
            ilog = globals()['ilog']

        # Load a face database
        ssdb = ScrapShotsDatabase()

        # Create a face detector
        face_detector = CascadeDetector()

        # Create an eye locator
        eye_locator = FilterEyeLocator()

        # Create an eye detection test
        edt = EyeDetectionTest(name='asef_scraps')

        #print "Testing..."
        for face_id in ssdb.keys()[:25]:
            face = ssdb[face_id]
            im = face.image

            dist = face.left_eye.l2(face.right_eye)
            dist = np.ceil(0.1 * dist)
            im.annotateCircle(face.left_eye, radius=dist, color='white')
            im.annotateCircle(face.right_eye, radius=dist, color='white')

            # Detect the faces
            faces = face_detector.detect(im)

            # Detect the eyes
            pred_eyes = eye_locator(im, faces)
            for rect, leye, reye in pred_eyes:
                im.annotateRect(rect)
                im.annotateCircle(leye, radius=1, color='red')
                im.annotateCircle(reye, radius=1, color='red')

            truth_eyes = [[face.left_eye, face.right_eye]]

            pred_eyes = [[leye, reye] for rect, leye, reye in pred_eyes]

            # Add to eye detection test
            edt.addSample(truth_eyes, pred_eyes, im=im, annotate=True)
            if ilog != None:
                ilog.log(im, label='test_ASEFEyeLocalization')

        edt.createSummary()

        # Very poor accuracy on the scrapshots database
        self.assertAlmostEqual(edt.face_rate, 1.0000, places=3)
        self.assertAlmostEqual(edt.both25_rate, 0.8800, places=3)
        self.assertAlmostEqual(edt.both10_rate, 0.5200, places=3)
        self.assertAlmostEqual(edt.both05_rate, 0.2800, places=3)
Example #2
0
import cv2
from pyvision.face.CascadeDetector import CascadeDetector
from pyvision.face.FilterEyeLocator import FilterEyeLocator

def mouseCallback(event, x, y, flags, param):
    if event in [cv2.EVENT_LBUTTONDOWN,cv2.EVENT_LBUTTONUP]:
        print("Mouse Event:",event,x,y)

if __name__ == '__main__':
    
    # Setup the webcam
    webcam  = pv.Webcam(size=(640,360))
    
    # Setup the face and eye detectors
    cd = CascadeDetector(min_size=(100,100))
    el = FilterEyeLocator()
    
    # Setup the mouse callback to handle mause events (optional)
    cv2.namedWindow("PyVision Live Demo")
    cv2.setMouseCallback("PyVision Live Demo", mouseCallback)
    
    while True:
        # Grab a frame from the webcam
        frame = webcam.query()
        
        # Run Face and Eye Detection
        rects = cd(frame)
        eyes = el(frame,rects)

        # Annotate the result
        for rect,leye,reye in eyes:
Example #3
0
    def __init__(self,parent,id,name,size=(640,672)):
        '''
        Create all the windows and controls used for the window and 
        '''
        wx.Frame.__init__(self,parent,id,name,size=size)
        
        self.CenterOnScreen(wx.HORIZONTAL)
        self.timing_window = None # Initialize timing window
        
        # ------------- Face Processing -----------------
        self.face_detector = CascadeDetector(cascade_name=CASCADE_NAME,image_scale=0.5)
        self.fel = FilterEyeLocator(FEL_NAME)
        self.face_rec = SVMFaceRec()
        
        self.svm_mode  = SVM_AUTOMATIC
        self.svm_C     = 4.000e+00
        self.svm_Gamma = 9.766e-04
        
        self.current_faces = []
        self.enrolling    = None
        self.enroll_count = 0
        self.enroll_max   = 32
        self.enroll_list  = []
        
        self.previous_time = time.time()
        
        self.arialblack24 = PIL.ImageFont.truetype(ARIAL_BLACK_NAME, 24)

        # ---------------- Basic Data -------------------
        try:
            self.webcam = Webcam()
        except SystemExit:
            raise
        except:
            trace = traceback.format_exc()
            message = TraceBackDialog(None, "Camera Error", CAMERA_ERROR, trace)
            message.ShowModal()
            
            sys.stderr.write("FaceL Error: an error occurred while trying to connect to the camera.  Details follow.\n\n")
            sys.stderr.write(trace)
            sys.exit(CAMERA_ERROR_CODE)

        # ------------- Other Components ----------------
        self.CreateStatusBar()
        
        # ------------------- Menu ----------------------
        # Creating the menubar.
        
        # Menu IDs
        license_id = wx.NewId()
        
        mirror_id = wx.NewId()
        face_id = wx.NewId()
        svm_tune_id = wx.NewId()
        performance_id = wx.NewId()
        
        # Menu Items
        self.file_menu = wx.Menu();

        self.file_menu.Append( wx.ID_ABOUT, "&About..." )
        self.file_menu.Append( license_id, "FaceL License..." )
        self.file_menu.AppendSeparator();
        self.file_menu.Append( wx.ID_EXIT, "E&xit" )

        self.options_menu = wx.Menu();
        self.face_menuitem = self.options_menu.AppendCheckItem( face_id, "Face Processing" )
        self.eye_menuitem = self.options_menu.AppendCheckItem( face_id, "Eye Detection" )
        self.mirror_menuitem = self.options_menu.AppendCheckItem( mirror_id, "Mirror Video" )
        self.options_menu.AppendSeparator()
        self.options_menu.Append( svm_tune_id, "SVM Tuning..." )
        self.options_menu.Append( performance_id, "Performance..." )
        
        # Create Menu Bar
        self.menu_bar = wx.MenuBar();
        self.menu_bar.Append( self.file_menu, "&File" )
        self.menu_bar.Append( self.options_menu, "&Options" )

        self.SetMenuBar( self.menu_bar )
        
        # Menu Events
        wx.EVT_MENU(self, wx.ID_ABOUT, self.onAbout )
        wx.EVT_MENU(self, license_id, self.onLicense )

        wx.EVT_MENU(self, mirror_id, self.onNull )
        wx.EVT_MENU(self, face_id, self.onNull )
        wx.EVT_MENU(self, svm_tune_id, self.onSVMTune )
        wx.EVT_MENU(self, performance_id, self.onTiming )
        
        # Set up menu checks
        self.face_menuitem.Check(True)
        self.eye_menuitem.Check(True)
        self.mirror_menuitem.Check(True)
        
        
        # ----------------- Image List ------------------
        
        # --------------- Image Display -----------------
        self.static_bitmap = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.EmptyBitmap(640, 480))
        
        self.controls_box = wx.StaticBox(self, wx.NewId(), "Controls")

        self.facel_logo = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.Bitmap(FACEL_LOGO))
        self.csu_logo = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.Bitmap(CSU_LOGO))
#        self.performance_box = wx.StaticBox(self, wx.NewId(), "Performance")
        
        self.enroll_chioce_label = wx.StaticText(self, wx.NewId(), "Enrollment Count:", style=wx.ALIGN_LEFT)
        self.enroll_choice = wx.Choice(self,wx.NewId(),wx.Point(0,0),wx.Size(-1,-1),['16','32','48','64','128','256'])
        self.enroll_choice.Select(3)
        
        self.train_button = wx.Button(self,wx.NewId(),'Train Labeler')
        self.reset_button = wx.Button(self,wx.NewId(),'Clear Labels')
        
        # --------------- Instrumentation ---------------
        
        
          
        self.enroll_label = wx.StaticText(self, wx.NewId(), "Click a face in the video to enroll.", style=wx.ALIGN_LEFT)

        self.ids_label = wx.StaticText(self, wx.NewId(), "Labels:", size=wx.Size(-1,16), style=wx.ALIGN_LEFT)
        self.ids_text = wx.StaticText(self, wx.NewId(), size = wx.Size(30,16), style= wx.ALIGN_RIGHT )  
        
        self.faces_label = wx.StaticText(self, wx.NewId(), "Faces:", size=wx.Size(-1,16), style=wx.ALIGN_LEFT)
        self.faces_text = wx.StaticText(self, wx.NewId(), size = wx.Size(30,16), style= wx.ALIGN_RIGHT )          
        

        # --------------- Window Layout -----------------
        enroll_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_sizer.Add(self.ids_label, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.Add(self.ids_text, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.AddSpacer(20)
        enroll_sizer.Add(self.faces_label, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.Add(self.faces_text, flag = wx.ALIGN_CENTER | wx.ALL, border=4)

        training_sizer = wx.BoxSizer(wx.HORIZONTAL)
        training_sizer.Add(self.train_button, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        training_sizer.Add(self.reset_button, flag = wx.ALIGN_CENTER | wx.ALL, border=4)

    
        enroll_choice_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_choice_sizer.Add(self.enroll_chioce_label, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        enroll_choice_sizer.Add(self.enroll_choice, flag = wx.ALIGN_CENTER | wx.ALL, border=0)

        controls_sizer = wx.StaticBoxSizer(self.controls_box,wx.VERTICAL) #wx.BoxSizer(wx.VERTICAL)
        controls_sizer.Add(self.enroll_label, flag = wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_choice_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=4)
        controls_sizer.Add(training_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=0)

        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        bottom_sizer.Add(self.facel_logo, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        bottom_sizer.Add(controls_sizer, flag = wx.ALIGN_TOP | wx.ALL, border=4)
        bottom_sizer.Add(self.csu_logo, flag = wx.ALIGN_CENTER | wx.ALL, border=0)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.static_bitmap, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        main_sizer.Add(bottom_sizer, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        

        self.SetAutoLayout(True)
        self.SetSizer(main_sizer)
        self.Layout()
        
        # -----------------------------------------------
        self.timer = FrameTimer(self)
        self.timer.Start(200)
        
        # -------------- Event Handleing ----------------
        wx.EVT_SIZE(self.static_bitmap, self.onBitmapResize)
        wx.EVT_LEFT_DOWN(self.static_bitmap, self.onClick)
                
        self.Bind(wx.EVT_BUTTON, self.onTrain, id=self.train_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.onReset, id=self.reset_button.GetId())
        
        # --------------- Setup State -------------------
        self.setupState()
Example #4
0
class VideoWindow(wx.Frame):
    '''
    This is the main FaceL window which includes the webcam video and enrollment and training controls.
    '''
    
    def __init__(self,parent,id,name,size=(640,672)):
        '''
        Create all the windows and controls used for the window and 
        '''
        wx.Frame.__init__(self,parent,id,name,size=size)
        
        self.CenterOnScreen(wx.HORIZONTAL)
        self.timing_window = None # Initialize timing window
        
        # ------------- Face Processing -----------------
        self.face_detector = CascadeDetector(cascade_name=CASCADE_NAME,image_scale=0.5)
        self.fel = FilterEyeLocator(FEL_NAME)
        self.face_rec = SVMFaceRec()
        
        self.svm_mode  = SVM_AUTOMATIC
        self.svm_C     = 4.000e+00
        self.svm_Gamma = 9.766e-04
        
        self.current_faces = []
        self.enrolling    = None
        self.enroll_count = 0
        self.enroll_max   = 32
        self.enroll_list  = []
        
        self.previous_time = time.time()
        
        self.arialblack24 = PIL.ImageFont.truetype(ARIAL_BLACK_NAME, 24)

        # ---------------- Basic Data -------------------
        try:
            self.webcam = Webcam()
        except SystemExit:
            raise
        except:
            trace = traceback.format_exc()
            message = TraceBackDialog(None, "Camera Error", CAMERA_ERROR, trace)
            message.ShowModal()
            
            sys.stderr.write("FaceL Error: an error occurred while trying to connect to the camera.  Details follow.\n\n")
            sys.stderr.write(trace)
            sys.exit(CAMERA_ERROR_CODE)

        # ------------- Other Components ----------------
        self.CreateStatusBar()
        
        # ------------------- Menu ----------------------
        # Creating the menubar.
        
        # Menu IDs
        license_id = wx.NewId()
        
        mirror_id = wx.NewId()
        face_id = wx.NewId()
        svm_tune_id = wx.NewId()
        performance_id = wx.NewId()
        
        # Menu Items
        self.file_menu = wx.Menu();

        self.file_menu.Append( wx.ID_ABOUT, "&About..." )
        self.file_menu.Append( license_id, "FaceL License..." )
        self.file_menu.AppendSeparator();
        self.file_menu.Append( wx.ID_EXIT, "E&xit" )

        self.options_menu = wx.Menu();
        self.face_menuitem = self.options_menu.AppendCheckItem( face_id, "Face Processing" )
        self.eye_menuitem = self.options_menu.AppendCheckItem( face_id, "Eye Detection" )
        self.mirror_menuitem = self.options_menu.AppendCheckItem( mirror_id, "Mirror Video" )
        self.options_menu.AppendSeparator()
        self.options_menu.Append( svm_tune_id, "SVM Tuning..." )
        self.options_menu.Append( performance_id, "Performance..." )
        
        # Create Menu Bar
        self.menu_bar = wx.MenuBar();
        self.menu_bar.Append( self.file_menu, "&File" )
        self.menu_bar.Append( self.options_menu, "&Options" )

        self.SetMenuBar( self.menu_bar )
        
        # Menu Events
        wx.EVT_MENU(self, wx.ID_ABOUT, self.onAbout )
        wx.EVT_MENU(self, license_id, self.onLicense )

        wx.EVT_MENU(self, mirror_id, self.onNull )
        wx.EVT_MENU(self, face_id, self.onNull )
        wx.EVT_MENU(self, svm_tune_id, self.onSVMTune )
        wx.EVT_MENU(self, performance_id, self.onTiming )
        
        # Set up menu checks
        self.face_menuitem.Check(True)
        self.eye_menuitem.Check(True)
        self.mirror_menuitem.Check(True)
        
        
        # ----------------- Image List ------------------
        
        # --------------- Image Display -----------------
        self.static_bitmap = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.EmptyBitmap(640, 480))
        
        self.controls_box = wx.StaticBox(self, wx.NewId(), "Controls")

        self.facel_logo = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.Bitmap(FACEL_LOGO))
        self.csu_logo = wx.StaticBitmap(self,wx.NewId(), bitmap=wx.Bitmap(CSU_LOGO))
#        self.performance_box = wx.StaticBox(self, wx.NewId(), "Performance")
        
        self.enroll_chioce_label = wx.StaticText(self, wx.NewId(), "Enrollment Count:", style=wx.ALIGN_LEFT)
        self.enroll_choice = wx.Choice(self,wx.NewId(),wx.Point(0,0),wx.Size(-1,-1),['16','32','48','64','128','256'])
        self.enroll_choice.Select(3)
        
        self.train_button = wx.Button(self,wx.NewId(),'Train Labeler')
        self.reset_button = wx.Button(self,wx.NewId(),'Clear Labels')
        
        # --------------- Instrumentation ---------------
        
        
          
        self.enroll_label = wx.StaticText(self, wx.NewId(), "Click a face in the video to enroll.", style=wx.ALIGN_LEFT)

        self.ids_label = wx.StaticText(self, wx.NewId(), "Labels:", size=wx.Size(-1,16), style=wx.ALIGN_LEFT)
        self.ids_text = wx.StaticText(self, wx.NewId(), size = wx.Size(30,16), style= wx.ALIGN_RIGHT )  
        
        self.faces_label = wx.StaticText(self, wx.NewId(), "Faces:", size=wx.Size(-1,16), style=wx.ALIGN_LEFT)
        self.faces_text = wx.StaticText(self, wx.NewId(), size = wx.Size(30,16), style= wx.ALIGN_RIGHT )          
        

        # --------------- Window Layout -----------------
        enroll_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_sizer.Add(self.ids_label, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.Add(self.ids_text, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.AddSpacer(20)
        enroll_sizer.Add(self.faces_label, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        enroll_sizer.Add(self.faces_text, flag = wx.ALIGN_CENTER | wx.ALL, border=4)

        training_sizer = wx.BoxSizer(wx.HORIZONTAL)
        training_sizer.Add(self.train_button, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        training_sizer.Add(self.reset_button, flag = wx.ALIGN_CENTER | wx.ALL, border=4)

    
        enroll_choice_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_choice_sizer.Add(self.enroll_chioce_label, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        enroll_choice_sizer.Add(self.enroll_choice, flag = wx.ALIGN_CENTER | wx.ALL, border=0)

        controls_sizer = wx.StaticBoxSizer(self.controls_box,wx.VERTICAL) #wx.BoxSizer(wx.VERTICAL)
        controls_sizer.Add(self.enroll_label, flag = wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_choice_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=4)
        controls_sizer.Add(training_sizer, flag = wx.ALIGN_LEFT | wx.ALL, border=0)

        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        bottom_sizer.Add(self.facel_logo, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        bottom_sizer.Add(controls_sizer, flag = wx.ALIGN_TOP | wx.ALL, border=4)
        bottom_sizer.Add(self.csu_logo, flag = wx.ALIGN_CENTER | wx.ALL, border=0)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.static_bitmap, flag = wx.ALIGN_CENTER | wx.ALL, border=0)
        main_sizer.Add(bottom_sizer, flag = wx.ALIGN_CENTER | wx.ALL, border=4)
        

        self.SetAutoLayout(True)
        self.SetSizer(main_sizer)
        self.Layout()
        
        # -----------------------------------------------
        self.timer = FrameTimer(self)
        self.timer.Start(200)
        
        # -------------- Event Handleing ----------------
        wx.EVT_SIZE(self.static_bitmap, self.onBitmapResize)
        wx.EVT_LEFT_DOWN(self.static_bitmap, self.onClick)
                
        self.Bind(wx.EVT_BUTTON, self.onTrain, id=self.train_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.onReset, id=self.reset_button.GetId())
        
        # --------------- Setup State -------------------
        self.setupState()
                
    def onTrain(self,event=None):
        '''
        Start the SVM training process.
        '''
        print "Train"
        #progress = wx.ProgressDialog(title="SVM Training", message="Training the Face Recognition Algorithm. Please Wait...")
        if self.svm_mode == SVM_AUTOMATIC:
            # Train with automatic tuning.
            self.face_rec.train() #callback=progress.Update)
            self.svm_C = self.face_rec.svm.C
            self.svm_Gamma = self.face_rec.svm.gamma
        else:
            # Train with manual tuning.
            self.face_rec.train( C=[self.svm_C] , Gamma=[self.svm_Gamma])# ,callback=progress.Update)
        
        #progress.Destroy()
        
    def onReset(self,event=None):
        '''
        Clear the enrollment data for the SVM.
        '''
        self.face_rec.reset() 
        self.setupState()

        
    def onFrame(self,event=None):
        '''
        Retrieve and process a video frame.
        '''
        self.timer.Stop()        
        starttime = time.time()
        self.detect_time = 0.0
        self.eye_time = 0.0
        self.label_time = 0.0
        img = self.webcam.query()
        
        face_processing = self.face_menuitem.IsChecked()
        eye_processing = self.eye_menuitem.IsChecked()
        
        names = []
        
        if face_processing:
            faces = self.findFaces(img)
            if self.enrolling != None:
                success = None
                for rect,leye,reye in faces:
                    img.annotateRect(self.enrolling,color='yellow')
                    if (success == None) and is_success(self.enrolling,rect):
                        success = rect
                        img.annotateRect(rect,color='blue')
                        if eye_processing:
                            img.annotatePoint(leye,color='blue')
                            img.annotatePoint(reye,color='blue')
                        self.enroll_list.append([img,rect,leye,reye])

                    else:
                        img.annotateRect(rect,color='red')
                        if eye_processing:
                            img.annotatePoint(leye,color='red')
                            img.annotatePoint(reye,color='red')
                        img.annotateLine(pv.Point(rect.x,rect.y),pv.Point(rect.x+rect.w,rect.y+rect.h), color='red')
                        img.annotateLine(pv.Point(rect.x+rect.w,rect.y),pv.Point(rect.x,rect.y+rect.h), color='red')

                if success == None:
                    rect = self.enrolling
                    img.annotateLine(pv.Point(rect.x,rect.y),pv.Point(rect.x+rect.w,rect.y+rect.h), color='yellow')
                    img.annotateLine(pv.Point(rect.x+rect.w,rect.y),pv.Point(rect.x,rect.y+rect.h), color='yellow')
                else:
                    #enroll in the identification algorithm
                    pass
            else:
                for rect,leye,reye in faces:
                    img.annotateRect(rect,color='blue')
                    if eye_processing:
                        img.annotatePoint(leye,color='blue')
                        img.annotatePoint(reye,color='blue')
                    
            
            if self.face_rec.isTrained():
                self.label_time = time.time()
                for rect,leye,reye in faces:
                    label = self.face_rec.predict(img,leye,reye)
                    names.append([0.5*(leye+reye),label])
                self.label_time = time.time() - self.label_time


        # Displaying Annotated Frame
        im = img.asAnnotated()
        if self.mirror_menuitem.IsChecked():
            im = im.transpose(FLIP_LEFT_RIGHT)
            
        if self.enrolling != None:
            draw = PIL.ImageDraw.Draw(im)
            x,y = self.enrolling.x,self.enrolling.y
            if self.mirror_menuitem.IsChecked():
                x = 640 - (x + self.enrolling.w)
            self.enroll_count += 1
            draw.text((x+10,y+10), "Enrolling: %2d of %2d"%(self.enroll_count,self.enroll_max), fill='yellow', font=self.arialblack24)
            del draw
            
            if self.enroll_count >= self.enroll_max:
                print "Count:",self.enroll_count
                
                if len(self.enroll_list) == 0:
                    warning_dialog = wx.MessageDialog(self,
                                                      "No faces were detected during the enrollment process.  Please face towards the camera and keep your face in the yellow rectangle during enrollment.",
                                                      style=wx.ICON_EXCLAMATION | wx.OK,
                                                      caption="Enrollment Error")
                    warning_dialog.ShowModal()
                else:
                    name_dialog = wx.TextEntryDialog(self, "Please enter a name to associate with the face. (%d faces captured)"%len(self.enroll_list), caption = "Enrollment ID")
                    result = name_dialog.ShowModal()
                    sub_id = name_dialog.GetValue()
                    if result == wx.ID_OK:
                        if sub_id == "":
                            print "Warning: Empty Subject ID"
                            warning_dialog = wx.MessageDialog(self,
                                                              "A name was entered in the previous dialog so this face will not be enrolled in the database.  Please repeat the enrollment process for this person.",
                                                              style=wx.ICON_EXCLAMATION | wx.OK,
                                                              caption="Enrollment Error")
                            warning_dialog.ShowModal()
                        else:
                            for data,rect,leye,reye in self.enroll_list:
                                self.face_rec.addTraining(data,leye,reye,sub_id)
                                self.setupState()

                                
                self.enroll_count = 0
                self.enrolling    = None
                self.enroll_list  = []
            
            
        if len(names) > 0:
            draw = PIL.ImageDraw.Draw(im)
            for pt,name in names:
                x,y = pt.X(),pt.Y() 
                w,h = draw.textsize(name,font=self.arialblack24)
                if self.mirror_menuitem.IsChecked():
                    x = 640 - x - 0.5*w
                else:
                    x = x - 0.5*w
                draw.text((x,y-20-h), name, fill='green', font=self.arialblack24)
            del draw

            
            
        wxImg = wx.EmptyImage(im.size[0], im.size[1])
        wxImg.SetData(im.tostring())
        bm = wxImg.ConvertToBitmap()
            
        self.static_bitmap.SetBitmap(bm)
        
        # Update timing gauges
        full_time = time.time() - starttime
        if self.timing_window != None:
            self.timing_window.update(self.detect_time,self.eye_time,self.label_time,full_time)
               
        self.ids_text.SetLabel("%d"%(self.face_rec.n_labels,))
        self.faces_text.SetLabel("%d"%(self.face_rec.n_faces,))
        
        sleep_time = 1
        if sys.platform.startswith("linux"):
            sleep_time = 10 
        # TODO: For macosx milliseconds should be 1
        # TODO: For linux milliseconds may need to be set to a higher value 10
        self.timer.Start(milliseconds = sleep_time, oneShot = 1)


    
    def setupState(self):
        #print "state",self.face_rec.n_labels,self.IsEnabled()
        if self.face_rec.n_labels >= 2:
            self.train_button.Enable()
        else:
            self.train_button.Disable()
            
        
    def onBitmapResize(self,event):
        w = event.GetSize().GetWidth()
        h = event.GetSize().GetHeight()

        self.static_bitmap.SetSize(event.GetSize())
              
              
    def onClick(self,event):
        '''
        Process a click in the Video window which starts the enrollment process.
        '''
        x = event.GetX()
        y = event.GetY()
        
        if self.mirror_menuitem.IsChecked():
            x = 640-x
            
        for rect,leye,reye in self.current_faces:
            if rect.containsPoint(pv.Point(x,y)):
                self.enrolling = rect
                self.enroll_count = 0
                self.enroll_max = int(self.enroll_choice.GetStringSelection())
                

    def findFaces(self,im):
        eye_processing = self.eye_menuitem.IsChecked()
        
        self.detect_time = time.time()
        rects = self.face_detector.detect(im)
        self.detect_time = time.time() - self.detect_time
 
        
        self.eye_time = time.time()       
        if eye_processing:
            faces = self.fel.locateEyes(im, rects)
        else:
            faces = []
            for rect in rects:
                affine = pv.AffineFromRect(rect,(1,1))
                leye = affine.invertPoint(AVE_LEFT_EYE)
                reye = affine.invertPoint(AVE_RIGHT_EYE)
                faces.append([rect,leye,reye])
        
        self.eye_time = time.time() - self.eye_time

        self.current_faces = faces

        return faces
    
    def onAbout(self,event):
        wx.MessageBox( ABOUT_MESSAGE,
                  "About FaceL", wx.OK | wx.ICON_INFORMATION )
        
    def onLicense(self,event):
        wx.MessageBox( LICENSE_MESSAGE,
                  "FaceL License", wx.OK | wx.ICON_INFORMATION )

    def onNull(self,*args,**kwargs):
        pass
    
    def onSVMTune(self,event):
        dialog = SVMTuningDialog(self, self.svm_mode, self.svm_C, self.svm_Gamma)
        dialog.CenterOnParent()

        result = dialog.ShowModal()
        if result == wx.ID_OK:
            self.svm_mode  = dialog.mode
            self.svm_C     = dialog.C
            self.svm_Gamma = dialog.Gamma
        
        print "SVM Tuning Info <MODE:%s; C:%0.2e; Gamma:%0.2e>"%(self.svm_mode,self.svm_C,self.svm_Gamma)
        
        dialog.Destroy()
        
        
    def onTiming(self,event):
        if self.timing_window == None:
            self.timing_window = TimingWindow(self, wx.NewId(),"Performance")
            self.timing_window.CenterOnParent()
            self.timing_window.Show(True)
            self.timing_window.Bind(wx.EVT_CLOSE, self.onCloseTiming, id=self.timing_window.GetId())

        else:
            self.timing_window.Show(True)
            self.timing_window.Raise()
        
        
    def onCloseTiming(self,event):
        self.timing_window.Destroy()
        self.timing_window = None
Example #5
0
    def __init__(self, parent, id, name, size=(640, 672)):
        '''
        Create all the windows and controls used for the window and 
        '''
        wx.Frame.__init__(self, parent, id, name, size=size)

        self.CenterOnScreen(wx.HORIZONTAL)
        self.timing_window = None  # Initialize timing window

        # ------------- Face Processing -----------------
        self.face_detector = CascadeDetector(cascade_name=CASCADE_NAME,
                                             image_scale=0.5)
        self.fel = FilterEyeLocator(FEL_NAME)
        self.face_rec = SVMFaceRec()

        self.svm_mode = SVM_AUTOMATIC
        self.svm_C = 4.000e+00
        self.svm_Gamma = 9.766e-04

        self.current_faces = []
        self.enrolling = None
        self.enroll_count = 0
        self.enroll_max = 32
        self.enroll_list = []

        self.previous_time = time.time()

        self.arialblack24 = PIL.ImageFont.truetype(ARIAL_BLACK_NAME, 24)

        # ---------------- Basic Data -------------------
        try:
            self.webcam = Webcam()
        except SystemExit:
            raise
        except:
            trace = traceback.format_exc()
            message = TraceBackDialog(None, "Camera Error", CAMERA_ERROR,
                                      trace)
            message.ShowModal()

            sys.stderr.write(
                "FaceL Error: an error occurred while trying to connect to the camera.  Details follow.\n\n"
            )
            sys.stderr.write(trace)
            sys.exit(CAMERA_ERROR_CODE)

        # ------------- Other Components ----------------
        self.CreateStatusBar()

        # ------------------- Menu ----------------------
        # Creating the menubar.

        # Menu IDs
        license_id = wx.NewId()

        mirror_id = wx.NewId()
        face_id = wx.NewId()
        svm_tune_id = wx.NewId()
        performance_id = wx.NewId()

        # Menu Items
        self.file_menu = wx.Menu()

        self.file_menu.Append(wx.ID_ABOUT, "&About...")
        self.file_menu.Append(license_id, "FaceL License...")
        self.file_menu.AppendSeparator()
        self.file_menu.Append(wx.ID_EXIT, "E&xit")

        self.options_menu = wx.Menu()
        self.face_menuitem = self.options_menu.AppendCheckItem(
            face_id, "Face Processing")
        self.eye_menuitem = self.options_menu.AppendCheckItem(
            face_id, "Eye Detection")
        self.mirror_menuitem = self.options_menu.AppendCheckItem(
            mirror_id, "Mirror Video")
        self.options_menu.AppendSeparator()
        self.options_menu.Append(svm_tune_id, "SVM Tuning...")
        self.options_menu.Append(performance_id, "Performance...")

        # Create Menu Bar
        self.menu_bar = wx.MenuBar()
        self.menu_bar.Append(self.file_menu, "&File")
        self.menu_bar.Append(self.options_menu, "&Options")

        self.SetMenuBar(self.menu_bar)

        # Menu Events
        wx.EVT_MENU(self, wx.ID_ABOUT, self.onAbout)
        wx.EVT_MENU(self, license_id, self.onLicense)

        wx.EVT_MENU(self, mirror_id, self.onNull)
        wx.EVT_MENU(self, face_id, self.onNull)
        wx.EVT_MENU(self, svm_tune_id, self.onSVMTune)
        wx.EVT_MENU(self, performance_id, self.onTiming)

        # Set up menu checks
        self.face_menuitem.Check(True)
        self.eye_menuitem.Check(True)
        self.mirror_menuitem.Check(True)

        # ----------------- Image List ------------------

        # --------------- Image Display -----------------
        self.static_bitmap = wx.StaticBitmap(self,
                                             wx.NewId(),
                                             bitmap=wx.EmptyBitmap(640, 480))

        self.controls_box = wx.StaticBox(self, wx.NewId(), "Controls")

        self.facel_logo = wx.StaticBitmap(self,
                                          wx.NewId(),
                                          bitmap=wx.Bitmap(FACEL_LOGO))
        self.csu_logo = wx.StaticBitmap(self,
                                        wx.NewId(),
                                        bitmap=wx.Bitmap(CSU_LOGO))
        #        self.performance_box = wx.StaticBox(self, wx.NewId(), "Performance")

        self.enroll_chioce_label = wx.StaticText(self,
                                                 wx.NewId(),
                                                 "Enrollment Count:",
                                                 style=wx.ALIGN_LEFT)
        self.enroll_choice = wx.Choice(self, wx.NewId(), wx.Point(0, 0),
                                       wx.Size(-1, -1),
                                       ['16', '32', '48', '64', '128', '256'])
        self.enroll_choice.Select(3)

        self.train_button = wx.Button(self, wx.NewId(), 'Train Labeler')
        self.reset_button = wx.Button(self, wx.NewId(), 'Clear Labels')

        # --------------- Instrumentation ---------------

        self.enroll_label = wx.StaticText(
            self,
            wx.NewId(),
            "Click a face in the video to enroll.",
            style=wx.ALIGN_LEFT)

        self.ids_label = wx.StaticText(self,
                                       wx.NewId(),
                                       "Labels:",
                                       size=wx.Size(-1, 16),
                                       style=wx.ALIGN_LEFT)
        self.ids_text = wx.StaticText(self,
                                      wx.NewId(),
                                      size=wx.Size(30, 16),
                                      style=wx.ALIGN_RIGHT)

        self.faces_label = wx.StaticText(self,
                                         wx.NewId(),
                                         "Faces:",
                                         size=wx.Size(-1, 16),
                                         style=wx.ALIGN_LEFT)
        self.faces_text = wx.StaticText(self,
                                        wx.NewId(),
                                        size=wx.Size(30, 16),
                                        style=wx.ALIGN_RIGHT)

        # --------------- Window Layout -----------------
        enroll_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_sizer.Add(self.ids_label,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.Add(self.ids_text,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.AddSpacer(20)
        enroll_sizer.Add(self.faces_label,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.Add(self.faces_text,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)

        training_sizer = wx.BoxSizer(wx.HORIZONTAL)
        training_sizer.Add(self.train_button,
                           flag=wx.ALIGN_CENTER | wx.ALL,
                           border=4)
        training_sizer.Add(self.reset_button,
                           flag=wx.ALIGN_CENTER | wx.ALL,
                           border=4)

        enroll_choice_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_choice_sizer.Add(self.enroll_chioce_label,
                                flag=wx.ALIGN_CENTER | wx.ALL,
                                border=0)
        enroll_choice_sizer.Add(self.enroll_choice,
                                flag=wx.ALIGN_CENTER | wx.ALL,
                                border=0)

        controls_sizer = wx.StaticBoxSizer(
            self.controls_box, wx.VERTICAL)  #wx.BoxSizer(wx.VERTICAL)
        controls_sizer.Add(self.enroll_label,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=0)
        controls_sizer.Add(enroll_sizer, flag=wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_choice_sizer,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=4)
        controls_sizer.Add(training_sizer,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=0)

        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        bottom_sizer.Add(self.facel_logo,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=0)
        bottom_sizer.Add(controls_sizer, flag=wx.ALIGN_TOP | wx.ALL, border=4)
        bottom_sizer.Add(self.csu_logo,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=0)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.static_bitmap,
                       flag=wx.ALIGN_CENTER | wx.ALL,
                       border=0)
        main_sizer.Add(bottom_sizer, flag=wx.ALIGN_CENTER | wx.ALL, border=4)

        self.SetAutoLayout(True)
        self.SetSizer(main_sizer)
        self.Layout()

        # -----------------------------------------------
        self.timer = FrameTimer(self)
        self.timer.Start(200)

        # -------------- Event Handleing ----------------
        wx.EVT_SIZE(self.static_bitmap, self.onBitmapResize)
        wx.EVT_LEFT_DOWN(self.static_bitmap, self.onClick)

        self.Bind(wx.EVT_BUTTON, self.onTrain, id=self.train_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.onReset, id=self.reset_button.GetId())

        # --------------- Setup State -------------------
        self.setupState()
Example #6
0
class VideoWindow(wx.Frame):
    '''
    This is the main FaceL window which includes the webcam video and enrollment and training controls.
    '''
    def __init__(self, parent, id, name, size=(640, 672)):
        '''
        Create all the windows and controls used for the window and 
        '''
        wx.Frame.__init__(self, parent, id, name, size=size)

        self.CenterOnScreen(wx.HORIZONTAL)
        self.timing_window = None  # Initialize timing window

        # ------------- Face Processing -----------------
        self.face_detector = CascadeDetector(cascade_name=CASCADE_NAME,
                                             image_scale=0.5)
        self.fel = FilterEyeLocator(FEL_NAME)
        self.face_rec = SVMFaceRec()

        self.svm_mode = SVM_AUTOMATIC
        self.svm_C = 4.000e+00
        self.svm_Gamma = 9.766e-04

        self.current_faces = []
        self.enrolling = None
        self.enroll_count = 0
        self.enroll_max = 32
        self.enroll_list = []

        self.previous_time = time.time()

        self.arialblack24 = PIL.ImageFont.truetype(ARIAL_BLACK_NAME, 24)

        # ---------------- Basic Data -------------------
        try:
            self.webcam = Webcam()
        except SystemExit:
            raise
        except:
            trace = traceback.format_exc()
            message = TraceBackDialog(None, "Camera Error", CAMERA_ERROR,
                                      trace)
            message.ShowModal()

            sys.stderr.write(
                "FaceL Error: an error occurred while trying to connect to the camera.  Details follow.\n\n"
            )
            sys.stderr.write(trace)
            sys.exit(CAMERA_ERROR_CODE)

        # ------------- Other Components ----------------
        self.CreateStatusBar()

        # ------------------- Menu ----------------------
        # Creating the menubar.

        # Menu IDs
        license_id = wx.NewId()

        mirror_id = wx.NewId()
        face_id = wx.NewId()
        svm_tune_id = wx.NewId()
        performance_id = wx.NewId()

        # Menu Items
        self.file_menu = wx.Menu()

        self.file_menu.Append(wx.ID_ABOUT, "&About...")
        self.file_menu.Append(license_id, "FaceL License...")
        self.file_menu.AppendSeparator()
        self.file_menu.Append(wx.ID_EXIT, "E&xit")

        self.options_menu = wx.Menu()
        self.face_menuitem = self.options_menu.AppendCheckItem(
            face_id, "Face Processing")
        self.eye_menuitem = self.options_menu.AppendCheckItem(
            face_id, "Eye Detection")
        self.mirror_menuitem = self.options_menu.AppendCheckItem(
            mirror_id, "Mirror Video")
        self.options_menu.AppendSeparator()
        self.options_menu.Append(svm_tune_id, "SVM Tuning...")
        self.options_menu.Append(performance_id, "Performance...")

        # Create Menu Bar
        self.menu_bar = wx.MenuBar()
        self.menu_bar.Append(self.file_menu, "&File")
        self.menu_bar.Append(self.options_menu, "&Options")

        self.SetMenuBar(self.menu_bar)

        # Menu Events
        wx.EVT_MENU(self, wx.ID_ABOUT, self.onAbout)
        wx.EVT_MENU(self, license_id, self.onLicense)

        wx.EVT_MENU(self, mirror_id, self.onNull)
        wx.EVT_MENU(self, face_id, self.onNull)
        wx.EVT_MENU(self, svm_tune_id, self.onSVMTune)
        wx.EVT_MENU(self, performance_id, self.onTiming)

        # Set up menu checks
        self.face_menuitem.Check(True)
        self.eye_menuitem.Check(True)
        self.mirror_menuitem.Check(True)

        # ----------------- Image List ------------------

        # --------------- Image Display -----------------
        self.static_bitmap = wx.StaticBitmap(self,
                                             wx.NewId(),
                                             bitmap=wx.EmptyBitmap(640, 480))

        self.controls_box = wx.StaticBox(self, wx.NewId(), "Controls")

        self.facel_logo = wx.StaticBitmap(self,
                                          wx.NewId(),
                                          bitmap=wx.Bitmap(FACEL_LOGO))
        self.csu_logo = wx.StaticBitmap(self,
                                        wx.NewId(),
                                        bitmap=wx.Bitmap(CSU_LOGO))
        #        self.performance_box = wx.StaticBox(self, wx.NewId(), "Performance")

        self.enroll_chioce_label = wx.StaticText(self,
                                                 wx.NewId(),
                                                 "Enrollment Count:",
                                                 style=wx.ALIGN_LEFT)
        self.enroll_choice = wx.Choice(self, wx.NewId(), wx.Point(0, 0),
                                       wx.Size(-1, -1),
                                       ['16', '32', '48', '64', '128', '256'])
        self.enroll_choice.Select(3)

        self.train_button = wx.Button(self, wx.NewId(), 'Train Labeler')
        self.reset_button = wx.Button(self, wx.NewId(), 'Clear Labels')

        # --------------- Instrumentation ---------------

        self.enroll_label = wx.StaticText(
            self,
            wx.NewId(),
            "Click a face in the video to enroll.",
            style=wx.ALIGN_LEFT)

        self.ids_label = wx.StaticText(self,
                                       wx.NewId(),
                                       "Labels:",
                                       size=wx.Size(-1, 16),
                                       style=wx.ALIGN_LEFT)
        self.ids_text = wx.StaticText(self,
                                      wx.NewId(),
                                      size=wx.Size(30, 16),
                                      style=wx.ALIGN_RIGHT)

        self.faces_label = wx.StaticText(self,
                                         wx.NewId(),
                                         "Faces:",
                                         size=wx.Size(-1, 16),
                                         style=wx.ALIGN_LEFT)
        self.faces_text = wx.StaticText(self,
                                        wx.NewId(),
                                        size=wx.Size(30, 16),
                                        style=wx.ALIGN_RIGHT)

        # --------------- Window Layout -----------------
        enroll_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_sizer.Add(self.ids_label,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.Add(self.ids_text,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.AddSpacer(20)
        enroll_sizer.Add(self.faces_label,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)
        enroll_sizer.Add(self.faces_text,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=4)

        training_sizer = wx.BoxSizer(wx.HORIZONTAL)
        training_sizer.Add(self.train_button,
                           flag=wx.ALIGN_CENTER | wx.ALL,
                           border=4)
        training_sizer.Add(self.reset_button,
                           flag=wx.ALIGN_CENTER | wx.ALL,
                           border=4)

        enroll_choice_sizer = wx.BoxSizer(wx.HORIZONTAL)
        enroll_choice_sizer.Add(self.enroll_chioce_label,
                                flag=wx.ALIGN_CENTER | wx.ALL,
                                border=0)
        enroll_choice_sizer.Add(self.enroll_choice,
                                flag=wx.ALIGN_CENTER | wx.ALL,
                                border=0)

        controls_sizer = wx.StaticBoxSizer(
            self.controls_box, wx.VERTICAL)  #wx.BoxSizer(wx.VERTICAL)
        controls_sizer.Add(self.enroll_label,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=0)
        controls_sizer.Add(enroll_sizer, flag=wx.ALIGN_LEFT | wx.ALL, border=0)
        controls_sizer.Add(enroll_choice_sizer,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=4)
        controls_sizer.Add(training_sizer,
                           flag=wx.ALIGN_LEFT | wx.ALL,
                           border=0)

        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        bottom_sizer.Add(self.facel_logo,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=0)
        bottom_sizer.Add(controls_sizer, flag=wx.ALIGN_TOP | wx.ALL, border=4)
        bottom_sizer.Add(self.csu_logo,
                         flag=wx.ALIGN_CENTER | wx.ALL,
                         border=0)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.static_bitmap,
                       flag=wx.ALIGN_CENTER | wx.ALL,
                       border=0)
        main_sizer.Add(bottom_sizer, flag=wx.ALIGN_CENTER | wx.ALL, border=4)

        self.SetAutoLayout(True)
        self.SetSizer(main_sizer)
        self.Layout()

        # -----------------------------------------------
        self.timer = FrameTimer(self)
        self.timer.Start(200)

        # -------------- Event Handleing ----------------
        wx.EVT_SIZE(self.static_bitmap, self.onBitmapResize)
        wx.EVT_LEFT_DOWN(self.static_bitmap, self.onClick)

        self.Bind(wx.EVT_BUTTON, self.onTrain, id=self.train_button.GetId())
        self.Bind(wx.EVT_BUTTON, self.onReset, id=self.reset_button.GetId())

        # --------------- Setup State -------------------
        self.setupState()

    def onTrain(self, event=None):
        '''
        Start the SVM training process.
        '''
        print "Train"
        #progress = wx.ProgressDialog(title="SVM Training", message="Training the Face Recognition Algorithm. Please Wait...")
        if self.svm_mode == SVM_AUTOMATIC:
            # Train with automatic tuning.
            self.face_rec.train()  #callback=progress.Update)
            self.svm_C = self.face_rec.svm.C
            self.svm_Gamma = self.face_rec.svm.gamma
        else:
            # Train with manual tuning.
            self.face_rec.train(C=[self.svm_C],
                                Gamma=[self.svm_Gamma
                                       ])  # ,callback=progress.Update)

        #progress.Destroy()

    def onReset(self, event=None):
        '''
        Clear the enrollment data for the SVM.
        '''
        self.face_rec.reset()
        self.setupState()

    def onFrame(self, event=None):
        '''
        Retrieve and process a video frame.
        '''
        self.timer.Stop()
        starttime = time.time()
        self.detect_time = 0.0
        self.eye_time = 0.0
        self.label_time = 0.0
        img = self.webcam.query()

        face_processing = self.face_menuitem.IsChecked()
        eye_processing = self.eye_menuitem.IsChecked()

        names = []

        if face_processing:
            faces = self.findFaces(img)
            if self.enrolling != None:
                success = None
                for rect, leye, reye in faces:
                    img.annotateRect(self.enrolling, color='yellow')
                    if (success == None) and is_success(self.enrolling, rect):
                        success = rect
                        img.annotateRect(rect, color='blue')
                        if eye_processing:
                            img.annotatePoint(leye, color='blue')
                            img.annotatePoint(reye, color='blue')
                        self.enroll_list.append([img, rect, leye, reye])

                    else:
                        img.annotateRect(rect, color='red')
                        if eye_processing:
                            img.annotatePoint(leye, color='red')
                            img.annotatePoint(reye, color='red')
                        img.annotateLine(pv.Point(rect.x, rect.y),
                                         pv.Point(rect.x + rect.w,
                                                  rect.y + rect.h),
                                         color='red')
                        img.annotateLine(pv.Point(rect.x + rect.w, rect.y),
                                         pv.Point(rect.x, rect.y + rect.h),
                                         color='red')

                if success == None:
                    rect = self.enrolling
                    img.annotateLine(pv.Point(rect.x, rect.y),
                                     pv.Point(rect.x + rect.w,
                                              rect.y + rect.h),
                                     color='yellow')
                    img.annotateLine(pv.Point(rect.x + rect.w, rect.y),
                                     pv.Point(rect.x, rect.y + rect.h),
                                     color='yellow')
                else:
                    #enroll in the identification algorithm
                    pass
            else:
                for rect, leye, reye in faces:
                    img.annotateRect(rect, color='blue')
                    if eye_processing:
                        img.annotatePoint(leye, color='blue')
                        img.annotatePoint(reye, color='blue')

            if self.face_rec.isTrained():
                self.label_time = time.time()
                for rect, leye, reye in faces:
                    label = self.face_rec.predict(img, leye, reye)
                    names.append([0.5 * (leye + reye), label])
                self.label_time = time.time() - self.label_time

        # Displaying Annotated Frame
        im = img.asAnnotated()
        if self.mirror_menuitem.IsChecked():
            im = im.transpose(FLIP_LEFT_RIGHT)

        if self.enrolling != None:
            draw = PIL.ImageDraw.Draw(im)
            x, y = self.enrolling.x, self.enrolling.y
            if self.mirror_menuitem.IsChecked():
                x = 640 - (x + self.enrolling.w)
            self.enroll_count += 1
            draw.text(
                (x + 10, y + 10),
                "Enrolling: %2d of %2d" % (self.enroll_count, self.enroll_max),
                fill='yellow',
                font=self.arialblack24)
            del draw

            if self.enroll_count >= self.enroll_max:
                print "Count:", self.enroll_count

                if len(self.enroll_list) == 0:
                    warning_dialog = wx.MessageDialog(
                        self,
                        "No faces were detected during the enrollment process.  Please face towards the camera and keep your face in the yellow rectangle during enrollment.",
                        style=wx.ICON_EXCLAMATION | wx.OK,
                        caption="Enrollment Error")
                    warning_dialog.ShowModal()
                else:
                    name_dialog = wx.TextEntryDialog(
                        self,
                        "Please enter a name to associate with the face. (%d faces captured)"
                        % len(self.enroll_list),
                        caption="Enrollment ID")
                    result = name_dialog.ShowModal()
                    sub_id = name_dialog.GetValue()
                    if result == wx.ID_OK:
                        if sub_id == "":
                            print "Warning: Empty Subject ID"
                            warning_dialog = wx.MessageDialog(
                                self,
                                "A name was entered in the previous dialog so this face will not be enrolled in the database.  Please repeat the enrollment process for this person.",
                                style=wx.ICON_EXCLAMATION | wx.OK,
                                caption="Enrollment Error")
                            warning_dialog.ShowModal()
                        else:
                            for data, rect, leye, reye in self.enroll_list:
                                self.face_rec.addTraining(
                                    data, leye, reye, sub_id)
                                self.setupState()

                self.enroll_count = 0
                self.enrolling = None
                self.enroll_list = []

        if len(names) > 0:
            draw = PIL.ImageDraw.Draw(im)
            for pt, name in names:
                x, y = pt.X(), pt.Y()
                w, h = draw.textsize(name, font=self.arialblack24)
                if self.mirror_menuitem.IsChecked():
                    x = 640 - x - 0.5 * w
                else:
                    x = x - 0.5 * w
                draw.text((x, y - 20 - h),
                          name,
                          fill='green',
                          font=self.arialblack24)
            del draw

        wxImg = wx.EmptyImage(im.size[0], im.size[1])
        wxImg.SetData(im.tostring())
        bm = wxImg.ConvertToBitmap()

        self.static_bitmap.SetBitmap(bm)

        # Update timing gauges
        full_time = time.time() - starttime
        if self.timing_window != None:
            self.timing_window.update(self.detect_time, self.eye_time,
                                      self.label_time, full_time)

        self.ids_text.SetLabel("%d" % (self.face_rec.n_labels, ))
        self.faces_text.SetLabel("%d" % (self.face_rec.n_faces, ))

        sleep_time = 1
        if sys.platform.startswith("linux"):
            sleep_time = 10
        # TODO: For macosx milliseconds should be 1
        # TODO: For linux milliseconds may need to be set to a higher value 10
        self.timer.Start(milliseconds=sleep_time, oneShot=1)

    def setupState(self):
        #print "state",self.face_rec.n_labels,self.IsEnabled()
        if self.face_rec.n_labels >= 2:
            self.train_button.Enable()
        else:
            self.train_button.Disable()

    def onBitmapResize(self, event):
        w = event.GetSize().GetWidth()
        h = event.GetSize().GetHeight()

        self.static_bitmap.SetSize(event.GetSize())

    def onClick(self, event):
        '''
        Process a click in the Video window which starts the enrollment process.
        '''
        x = event.GetX()
        y = event.GetY()

        if self.mirror_menuitem.IsChecked():
            x = 640 - x

        for rect, leye, reye in self.current_faces:
            if rect.containsPoint(pv.Point(x, y)):
                self.enrolling = rect
                self.enroll_count = 0
                self.enroll_max = int(self.enroll_choice.GetStringSelection())

    def findFaces(self, im):
        eye_processing = self.eye_menuitem.IsChecked()

        self.detect_time = time.time()
        rects = self.face_detector.detect(im)
        self.detect_time = time.time() - self.detect_time

        self.eye_time = time.time()
        if eye_processing:
            faces = self.fel.locateEyes(im, rects)
        else:
            faces = []
            for rect in rects:
                affine = pv.AffineFromRect(rect, (1, 1))
                leye = affine.invertPoint(AVE_LEFT_EYE)
                reye = affine.invertPoint(AVE_RIGHT_EYE)
                faces.append([rect, leye, reye])

        self.eye_time = time.time() - self.eye_time

        self.current_faces = faces

        return faces

    def onAbout(self, event):
        wx.MessageBox(ABOUT_MESSAGE, "About FaceL",
                      wx.OK | wx.ICON_INFORMATION)

    def onLicense(self, event):
        wx.MessageBox(LICENSE_MESSAGE, "FaceL License",
                      wx.OK | wx.ICON_INFORMATION)

    def onNull(self, *args, **kwargs):
        pass

    def onSVMTune(self, event):
        dialog = SVMTuningDialog(self, self.svm_mode, self.svm_C,
                                 self.svm_Gamma)
        dialog.CenterOnParent()

        result = dialog.ShowModal()
        if result == wx.ID_OK:
            self.svm_mode = dialog.mode
            self.svm_C = dialog.C
            self.svm_Gamma = dialog.Gamma

        print "SVM Tuning Info <MODE:%s; C:%0.2e; Gamma:%0.2e>" % (
            self.svm_mode, self.svm_C, self.svm_Gamma)

        dialog.Destroy()

    def onTiming(self, event):
        if self.timing_window == None:
            self.timing_window = TimingWindow(self, wx.NewId(), "Performance")
            self.timing_window.CenterOnParent()
            self.timing_window.Show(True)
            self.timing_window.Bind(wx.EVT_CLOSE,
                                    self.onCloseTiming,
                                    id=self.timing_window.GetId())

        else:
            self.timing_window.Show(True)
            self.timing_window.Raise()

    def onCloseTiming(self, event):
        self.timing_window.Destroy()
        self.timing_window = None
Example #7
0
    
    # Open the file to use as output.
    f = open(args[1],'wb')
    csv_file = csv.writer(f)
    headers = ['image_name','detect_number','detect_x','detect_y','detect_width','detect_height','eye1_x','eye1_y','eye2_x','eye2_y']
    csv_file.writerow(headers)
    
    # Create an image log if this is being saved to a file.
    ilog = None
    if options.log_dir != None:
        print("Creating Image Log...")
        ilog = pv.ImageLog(options.log_dir)
    
    # For each image run face and eye detection
    face_detect = CascadeDetector(image_scale=1.3*options.scale)
    locate_eyes = FilterEyeLocator()#locator_filename)
    
    c = 0
    for pathname in image_names:
        c += 1
        
        im = pv.Image(pathname)

        scale = options.log_scale
        log_im = pv.AffineScale(scale,(int(scale*im.width),int(scale*im.height))).transformImage(im)
        
            
 
        results = processFaces(im,face_detect,locate_eyes)

        if options.rotate: