Exemple #1
0
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''

        # determine the face rect
        true_rect = face_from_eyes(left_eye, right_eye)

        # run the face detector
        rects = self.face_detector.detect(im)

        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect, true_rect):

                laffine, raffine = self.generateTransforms(pred_rect)

                lcropped = laffine.transformImage(im)
                rcropped = raffine.transformImage(im)

                #Normalize the images
                lcropped = pv.meanStd(lcropped)
                rcropped = pv.meanStd(rcropped)

                # Mark the eyes
                leye = laffine.transformPoint(left_eye)
                reye = raffine.transformPoint(right_eye)

                # Add training data to locators
                self.left_locator.addTraining(lcropped, leye)
                self.right_locator.addTraining(rcropped, reye)

                # Just use the first success
                return

        # The face was not detected
        self.detection_failures += 1
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''
        
        # determine the face rect
        true_rect = face_from_eyes(left_eye,right_eye)
        
        # run the face detector
        rects = self.face_detector.detect(im)
        
        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect,true_rect):
                
                laffine,raffine = self.generateTransforms(pred_rect)
                
                lcropped = laffine.transformImage(im)
                rcropped = raffine.transformImage(im)
                
                #Normalize the images
                lcropped = pv.meanStd(lcropped)
                rcropped = pv.meanStd(rcropped)
                
                # Mark the eyes
                leye = laffine.transformPoint(left_eye)
                reye = raffine.transformPoint(right_eye)

                # Add training data to locators
                self.left_locator.addTraining(lcropped,leye)
                self.right_locator.addTraining(rcropped,reye)
                
                # Just use the first success
                return
            
        # The face was not detected
        self.detection_failures += 1
Exemple #3
0
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''

        # determine the face rect
        true_rect = face_from_eyes(left_eye, right_eye)

        # run the face detector
        rects = self.face_detector.detect(im)

        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect, true_rect):
                # Transform the face
                affine = pv.AffineFromRect(pred_rect, self.tile_size)

                w, h = self.tile_size

                if self.perturbations:
                    # Randomly rotate, translate and scale the images
                    center = pv.AffineTranslate(-0.5 * w, -0.5 * h,
                                                self.tile_size)
                    rotate = pv.AffineRotate(random.uniform(-pi / 8, pi / 8),
                                             self.tile_size)
                    scale = pv.AffineScale(random.uniform(0.9, 1.1),
                                           self.tile_size)
                    translate = pv.AffineTranslate(
                        random.uniform(-0.05 * w, 0.05 * w),
                        random.uniform(-0.05 * h, 0.05 * h), self.tile_size)
                    inv_center = pv.AffineTranslate(0.5 * w, 0.5 * h,
                                                    self.tile_size)

                    affine = inv_center * translate * scale * rotate * center * affine
                    #affine = affine*center*rotate*scale*translate*inv_center

                cropped = affine.transformImage(im)
                cropped = pv.meanStd(cropped)

                # Mark the eyes
                leye = affine.transformPoint(left_eye)
                reye = affine.transformPoint(right_eye)

                # Add training data to locators
                self.training_labels.append((leye, reye))

                self.normalize.addTraining(0.0, cropped)
                #self.left_locator.addTraining(cropped,leye)
                #self.right_locator.addTraining(cropped,reye)

                # Just use the first success
                return

        # The face was not detected
        self.detection_failures += 1
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''
        
        # determine the face rect
        true_rect = face_from_eyes(left_eye,right_eye)
        
        # run the face detector
        rects = self.face_detector.detect(im)
        
        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect,true_rect):
                # Transform the face
                affine = pv.AffineFromRect(pred_rect,self.tile_size)

                w,h = self.tile_size
                
                if self.perturbations:
                    # Randomly rotate, translate and scale the images
                    center = pv.AffineTranslate(-0.5*w,-0.5*h,self.tile_size)
                    rotate = pv.AffineRotate(random.uniform(-pi/8,pi/8),self.tile_size)
                    scale = pv.AffineScale(random.uniform(0.9,1.1),self.tile_size)
                    translate = pv.AffineTranslate(random.uniform(-0.05*w,0.05*w),
                                               random.uniform(-0.05*h,0.05*h),
                                               self.tile_size)
                    inv_center = pv.AffineTranslate(0.5*w,0.5*h,self.tile_size)
                    
                    affine = inv_center*translate*scale*rotate*center*affine
                    #affine = affine*center*rotate*scale*translate*inv_center
                
                cropped = affine.transformImage(im)
                cropped = pv.meanStd(cropped)
                
                # Mark the eyes
                leye = affine.transformPoint(left_eye)
                reye = affine.transformPoint(right_eye)

                # Add training data to locators
                self.training_labels.append((leye,reye))

                self.normalize.addTraining(0.0,cropped)
                #self.left_locator.addTraining(cropped,leye)
                #self.right_locator.addTraining(cropped,reye)
                
                # Just use the first success
                return
            
        # The face was not detected
        self.detection_failures += 1
