def model_build(path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "res", "train"), feature=PCA(), dist_metric=EuclideanDistance(), k=1, sz=None): model_fn = os.path.join(path, "mdl.pkl") if not os.path.isfile(model_fn): [X,y] = read_images(path, sz=sz) classifier = NearestNeighbor(dist_metric=dist_metric, k=k) model = PredictableModel(feature=feature, classifier=classifier) model.compute(X, y) save_model(model_fn, model) return load_model(model_fn)
def create_model_file(username, image_path, feature, classifier): # read images and set labels [X, y] = read_images(image_path) # Define the model as the combination model = PredictableModel(feature=feature.value, classifier=classifier.value) # Compute the Fisherfaces on the given data (in X) and labels (in y): model.compute(X, y) # We then save the model, which uses Pythons pickle module: model_name = username + "_model.pkl" save_model(model_name, model)
def entrenarModelo(dirImagenes = None, arcModelo = arcModelo): if dirImagenes is None: print dirImagenes return 0 [X,y,clases] = read_images(sys.argv[2]) modelo = PredictableModel(feature=Fisherfaces(), classifier=NearestNeighbor(dist_metric=EuclideanDistance(), k=1)) #configuración del modelo modelo.compute(X, y) pkl = open(arcModelo, 'wb') cPickle.dump([modelo,clases,tamanioCara],pkl) #se usa cPickle directamente en vez de save_model para poder insertar metadata pkl.close() validacion = KFoldCrossValidation(modelo, k=10) validacion.validate(X, y) validacion.print_results()
class App(object): def __init__( self, video_src, dataset_fn, face_sz=(130, 130), cascade_fn="/home/philipp/projects/opencv2/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_alt2.xml" ): self.face_sz = face_sz self.cam = create_capture(video_src) ret, self.frame = self.cam.read() self.detector = CascadedDetector(cascade_fn=cascade_fn, minNeighbors=5, scaleFactor=1.1) # define feature extraction chain & and classifier) feature = ChainOperator(TanTriggsPreprocessing(), LBP()) classifier = NearestNeighbor(dist_metric=ChiSquareDistance()) # build the predictable model self.predictor = PredictableModel(feature, classifier) # read the data & compute the predictor self.dataSet = DataSet(filename=dataset_fn, sz=self.face_sz) self.predictor.compute(self.dataSet.data, self.dataSet.labels) def run(self): while True: ret, frame = self.cam.read() # resize the frame to half the original size img = cv2.resize(frame, (frame.shape[1] / 2, frame.shape[0] / 2), interpolation=cv2.INTER_CUBIC) imgout = img.copy() for i, r in enumerate(self.detector.detect(img)): x0, y0, x1, y1 = r # get face, convert to grayscale & resize to face_sz face = img[y0:y1, x0:x1] face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) face = cv2.resize(face, self.face_sz, interpolation=cv2.INTER_CUBIC) # get a prediction prediction = self.predictor.predict(face)[0] # draw the face area cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 255, 0), 2) # draw the predicted name (folder name...) draw_str(imgout, (x0 - 20, y0 - 20), self.dataSet.names[prediction]) cv2.imshow('videofacerec', imgout) # get pressed key ch = cv2.waitKey(10) if ch == 27: break
def computeAndSaveModel(path_to_database, path_for_model_output, size, model_type="Fisherfaces", num_components=0, classifier_neighbours=1): print "\n[+] Saving new model (confirmed below)." [X,y,names] = read_images(path_to_database, sz=size) if model_type == "Eigenfaces": model = PredictableModel(PCA(num_components=num_components), NearestNeighbor(k=classifier_neighbours), dimensions=size, namesDict=names) elif model_type == "Fisherfaces": model = PredictableModel(Fisherfaces(num_components=num_components), NearestNeighbor(k=classifier_neighbours), dimensions=size, namesDict=names) else: print "[-] specify the type of model you want to comput as either 'Fisherface' or 'Eigenface' in the computeAndSaveModel function." return False model.compute(X,y) save_model(path_for_model_output, model) print "\n[+] Saving confirmed. New model saved to:", path_for_model_output
def create_model_db(user, modelpath, feature, classifier, setsize=None): [X, y], testpersons = read_images_db(user, setsize) # Define the model as the combination model = PredictableModel(feature=feature.value, classifier=classifier.value) # Compute the feature-algorithm on the given data (in X) and labels (in y): model.compute(X, y) # We then save the model, which uses Pythons pickle module: model_name = "{}_{}_model.pkl".format(user.username, user.id) testpersons_name = "{}_{}_testpersons.pkl".format(user.username, user.id) #save_model(os.path.join(modelpath, model_name), model) #with open(os.path.join(modelpath, testpersons_name), "w") as picklefile: # pickle.dump(testpersons, picklefile) return model, testpersons
class App(object): def __init__(self, video_src, dataset_fn, face_sz=(130,130), cascade_fn=join(curpath, 'haarcascade_frontalface_alt2.xml')): self.face_sz = face_sz self.cam = create_capture(video_src) ret, self.frame = self.cam.read() self.detector = CascadedDetector(cascade_fn=cascade_fn, minNeighbors=5, scaleFactor=1.1) # define feature extraction chain & and classifier) feature = ChainOperator(TanTriggsPreprocessing(), LBP()) classifier = NearestNeighbor(dist_metric=ChiSquareDistance()) # build the predictable model self.predictor = PredictableModel(feature, classifier) # read the data & compute the predictor self.dataSet = DataSet(filename=dataset_fn,sz=self.face_sz) self.predictor.compute(self.dataSet.data,self.dataSet.labels) def run(self): while True: ret, frame = self.cam.read() # resize the frame to half the original size img = cv2.resize(frame, (frame.shape[1]/2, frame.shape[0]/2), interpolation = cv2.INTER_CUBIC) imgout = img.copy() for i,r in enumerate(self.detector.detect(img)): x0,y0,x1,y1 = r # get face, convert to grayscale & resize to face_sz face = img[y0:y1, x0:x1] face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY) face = cv2.resize(face, self.face_sz, interpolation = cv2.INTER_CUBIC) # get a prediction prediction = self.predictor.predict(face) # draw the face area cv2.rectangle(imgout, (x0,y0),(x1,y1),(0,255,0),2) # draw the predicted name (folder name...) draw_str(imgout, (x0-20,y0-20), self.dataSet.names[prediction]) cv2.imshow('videofacerec', imgout) # get pressed key ch = cv2.waitKey(10) if ch == 27: break
def train(train_path): # Now read in the image data. This must be a valid path! [X, y, class_names] = read_images(train_path) print X, y, class_names # Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # Define the model as the combination model = PredictableModel(feature=feature, classifier=classifier) # Compute the Fisherfaces on the given data (in X) and labels (in y): model.compute(X, y) # Then turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:, i].reshape(X[0].shape) E.append(minmax_normalize(e, 0, 255, dtype=np.uint8)) # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # Perform a 10-fold cross validation cv = KFoldCrossValidation(model, k=10) cv.validate(X, y) # And print the result: cv.print_results() save_model('model.pkl', model, class_names) return [model, class_names]
def test_one_method(input_faces, test_faces, feature, classifier, chain=True): if chain: feature = ChainOperator(TanTriggsPreprocessing(), feature) model = PredictableModel(feature, classifier) id_list, face_list = zip(*input_faces) start = time.clock() model.compute(face_list, id_list) stop = time.clock() training_time = stop-start res_list = [] start = time.clock() for id, image in test_faces: res = model.predict(image) res_list.append([id]+res) stop = time.clock() predict_time = stop-start return (training_time, predict_time, res_list)
def checkFace(origin_img): #To do model = PredictableModel(Fisherfaces(), NearestNeighbor()) result_name = 'unknown' [X,y,subject_names] = read_images(path) list_of_labels = list(xrange(max(y)+1)) subject_dictionary = dict(zip(list_of_labels, subject_names)) model.compute(X,y) gray = cv2.cvtColor(origin_img, cv2.COLOR_BGR2GRAY) sampleImage = cv2.resize(gray, (256,256)) [ predicted_label, generic_classifier_output] = model.predict(sampleImage) print [ predicted_label, generic_classifier_output] if int(generic_classifier_output['distances']) <= 700: result_name = str(subject_dictionary[predicted_label]) return result_name
def train(train_path): # Now read in the image data. This must be a valid path! [X,y,class_names] = read_images(train_path) print X,y,class_names # Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # Define the model as the combination model = PredictableModel(feature=feature, classifier=classifier) # Compute the Fisherfaces on the given data (in X) and labels (in y): model.compute(X, y) # Then turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:,i].reshape(X[0].shape) E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # Perform a 10-fold cross validation cv = KFoldCrossValidation(model, k=10) cv.validate(X, y) # And print the result: cv.print_results() save_model('model.pkl', model, class_names) return [model,class_names]
# input_faces = [(a, b) for a, b in input_faces if b is not None] print "nulls removed, ", len(input_faces) # images in one list, id's on another id_list, face_list = zip(*input_faces) # print "saving images" # utils.save_images(face_list) # show random image # utils.show_image_and_wait_for_input(face_list[len(face_list)/2]) print "Train the model" start = time.clock() # model.compute(X, y) model.compute(face_list, id_list) stop = time.clock() print "Training done in", stop - start, " next...find a face" target = "10.bmp" if len(sys.argv) > 3: target = sys.argv[3] fp = utils.FaceProcessor() while target != "quit": # prufu_mynd = Image.open(os.path.join(path, target)) prufu_mynd = cv2.imread(os.path.join(path, target)) print "Nota mynd: ", os.path.join(path, target) if prufu_mynd is not None: prufu_mynd = fp.process_image(prufu_mynd)
# Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # Define the model as the combination my_model = PredictableModel(feature=feature, classifier=classifier) # Compute the Fisherfaces on the given data (in X) and labels (in y): my_model.compute(X, y) # We then save the model, which uses Pythons pickle module: save_model('model.pkl', my_model) model = load_model('model.pkl') # Then turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:,i].reshape(X[0].shape) E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # Perform a 10-fold cross validation cv = KFoldCrossValidation(model, k=10) cv.validate(X, y) # And print the result:
class FaceDatabase(object): def __init__(self, database_folder, feature_parameter="LPQ", metric="chi", k=3): self.model = None handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) path = database_folder start = time.clock() input_faces = utils.read_images_from_single_folder(path) stop = time.clock() print("read {}, images from {} in {} seconds.".format( len(input_faces), path, stop - start)) feature = None m = { "fisher": Fisherfaces, "fisher80": Fisherfaces, "pca": PCA, "pca10": PCA, "lda": LDA, "spatial": SpatialHistogram, "LPQ": SpatialHistogram } if feature_parameter in m: if feature_parameter == 'LPQ': feature = SpatialHistogram(LPQ()) self.threshold = threshold_function(71.4, 70) elif feature_parameter == 'fisher80': feature = Fisherfaces(80) self.threshold = threshold_function(0.61, 0.5) elif feature_parameter == 'fisher': feature = Fisherfaces() self.threshold = threshold_function(0.61, 0.5) elif feature_parameter == 'pca80': feature = PCA(80) else: feature = m[feature_parameter]() metric_param = None d = { "euclid": EuclideanDistance, "cosine": CosineDistance, "normal": NormalizedCorrelation, "chi": ChiSquareDistance, "histo": HistogramIntersection, "l1b": L1BinRatioDistance, "chibrd": ChiSquareBRD } if metric in d: metric_param = d[metric]() else: metric_param = ChiSquareDistance() classifier = NearestNeighbor(dist_metric=metric_param, k=k) feature = ChainOperator(TanTriggsPreprocessing(), feature) # feature = ChainOperator(TanTriggsPreprocessing(0.1, 10.0, 1.0, 3.0), feature) self.model = PredictableModel(feature, classifier) # images in one list, id's on another id_list, face_list = zip(*input_faces) print "Train the model" start = time.clock() # model.compute(X, y) self.model.compute(face_list, id_list) stop = time.clock() print "Training done in", stop - start, " next...find a face" # threshold_lpq_normalized = threshold_function(0.67, 0.3) # threshold_lpq_chisquared = threshold_function(71.4, 70) # threshold_spatial_cosine = threshold_function(0.908, 0.908) # threshold_spatial_chisuearbrd = threshold_function() # threshold = threshold_lpq_normalized def find_face(self, input_face_image): assert self.model, "Model is not valid" res = self.model.predict(input_face_image) print res return self.threshold(res)
# Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # Define the model as the combination my_model = PredictableModel(feature=feature, classifier=classifier) # Compute the Fisherfaces on the given data (in X) and labels (in y): my_model.compute(X, y) # We then save the model, which uses Pythons pickle module: save_model('model.pkl', my_model) model = load_model('model.pkl') # Then turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in range(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:,i].reshape(X[0].shape) E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # Perform a 10-fold cross validation cv = KFoldCrossValidation(model, k=10) cv.validate(X, y) # And print the result:
def run(): # This is where we write the images, if an output_dir is given # in command line: # out_dir = None # You'll need at least a path to your image data, please see # the tutorial coming with this source code on how to prepare # your image data: # if len(sys.argv) < 2: # print ("USAGE: facerec_demo.py </path/to/images>") # sys.exit() # Now read in the image data. This must be a valid path! # [X,y] = read_images(sys.argv[1]) [X, y] = read_images('../data/trainset/') # dataset = FilesystemReader(sys.argv[1]) # Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: svm = SVM(C=0.1, kernel='rbf', degree=4, gamma='auto', coef0=0.0) knn = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # # Define the model as the combination model_svm = PredictableModel(feature=feature, classifier=svm) model_knn = PredictableModel(feature=feature, classifier=knn) # # Compute the Fisherfaces on the given data (in X) and labels (in y): model_svm.compute(X, y) model_knn.compute(X, y) # E = [] # for i in range(min(model.feature.eigenvectors.shape[1], 16)): # e = model.feature.eigenvectors[:,i].reshape(X[0].shape) # E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # cv = LeaveOneOutCrossValidation(model) # print(cv0) # cv0.validate(dataset.data,dataset.classes,print_debug=True) cv_svm = KFoldCrossValidation(model_svm, k=10) cv_knn = KFoldCrossValidation(model_knn, k=10) param_grid = [ { 'C': [0.05, 0.1, 0.3, 0.5, 1, 2, 5], 'gamma': [0.001, 0.0001], 'kernel': ['rbf'] }, ] [tX, tY] = read_images('../data/testset/') # cv_svm.validate(X, y) # cv_knn.validate(X, y) gs(model_svm, X, y, param_grid) count1 = 0 count2 = 0 for i in range(len(tY)): r1 = model_svm.predict(tX[i]) r2 = model_knn.predict(tX[i]) if r1[0] == tY[i]: count1 += 1 if r2[0] == tY[i]: count2 += 1 print('SVM ACC:{0}'.format(count1 / len(tY))) print('KNN ACC:{0}'.format(count2 / len(tY))) print(cv_knn.print_results()) print(cv_svm.print_results())
class FaceDatabase(object): def __init__(self, database_folder, feature_parameter="LPQ", metric="chi", k=3): self.model = None handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) path = database_folder start = time.clock() input_faces = utils.read_images_from_single_folder(path) stop = time.clock() print("read {}, images from {} in {} seconds.".format(len(input_faces), path, stop-start)) feature = None m = { "fisher": Fisherfaces, "fisher80": Fisherfaces, "pca": PCA, "pca10": PCA, "lda": LDA, "spatial": SpatialHistogram, "LPQ": SpatialHistogram } if feature_parameter in m: if feature_parameter == 'LPQ': feature = SpatialHistogram(LPQ()) self.threshold = threshold_function(71.4, 70) elif feature_parameter == 'fisher80': feature = Fisherfaces(80) self.threshold = threshold_function(0.61, 0.5) elif feature_parameter == 'fisher': feature = Fisherfaces() self.threshold = threshold_function(0.61, 0.5) elif feature_parameter == 'pca80': feature = PCA(80) else: feature = m[feature_parameter]() metric_param = None d = {"euclid": EuclideanDistance, "cosine": CosineDistance, "normal": NormalizedCorrelation, "chi": ChiSquareDistance, "histo": HistogramIntersection, "l1b": L1BinRatioDistance, "chibrd": ChiSquareBRD } if metric in d: metric_param = d[metric]() else: metric_param = ChiSquareDistance() classifier = NearestNeighbor(dist_metric=metric_param, k=k) feature = ChainOperator(TanTriggsPreprocessing(), feature) # feature = ChainOperator(TanTriggsPreprocessing(0.1, 10.0, 1.0, 3.0), feature) self.model = PredictableModel(feature, classifier) # images in one list, id's on another id_list, face_list = zip(*input_faces) print "Train the model" start = time.clock() # model.compute(X, y) self.model.compute(face_list, id_list) stop = time.clock() print "Training done in", stop-start, " next...find a face" # threshold_lpq_normalized = threshold_function(0.67, 0.3) # threshold_lpq_chisquared = threshold_function(71.4, 70) # threshold_spatial_cosine = threshold_function(0.908, 0.908) # threshold_spatial_chisuearbrd = threshold_function() # threshold = threshold_lpq_normalized def find_face(self, input_face_image): assert self.model, "Model is not valid" res = self.model.predict(input_face_image) print res return self.threshold(res)
sampleImage = cv2.resize(sampleImage, (256, 256)) [a1, a2] = model.predict(sampleImage) cv2.imshow('Result', img) if int(a2['distances']) <= 10000: cv2.putText(img, 'You are found', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 250), 3, 1) else: cv2.putText(img, 'Who dat', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 250), 3, 1) ret = 0 cv2.imshow('result', img) cv2.waitKey(10) cv2.destroyAllWindows() return ret init() [X, y, subject_names] = read_images(pathdir) list_of_labels = list(xrange(max(y) + 1)) subject_dictionary = dict(zip(list_of_labels, subject_names)) model.compute(X, y) while 1: ans = judge() cv2.destroyAllWindows() vc.release()
[X,y] = read_images(sys.argv[1]) # Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a 1-NN classifier with Euclidean Distance: classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # Define the model as the combination model = PredictableModel(feature=feature, classifier=classifier) # Compute the Fisherfaces on the given data (in X) and labels (in y): model.compute(X, y) # Then turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:,i].reshape(X[0].shape) E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.png") # Perform a 10-fold cross validation cv = KFoldCrossValidation(model, k=10) cv.validate(X, y) # And print the result: print cv
##count = 0 ##if not os.path.exists(os.path.join(pathdir+name)): ## os.makedirs(os.path.join(pathdir+name)) ## print 'No path' ###################################### #Going through the database [X,y,subject_names] = read_images(pathdir) #Creates a list of the number of members list_of_labels = list(xrange(max(y)+1)) #Maps a dictionary between the numbers and the names of the individuals subject_dictionary = dict(zip(list_of_labels, subject_names)) #Using the 3 Models to compute Similarities Based on Data Sets model.compute(X,y) model1.compute(X,y) model2.compute(X,y) ###################################### #Loading the Pictures pictures = open('links.txt','r') for i in pictures: i= i.strip() if i[:4] == 'http': req = urllib.urlopen(i) arr = np.asarray(bytearray(req.read()), dtype=np.uint8) #Each picture we analyze is stored in img img = cv2.imdecode(arr, -1) #Now doing facial Detection. For more information, refer to Facial Detection.py. The process is the same as the one done here
# [ 1, first_name in database] # [ 2, second_name in database] # ... # [ n, last_name in database] # # This dictionary is used in for the greeting and labeling # list_of_labels = list(xrange(max(y)+1)) subject_dictionary = dict(zip(list_of_labels, subject_names)) # # This constructs the linear distriminant analysis matrix, which is used for facial identification # initial_time = time.time() print "Constructing linear discriminant analysis matrix for facial identification: " model.compute(Z,y) print "Construction completed in {0:.2f} seconds.\n".format(time.time() - initial_time) current_state = "Tracking" # # Get a new frame from the webcam # rval, frame = vc.read() # # Copy the frame adn convert the whole thing to black and white to make recognition easier # img = frame rows,cols,ch = frame.shape
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # add handler to facerec modules logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # load a dataset (e.g. AT&T Facedatabase) dataSet = DataSet("/root/libface/img/yalefaces") # define Fisherfaces as feature extraction method feature = Fisherfaces() # define a 1-NN classifier with Euclidean Distance classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) # define the model as the combination model = PredictableModel(feature=feature, classifier=classifier) # show fisherfaces model.compute(dataSet.data, dataSet.labels) #try to recgonize im = Image.open("/root/libface/img/reg.jpg") im = im.convert("L") ar = [] ar.append(np.asarray(im, dtype=np.uint8)) print(dataSet.names[model.predict(ar)]) # turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) """ E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:,i].reshape(dataSet.data[0].shape) E.append(minmax_normalize(e,0,255, dtype=np.uint8)) # plot them and store the plot to "python_fisherfaces_fisherfaces.pdf"
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) dataSet = DataSet("/home/philipp/facerec/data/yalefaces_recognition") feature = Fisherfaces() classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) model = PredictableModel(feature=feature, classifier=classifier) model.compute(dataSet.data, dataSet.labels) E = [] for i in xrange(min(model.feature.eigenvectors.shape[1], 16)): e = model.feature.eigenvectors[:, i].reshape(dataSet.data[0].shape) E.append(minmax_normalize(e, 0, 255, dtype=np.uint8)) subplot(title="Fisherfaces", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="fisherfaces.pdf") cv = KFoldCrossValidation(model, k=10)
Xtrain,ytrain=read_images('/home/rishabh/f1/faces aviral train/',(100,129)) Xtest,ytest=read_images('/home/rishabh/f1/faces aviral/',(100,129)) mod1=PredictableModel(PCA(num_components=50),NearestNeighbor(k=1)) mod2=PredictableModel(PCA(num_components=50),NearestNeighbor(k=1,dist_metric=CosineDistance())) mod3=PredictableModel(Fisherfaces(num_components=50),NearestNeighbor(k=1)) mod4=PredictableModel(Fisherfaces(num_components=50),NearestNeighbor(k=1,dist_metric=CosineDistance())) mod5=PredictableModel(SpatialHistogram(),NearestNeighbor(k=1)) mod6=PredictableModel(SpatialHistogram(),NearestNeighbor(k=1,dist_metric=CosineDistance())) mod7=PredictableModel(SpatialHistogram(lbp_operator=LPQ()),NearestNeighbor(k=1)) mod8=PredictableModel(SpatialHistogram(lbp_operator=LPQ()),NearestNeighbor(k=1,dist_metric=CosineDistance())) mod9=PredictableModel(SpatialHistogram(),NearestNeighbor(k=1,dist_metric=ChiSquareDistance())) mod10=PredictableModel(SpatialHistogram(),NearestNeighbor(k=1,dist_metric=NormalizedCorrelation())) mod1.compute(Xtrain,ytrain) mod2.compute(Xtrain,ytrain) mod3.compute(Xtrain,ytrain) mod4.compute(Xtrain,ytrain) mod5.compute(Xtrain,ytrain) mod6.compute(Xtrain,ytrain) mod7.compute(Xtrain,ytrain) mod8.compute(Xtrain,ytrain) mod9.compute(Xtrain,ytrain) mod10.compute(Xtrain,ytrain) #For Training Size 3 p=np.array(np.ones(len(Xtest))*9,dtype=int) count=0
# Define the model as the combination # model = PredictableModel(feature=feature, classifier=classifier) # Compute the model on the given data (in X) and labels (in y): feature = ChainOperator(TanTriggsPreprocessing(), feature) # classifier = NearestNeighbor() model = PredictableModel(feature, classifier) # images in one list, id's on another id_list, face_list = zip(*input_faces) print "Train the model" start = time.clock() # model.compute(X, y) model.compute(face_list, id_list) stop = time.clock() print "Training done in", stop-start, " next...find a face" # test_path = "/Users/matti/Documents/forritun/att_faces/" test_path = "/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_02" """ target = "10.bmp" if len(sys.argv) > 3: target = sys.argv[3] """ fp = utils.FaceProcessor() test_list = [ ("10.bmp", 41),