def __init__(self, face_detector=CascadeDetector(), tile_size=(128,128), validate=None, n_iter=1, annotate=False,**kwargs): ''' Create an eye locator. This default implentation uses a cascade classifier for face detection and then SVR for eye location. ''' self.face_detector = face_detector self.left_eye = None self.right_eye = None self.tile_size = tile_size self.validate = validate self.n_iter = n_iter self.annotate = annotate self.perturbations = True # this object handles pca normalization self.normalize = VectorClassifier.VectorClassifier( VectorClassifier.TYPE_REGRESSION, reg_norm=VectorClassifier.REG_NORM_NONE) # point locators that learn to find the eyes. self.left_locator = SVMLocator(svm_type=SVM.TYPE_NU_SVR ,normalization=VectorClassifier.NORM_NONE) self.right_locator = SVMLocator(svm_type=SVM.TYPE_NU_SVR ,normalization=VectorClassifier.NORM_NONE) # Number of training images where the face detection did not work. self.detection_failures = 0 self.training_labels = []
def __init__(self, face_detector=CascadeDetector(), tile_size=(128, 128), validate=None, n_iter=1, annotate=False, **kwargs): ''' Create an eye locator. This default implentation uses a cascade classifier for face detection and then SVR for eye location. ''' self.face_detector = face_detector self.left_eye = None self.right_eye = None self.tile_size = tile_size self.validate = validate self.n_iter = n_iter self.annotate = annotate self.perturbations = True # this object handles pca normalization self.normalize = VectorClassifier.VectorClassifier( VectorClassifier.TYPE_REGRESSION, reg_norm=VectorClassifier.REG_NORM_NONE) # point locators that learn to find the eyes. self.left_locator = SVMLocator( svm_type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_NONE) self.right_locator = SVMLocator( svm_type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_NONE) # Number of training images where the face detection did not work. self.detection_failures = 0 self.training_labels = []
class SVMEyeDetector: ''' This class detects faces and then returns the detection rectangles and the eye coordinates. ''' def __init__(self, face_detector=CascadeDetector(), tile_size=(128, 128), validate=None, n_iter=1, annotate=False, **kwargs): ''' Create an eye locator. This default implentation uses a cascade classifier for face detection and then SVR for eye location. ''' self.face_detector = face_detector self.left_eye = None self.right_eye = None self.tile_size = tile_size self.validate = validate self.n_iter = n_iter self.annotate = annotate self.perturbations = True # this object handles pca normalization self.normalize = VectorClassifier.VectorClassifier( VectorClassifier.TYPE_REGRESSION, reg_norm=VectorClassifier.REG_NORM_NONE) # point locators that learn to find the eyes. self.left_locator = SVMLocator( svm_type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_NONE) self.right_locator = SVMLocator( svm_type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_NONE) # Number of training images where the face detection did not work. self.detection_failures = 0 self.training_labels = [] 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 train(self, **kwargs): ''' Train the eye locators. ''' self.normalize.trainNormalization() vectors = self.normalize.vectors #print len(self.training_labels) #print vectors.shape for i in range(len(self.training_labels)): leye, reye = self.training_labels[i] vec = vectors[i] self.left_locator.addTraining(vec, leye) self.right_locator.addTraining(vec, reye) self.left_locator.train(**kwargs) self.right_locator.train(**kwargs) self.left_eye = self.left_locator.mean self.right_eye = self.right_locator.mean del self.normalize.labels del self.normalize.vectors del self.training_labels def detect(self, im): ''' @returns: a list of tuples where each tuple contains (registered_image, detection_rect, left_eye, right_eye) ''' result = [] rects = self.face_detector.detect(im) # Anotate Faces for rect in rects: # Transform the face affine = pv.AffineFromRect(rect, self.tile_size) cropped = affine.transformImage(im) for _ in range(self.n_iter): cropped = pv.meanStd(cropped) # Find the eyes data = cropped.asMatrix2D().flatten() data = np.array(data, 'd').flatten() data = self.normalize.normalizeVector(data) pleye = self.left_locator.predict(data) preye = self.right_locator.predict(data) pleye = affine.invertPoint(pleye) preye = affine.invertPoint(preye) # Seccond Pass affine = pv.AffineFromPoints(pleye, preye, self.left_eye, self.right_eye, self.tile_size) cropped = affine.transformImage(im) #affine = AffineFromPoints(pleye,preye,self.left_eye,self.right_eye,self.tile_size) #reg = affine.transformImage(im) reg = cropped if self.validate != None and not self.validate(reg): # Validate the face. if self.annotate: im.annotateRect(rect, color='red') im.annotatePoint(pleye, color='red') im.annotatePoint(preye, color='red') continue if self.annotate: reg.annotatePoint(self.left_eye, color='green') reg.annotatePoint(self.right_eye, color='green') im.annotatePoint(pleye, color='green') im.annotatePoint(preye, color='green') im.annotateRect(rect, color='green') result.append((reg, rect, pleye, preye)) return result
def createLocators(self, **kwargs): ''' Create two point locators that use the methods of interest ''' self.left_locator = SVMLocator( type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_VALUE) self.right_locator = SVMLocator( type=SVM.TYPE_NU_SVR, normalization=VectorClassifier.NORM_VALUE)
class SVMEyeDetector: ''' This class detects faces and then returns the detection rectangles and the eye coordinates. ''' def __init__(self, face_detector=CascadeDetector(), tile_size=(128,128), validate=None, n_iter=1, annotate=False,**kwargs): ''' Create an eye locator. This default implentation uses a cascade classifier for face detection and then SVR for eye location. ''' self.face_detector = face_detector self.left_eye = None self.right_eye = None self.tile_size = tile_size self.validate = validate self.n_iter = n_iter self.annotate = annotate self.perturbations = True # this object handles pca normalization self.normalize = VectorClassifier.VectorClassifier( VectorClassifier.TYPE_REGRESSION, reg_norm=VectorClassifier.REG_NORM_NONE) # point locators that learn to find the eyes. self.left_locator = SVMLocator(svm_type=SVM.TYPE_NU_SVR ,normalization=VectorClassifier.NORM_NONE) self.right_locator = SVMLocator(svm_type=SVM.TYPE_NU_SVR ,normalization=VectorClassifier.NORM_NONE) # Number of training images where the face detection did not work. self.detection_failures = 0 self.training_labels = [] 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 train(self,**kwargs): ''' Train the eye locators. ''' self.normalize.trainNormalization() vectors = self.normalize.vectors #print len(self.training_labels) #print vectors.shape for i in range(len(self.training_labels)): leye,reye = self.training_labels[i] vec = vectors[i] self.left_locator.addTraining(vec,leye) self.right_locator.addTraining(vec,reye) self.left_locator.train(**kwargs) self.right_locator.train(**kwargs) self.left_eye = self.left_locator.mean self.right_eye = self.right_locator.mean del self.normalize.labels del self.normalize.vectors del self.training_labels def detect(self, im): ''' @returns: a list of tuples where each tuple contains (registered_image, detection_rect, left_eye, right_eye) ''' result = [] rects = self.face_detector.detect(im) # Anotate Faces for rect in rects: # Transform the face affine = pv.AffineFromRect(rect,self.tile_size) cropped = affine.transformImage(im) for _ in range(self.n_iter): cropped = pv.meanStd(cropped) # Find the eyes data = cropped.asMatrix2D().flatten() data = np.array(data,'d').flatten() data = self.normalize.normalizeVector(data) pleye = self.left_locator.predict(data) preye = self.right_locator.predict(data) pleye = affine.invertPoint(pleye) preye = affine.invertPoint(preye) # Seccond Pass affine = pv.AffineFromPoints(pleye,preye,self.left_eye,self.right_eye,self.tile_size) cropped = affine.transformImage(im) #affine = AffineFromPoints(pleye,preye,self.left_eye,self.right_eye,self.tile_size) #reg = affine.transformImage(im) reg = cropped if self.validate != None and not self.validate(reg): # Validate the face. if self.annotate: im.annotateRect(rect,color='red') im.annotatePoint(pleye,color='red') im.annotatePoint(preye,color='red') continue if self.annotate: reg.annotatePoint(self.left_eye,color='green') reg.annotatePoint(self.right_eye,color='green') im.annotatePoint(pleye,color='green') im.annotatePoint(preye,color='green') im.annotateRect(rect,color='green') result.append((reg,rect,pleye,preye)) return result