Exemple #5
0
    def addSample(self, truth_eyes, detected_eyes, im=None, annotate=False):
        ''''''
        self.images += 1
        
        if isinstance(im,pv.Image):
            name = im.filename
            if self.pixels != None:
                self.pixels += im.asPIL().size[0] * im.asPIL().size[1]
        elif isinstance(im,str):
            name = im
            self.pixels = None
        else:
            name = "%d"%self.sample_id
            self.pixels = None
            self.sample_id += 1
            
        self.stop_time = time.time()

        for tl,tr in truth_eyes:
            tface = face_from_eyes(tl,tr)

            detect_face = False
            eye_dist = None
            detect_b25  = False
            detect_b10  = False
            detect_b05  = False
            detect_l25  = False
            detect_l10  = False
            detect_l05  = False
            detect_r25  = False
            detect_r10  = False
            detect_r05  = False
            eye_dist = None
            tl_x  = None
            tl_y  = None
            tr_x  = None
            tr_y  = None
            pl_x  = None
            pl_y  = None
            pr_x  = None
            pr_y  = None
            dlx   = None
            dly   = None
            dl2   = None
            dl    = None
            dlfrac= None
            drx   = None
            dry   = None
            dr2   = None
            dr    = None
            drfrac= None
            deye  = None
            dmean = None
            
            for pl,pr in detected_eyes:
                dface = face_from_eyes(pl,pr)
                
                if not self.test_detect or is_success(tface,dface):
                    tl_x = tl.X()
                    tl_y = tl.Y()
                    tr_x = tr.X()
                    tr_y = tr.Y()
                    eye_dist = math.sqrt((tl_x-tr_x)*(tl_x-tr_x) + (tl_y-tr_y)*(tl_y-tr_y))
                    pl_x = pl.X()
                    pl_y = pl.Y()
                    pr_x = pr.X()
                    pr_y = pr.Y()
                    
                    detect_face = True
                    
                    eye_dist = math.sqrt((tl_x-tr_x)*(tl_x-tr_x) + (tl_y-tr_y)*(tl_y-tr_y))
                    
                    dlx = pl_x-tl_x
                    dly = pl_y-tl_y
                    dl2 = dlx*dlx + dly*dly
                    dl = math.sqrt(dl2)
                    dlfrac = dl/eye_dist
                    
                    drx = pr_x-tr_x
                    dry = pr_y-tr_y
                    dr2 = drx*drx + dry*dry
                    dr = math.sqrt(dr2)
                    drfrac = dr/eye_dist
                    
                    deye = max(drfrac,dlfrac)
                    
                    dmean = 0.5*(dr+dl)
                    
                    detect_l25  = 0.25 > dlfrac
                    detect_l10  = 0.10 > dlfrac
                    detect_l05  = 0.05 > dlfrac
                    detect_r25  = 0.25 > drfrac
                    detect_r10  = 0.10 > drfrac
                    detect_r05  = 0.05 > drfrac
                    detect_b25  = 0.25 > deye
                    detect_b10  = 0.10 > deye
                    detect_b05  = 0.05 > deye

                    break
                            
            self.table.setElement(self.faces,'name',name)              
            self.table.setElement(self.faces,'detect_face',detect_face)              
            self.table.setElement(self.faces,'detect_l25',detect_l25)              
            self.table.setElement(self.faces,'detect_l10',detect_l10)              
            self.table.setElement(self.faces,'detect_l05',detect_l05)              
            self.table.setElement(self.faces,'detect_r25',detect_r25)              
            self.table.setElement(self.faces,'detect_r10',detect_r10)              
            self.table.setElement(self.faces,'detect_r05',detect_r05)              
            self.table.setElement(self.faces,'detect_b25',detect_b25)              
            self.table.setElement(self.faces,'detect_b10',detect_b10)              
            self.table.setElement(self.faces,'detect_b05',detect_b05)              
            self.table.setElement(self.faces,'eye_dist',eye_dist)
                          
            self.table.setElement(self.faces,'truth_lx',tl_x)              
            self.table.setElement(self.faces,'truth_ly',tl_y)              
            self.table.setElement(self.faces,'truth_rx',tr_x)              
            self.table.setElement(self.faces,'truth_ry',tr_y)              
            
            self.table.setElement(self.faces,'pred_lx',pl_x)              
            self.table.setElement(self.faces,'pred_ly',pl_y)              
            self.table.setElement(self.faces,'pred_rx',pr_x)              
            self.table.setElement(self.faces,'pred_ry',pr_y)              
            
            self.table.setElement(self.faces,'dlx',dlx)              
            self.table.setElement(self.faces,'dly',dly)              
            #self.table.setElement(self.faces,'dl2',dl2)              
            self.table.setElement(self.faces,'dl',dl) # BUGFIX: 20080813 This was outputing dl2.             
            self.table.setElement(self.faces,'dlfrac',dlfrac)              
            self.table.setElement(self.faces,'drx',drx)              
            self.table.setElement(self.faces,'dry',dry)              
            #self.table.setElement(self.faces,'dr2',dr2)              
            self.table.setElement(self.faces,'dr',dr)              
            self.table.setElement(self.faces,'drfrac',drfrac)              
            self.table.setElement(self.faces,'deye',deye)              
            self.table.setElement(self.faces,'dmean',dmean) 
                         
            self.faces += 1
            if dlfrac != None:
                self.bothsse += dlfrac**2 + drfrac**2
                self.leftsse += dlfrac**2
                self.rightsse += drfrac**2
            
            if detect_face: self.face_successes    += 1
            if detect_b25:  self.both25_successes  += 1
            if detect_l25:  self.left25_successes  += 1
            if detect_r25:  self.right25_successes += 1
            if detect_b10:  self.both10_successes  += 1
            if detect_l10:  self.left10_successes  += 1
            if detect_r10:  self.right10_successes += 1
            if detect_b05:  self.both05_successes  += 1
            if detect_l05:  self.left05_successes  += 1
            if detect_r05:  self.right05_successes += 1
    def addSample(self, truth_eyes, detected_eyes, im=None, annotate=False):
        ''''''
        self.images += 1

        if isinstance(im, pv.Image):
            name = im.filename
            if self.pixels != None:
                self.pixels += im.asPIL().size[0] * im.asPIL().size[1]
        elif isinstance(im, str):
            name = im
            self.pixels = None
        else:
            name = "%d" % self.sample_id
            self.pixels = None
            self.sample_id += 1

        self.stop_time = time.time()

        for tl, tr in truth_eyes:
            tface = face_from_eyes(tl, tr)

            detect_face = False
            eye_dist = None
            detect_b25 = False
            detect_b10 = False
            detect_b05 = False
            detect_l25 = False
            detect_l10 = False
            detect_l05 = False
            detect_r25 = False
            detect_r10 = False
            detect_r05 = False
            eye_dist = None
            tl_x = None
            tl_y = None
            tr_x = None
            tr_y = None
            pl_x = None
            pl_y = None
            pr_x = None
            pr_y = None
            dlx = None
            dly = None
            dl2 = None
            dl = None
            dlfrac = None
            drx = None
            dry = None
            dr2 = None
            dr = None
            drfrac = None
            deye = None
            dmean = None

            for pl, pr in detected_eyes:
                dface = face_from_eyes(pl, pr)

                if not self.test_detect or is_success(tface, dface):
                    tl_x = tl.X()
                    tl_y = tl.Y()
                    tr_x = tr.X()
                    tr_y = tr.Y()
                    eye_dist = math.sqrt((tl_x - tr_x) * (tl_x - tr_x) +
                                         (tl_y - tr_y) * (tl_y - tr_y))
                    pl_x = pl.X()
                    pl_y = pl.Y()
                    pr_x = pr.X()
                    pr_y = pr.Y()

                    detect_face = True

                    eye_dist = math.sqrt((tl_x - tr_x) * (tl_x - tr_x) +
                                         (tl_y - tr_y) * (tl_y - tr_y))

                    dlx = pl_x - tl_x
                    dly = pl_y - tl_y
                    dl2 = dlx * dlx + dly * dly
                    dl = math.sqrt(dl2)
                    dlfrac = dl / eye_dist

                    drx = pr_x - tr_x
                    dry = pr_y - tr_y
                    dr2 = drx * drx + dry * dry
                    dr = math.sqrt(dr2)
                    drfrac = dr / eye_dist

                    deye = max(drfrac, dlfrac)

                    dmean = 0.5 * (dr + dl)

                    detect_l25 = 0.25 > dlfrac
                    detect_l10 = 0.10 > dlfrac
                    detect_l05 = 0.05 > dlfrac
                    detect_r25 = 0.25 > drfrac
                    detect_r10 = 0.10 > drfrac
                    detect_r05 = 0.05 > drfrac
                    detect_b25 = 0.25 > deye
                    detect_b10 = 0.10 > deye
                    detect_b05 = 0.05 > deye

                    break

            self.table.setElement(self.faces, 'name', name)
            self.table.setElement(self.faces, 'detect_face', detect_face)
            self.table.setElement(self.faces, 'detect_l25', detect_l25)
            self.table.setElement(self.faces, 'detect_l10', detect_l10)
            self.table.setElement(self.faces, 'detect_l05', detect_l05)
            self.table.setElement(self.faces, 'detect_r25', detect_r25)
            self.table.setElement(self.faces, 'detect_r10', detect_r10)
            self.table.setElement(self.faces, 'detect_r05', detect_r05)
            self.table.setElement(self.faces, 'detect_b25', detect_b25)
            self.table.setElement(self.faces, 'detect_b10', detect_b10)
            self.table.setElement(self.faces, 'detect_b05', detect_b05)
            self.table.setElement(self.faces, 'eye_dist', eye_dist)

            self.table.setElement(self.faces, 'truth_lx', tl_x)
            self.table.setElement(self.faces, 'truth_ly', tl_y)
            self.table.setElement(self.faces, 'truth_rx', tr_x)
            self.table.setElement(self.faces, 'truth_ry', tr_y)

            self.table.setElement(self.faces, 'pred_lx', pl_x)
            self.table.setElement(self.faces, 'pred_ly', pl_y)
            self.table.setElement(self.faces, 'pred_rx', pr_x)
            self.table.setElement(self.faces, 'pred_ry', pr_y)

            self.table.setElement(self.faces, 'dlx', dlx)
            self.table.setElement(self.faces, 'dly', dly)
            #self.table.setElement(self.faces,'dl2',dl2)
            self.table.setElement(
                self.faces, 'dl',
                dl)  # BUGFIX: 20080813 This was outputing dl2.
            self.table.setElement(self.faces, 'dlfrac', dlfrac)
            self.table.setElement(self.faces, 'drx', drx)
            self.table.setElement(self.faces, 'dry', dry)
            #self.table.setElement(self.faces,'dr2',dr2)
            self.table.setElement(self.faces, 'dr', dr)
            self.table.setElement(self.faces, 'drfrac', drfrac)
            self.table.setElement(self.faces, 'deye', deye)
            self.table.setElement(self.faces, 'dmean', dmean)

            self.faces += 1
            if dlfrac != None:
                self.bothsse += dlfrac**2 + drfrac**2
                self.leftsse += dlfrac**2
                self.rightsse += drfrac**2

            if detect_face: self.face_successes += 1
            if detect_b25: self.both25_successes += 1
            if detect_l25: self.left25_successes += 1
            if detect_r25: self.right25_successes += 1
            if detect_b10: self.both10_successes += 1
            if detect_l10: self.left10_successes += 1
            if detect_r10: self.right10_successes += 1
            if detect_b05: self.both05_successes += 1
            if detect_l05: self.left05_successes += 1
            if detect_r05: self.right05_successes += 1