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()
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
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 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 get_model(numeric_dataset, model_filename=None): feature = ChainOperator(Resize((128,128)), Fisherfaces()) classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=1) inner_model = PredictableModel(feature=feature, classifier=classifier) model = PredictableModelWrapper(inner_model) model.set_data(numeric_dataset) model.compute() if not model_filename is None: save_model(model_filename, model) return model
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 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 __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 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 __init__(self, video_src, dataset_fn, face_sz=(130,130), cascade_fn="/Users/george/job/__webdocs/webguerillas/opencv/project/facereg/opencv/OpenCV-2.4.2/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 __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 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]
class ProPos: face_cascade = cv2.CascadeClassifier( 'haarcascades/haarcascade_frontalface_default.xml') model = PredictableModel(Fisherfaces(), NearestNeighbor()) cap = cv2.VideoCapture(0) camera_id = 1 url = 'http://localhost:8080/' last_face = '' recognizer = cv2.createLBPHFaceRecognizer() path = 'img_db/' def __init__(self): print 'Working' def read_images(self, path, size=(256, 256)): c = 0 x, y = [], [] folder_names = [] for dirname, dirnames, filenames in os.walk(path): for subdirname in dirnames: folder_names.append(subdirname) subject_path = os.path.join(dirname, subdirname) for filename in os.listdir(subject_path): try: im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE) # resize to given size (if given) if (size is not None): im = cv2.resize(im, size) x.append(np.asarray(im, dtype=np.uint8)) y.append(c) except IOError, (errno, strerror): print "I/O error({0}): {1}".format(errno, strerror) except: print "Unexpected error:", sys.exc_info()[0] raise
print "USAGE: lpq_experiment.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]) # 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) # The models we want to evaluate: model0 = PredictableModel(feature=PCA(num_components=50), classifier=NearestNeighbor( dist_metric=EuclideanDistance(), k=1)) model1 = PredictableModel(feature=Fisherfaces(), classifier=NearestNeighbor( dist_metric=EuclideanDistance(), k=1)) model2 = PredictableModel( feature=SpatialHistogram(lbp_operator=ExtendedLBP()), classifier=NearestNeighbor(dist_metric=ChiSquareDistance(), k=1)) model3 = PredictableModel(feature=SpatialHistogram(lbp_operator=LPQ()), classifier=NearestNeighbor( dist_metric=ChiSquareDistance(), k=1)) # I should rewrite the framework to offer a less memory-intense solution here: cv0 = KFoldCrossValidation(model0, k=10) cv1 = KFoldCrossValidation(model1, k=10) cv2 = KFoldCrossValidation(model2, k=10) cv3 = KFoldCrossValidation(model3, k=10)
# Now read in the image data. This must be a valid path! [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) prediction = model.predict(X) predicted_label = prediction[0] classifier_output = prediction[1] distance = classifier_output['distances'][0] print distance 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")
if len(sys.argv) > 2: feature_parameter = sys.argv[2] m = { "fisher": Fisherfaces, "pca": PCA, "lda": LDA, "spatial": SpatialHistogram } if feature_parameter in m: feature = m[feature_parameter]() # 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 model on the given data (in X) and labels (in y): print "processing input images, ", len(input_faces) input_faces = utils.convert_all_files(input_faces) # remove null faces # 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
# # Set up the Haar cascade to detect (not recognize) the faces # # # We're going to use the Fisherfaces face recognition module # initial_time = time.time() print "Initializing Haar cascades for face, eyes, nose and mouth detection: " # # This was prior to using the TanTriggsPreprocessing, we can go back #model = PredictableModel(Fisherfaces(), NearestNeighbor()) feature = ChainOperator(TanTriggsPreprocessing(), Fisherfaces()) classifier = NearestNeighbor() model = PredictableModel(feature, classifier) face_cascade = cv2.CascadeClassifier(haarcascade) eye_cascade = cv2.CascadeClassifier(eyehaarcascade) nose_cascade = cv2.CascadeClassifier(nosehaarcascade) mouth_cascade = cv2.CascadeClassifier(mouthhaarcascade) print "Initialization completed in {0:.2f} seconds.\n".format(time.time() - initial_time) # # Main loop # Press "l" to learn a new image # Press "r" to reload image database # Press "v" to toggle voice synthesis # Press "b" for best guess of image # Press "e" to toggle eye detection # Press "p" to preprocess pictures using TanTriggs
def __init__(self, feature, classifier, image_size, subject_names): PredictableModel.__init__(self, feature=feature, classifier=classifier) self.image_size = image_size self.subject_names = subject_names
# print "read images" # print len(X),len(y) # 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): #--------------------------------------------- # print "Generating model" if(not os.path.exists("./temp/mymodel")): model.compute(X, y) save_model("./temp/mymodel", model) #saving model here - CHANGE THIS exit() # print "loading model" model = load_model("./temp/mymodel") # print "loaded model" urlForImage = sys.argv[2] tmpfilename = "./temp/"+str(urlForImage.split('/')[-1]) #saving image here - CHANGE THIS urllib.urlretrieve(urlForImage, tmpfilename) im = Image.open(tmpfilename) #add rotate of 90? Don't think so.
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"
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) 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")
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) logger.debug("Iniciando treinamento.") # Define the Fisherfaces as Feature Extraction method: feature = Fisherfaces() # Define a k-NN classifier classifier = NearestNeighbor(dist_metric=CosineDistance(), k=5) # 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) logger.debug(model) # 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 "fisherfaces.png" subplot(title="Fisherfaces", images=E, rows=4,
# Define a 1-NN classifier with Euclidean Distance: # classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=3) # Ć¾okkalegt, [1.7472255 , 1.80661233, 1.89985602] bara fremsta rĆ©tt # classifier = NearestNeighbor(dist_metric=CosineDistance(), k=3) # Ć¾okkalegt, niĆ°urstƶưur sem mĆnus tƶlur ([-0.72430667, -0.65913855, -0.61865271]) # classifier = NearestNeighbor(dist_metric=NormalizedCorrelation(), k=3) # Ć”gƦtt 0.28873109, 0.35998333, 0.39835315 (bara fremsta rĆ©tt) classifier = NearestNeighbor(dist_metric=ChiSquareDistance(), k=3) # gott, 32.49907228, 44.53673458, 45.39480197 bara sĆĆ°asta rangt # classifier = NearestNeighbor(dist_metric=HistogramIntersection(), k=3) # sƶkkar # classifier = NearestNeighbor(dist_metric=L1BinRatioDistance(), k=3) # nokkuĆ° gott, 36.77156378, 47.84164013, 52.63872497] - sĆĆ°asta rangt # classifier = NearestNeighbor(dist_metric=ChiSquareBRD(), k=3) # 36.87781902, 44.06119053, 46.40875114 - sĆĆ°asta rangt # 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" """
# Define filters for the Dataset: yale_subset_0_40 = YaleBaseFilter(0, 40, 0, 40) # Now read in the image data. Apply filters, scale to 128 x 128 pixel: [X, y] = read_images(sys.argv[1], yale_subset_0_40, sz=(64, 64)) # 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.INFO) # The models we want to evaluate: model0 = PredictableModel( feature=SpatialHistogram(lbp_operator=ExtendedLBP()), classifier=NearestNeighbor(dist_metric=ChiSquareDistance(), k=1)) model1 = PredictableModel(feature=SpatialHistogram(lbp_operator=LPQ()), classifier=NearestNeighbor( dist_metric=ChiSquareDistance(), k=1)) # The sigmas we'll apply for each run: sigmas = [0] print('The experiment will be run %s times!' % ITER_MAX) # Initialize experiments (with empty results): experiments = {} experiments['lbp_model'] = { 'model': model0, 'results': {}, 'color': 'r', 'linestyle': '--', 'marker': '*'
# Now read in the image data. This must be a valid path! [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 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)
# load a dataset random.seed() dataSet = DataSet("/Users/gsj987/Desktop/ęÆč®¾čµę/faces_girls") #dataSet.shuffle() #idx = np.argsort([random.random() for i in xrange(len(dataSet.labels))]) #dataSet.labels = dataSet.labels[idx] for ind in range(20, 400, 20): # define a 1-NN classifier with Euclidean Distance classifier = SVM(svm_parameter("-t 1 -c 1 -g 1 -r 262144 -q")) # define Fisherfaces as feature extraction method feature = ChainOperator(HistogramEqualization(), PCA(ind)) #print feature.compute(dataSet.data, dataSet.labels) # now stuff them into a PredictableModel model = PredictableModel(feature=feature, classifier=classifier) # show fisherfaces model.compute(dataSet.data,dataSet.labels) #print model.feature.model2.eigenvectors.shape, dataSet.data #es = model.feature.model2.eigenvectors #img = smp.toimage(np.dot(es,dd[0]).reshape(120,120)) #img.save("pca100.jpg") #plot_eigenvectors(model.feature.model2.eigenvectors, 9, sz=dataSet.data[0].shape, filename=None) # perform a 5-fold cross validation cv = KFoldCrossValidation(model, 5) cv.validate(dataSet.data, dataSet.labels) print ind,cv.tp, cv.fp, "%.4f" %(cv.tp/(cv.tp+cv.fp+0.0001))
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"
# coding=utf-8 from facerec.feature import Fisherfaces from facerec.classifier import NearestNeighbor from facerec.model import PredictableModel from PIL import Image import numpy as np from PIL import Image import sys, os import time #sys.path.append("../..") import cv2 import multiprocessing model = PredictableModel(Fisherfaces(), NearestNeighbor()) cascPath = sys.argv[1] vc=cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier(cascPath) #una volta ottenuto (prossimo step) un db di facce, le def read_images(path, sz=(256,256)): """Reads the images in a given folder, resizes images on the fly if size is given. Args: path: Path to a folder with subfolders representing the subjects (persons). sz: A tuple with the size Resizes Returns: A list [X,y] X: The images, which is a Python list of numpy arrays.
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())
[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 = SVM() # Define the model as the combination model = PredictableModel(feature=feature, classifier=classifier) # Compute a model: model.compute(X, y) # Save the Model using joblib: save_model('model.pkl', model) # Perform a Grid Search for the Set of Parameters: tuned_parameters = [{ 'kernel': ['rbf'], 'gamma': [1e-3, 1e-4], 'C': [1, 10, 100, 1000] }, { 'kernel': ['linear'], 'C': [1, 10, 100, 1000] }] # Find a good set of parameters: grid_search(model, X, y, tuned_parameters)
from facerec.feature import Fisherfaces, PCA, Identity from facerec.classifier import NearestNeighbor from facerec.model import PredictableModel from PIL import Image import numpy as np from PIL import Image import sys, os import time #sys.path.append("../..") import cv2 import multiprocessing model = PredictableModel(PCA(), NearestNeighbor()) vc = cv2.VideoCapture(0) # Choosing the haar cascade for face detection face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml') # Reads the database of faces def read_images(path, sz=(256, 256)): # Reads the images in a given folder, resizes images on the fly if size is given. # Args: # path: Path to a folder with subfolders representing the subjects (persons). # sz: A tuple with the size Resizes # Returns: # A list [X,y] # X: The images, which is a Python list of numpy arrays. # y: The corresponding labels (the unique number of the subject, person) in a Python list. c = 0 X, y = [], []
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: # Now read in the image data. This must be a valid path! [X, y, keys] = read_images("../faces/") print len(X), keys # Then set up a handler for logging: 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)) img_path = 'rawand1.jpg' coverted_img_path = "temp_%s" % img_path detect_face(img_path, outfile=coverted_img_path) img = Image.open(coverted_img_path) img = img.convert("L")
# Now read in the image data. This must be a valid path! [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
from feature import Fisherfaces from facerec.classifier import NearestNeighbor from facerec.model import PredictableModel from PIL import Image import numpy as np from PIL import Image import sys, os import time #sys.path.append("../..") import cv2 import multiprocessing model = PredictableModel(Fisherfaces(), NearestNeighbor()) vc=cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('path to the classifier : haarcascade_frontalface_alt_tree.xml(suggerito)') #una volta ottenuto (prossimo step) un db di facce, le def read_images(path, sz=(256,256)): """Reads the images in a given folder, resizes images on the fly if size is given. Args: path: Path to a folder with subfolders representing the subjects (persons). sz: A tuple with the size Resizes Returns: A list [X,y]
# 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 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)
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: # Now read in the image data. This must be a valid path! [X, y, keys] = read_images("../faces/") print len(X), keys # Then set up a handler for logging: 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)) img_path = 'rawand1.jpg' coverted_img_path = "temp_%s" % img_path detect_face(img_path,outfile=coverted_img_path) img = Image.open(coverted_img_path)
if (sz is not None): im = im.resize(sz, Image.ANTIALIAS) X.append(np.asarray(im, dtype=np.uint8)) y.append(c) # except IOError, (errno, strerror): # print "I/O error({0}): {1}".format(errno, strerror) except: print "Unexpected error:", sys.exc_info()[0] raise c = c+1 return [X,y] 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)
def __init__(self, feature, classifier): PredictableModel.__init__(self, feature=feature, classifier=classifier) self.subject_names = []