def create_model(model_type, output, stream_string, dim, streams, image_stream_type, dist=None, save=False): """ Create a new instance of model, with classifier and feature pre-processing Args: model_type: enum value of SVM, CNN, HMM output: output file location stream_string: friendly string of image types dim: size of images streams: number of image streams image_stream_type: image type flags dist: distance to filter for (int or None) save: save output Returns: model object composed of classifier and feature extraction """ if model_type == ModelType.SVM: classifier = SupportVectorMachine(output, stream_string, dim, dist=dist) mu = MeanNormalisation(streams=streams, flattened=classifier.flattened) pca = PCA(num_components=150) feature = FeatureChain(mu, pca) elif model_type == ModelType.HMM: l = 16 # Height of block for feature extraction p = 12 # Amount of overlap between blocks k_size = 25 # DCT kernel size diag = True classifier = HiddenMarkovModel(output, stream_string, dim, dist=dist, diag=diag) feature = FeatureChain( BlockExtractor(dim, streams, classifier.flattened, l=l, p=p), FeatureMap(DiscreteCosine(k_size, (l, dim[1]), streams), k_size, streams)) else: dim = list(dim) + [streams] classifier = ConvolutionalNeuralNetwork(output, stream_string, shape=dim, dist=dist) feature = MinMaxNormalisation(streams=streams, flattened=False) model = Model(feature, classifier, image_stream_type, save_data=save) return model
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 = [], []
class Feature(Enum): pca = PCA() spacial = SpatialHistogram() identity = Identity() fisherfaces = Fisherfaces()
print("USAGE: facerec_demo.py </path/to/images>") sys.exit() yale_filter = YaleBaseFilter(-25, 25, -25, 25) # Now read in the image data. This must be a valid path! [X, y] = read_images(sys.argv[1], yale_filter) # 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 = PCA() # 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 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,
test_faces = [] for image, id in test_list: test_faces.append((id, utils.read_image(os.path.join(test_path, image)))) # 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) # ætla að prófa allar aðferðir # feature = Fisherfaces() m = (Fisherfaces(), PCA(), SpatialHistogram(), SpatialHistogram(LPQ())) classifiers = ( # Define a 1-NN classifier with Euclidean Distance: NearestNeighbor(dist_metric=EuclideanDistance(), k=3), NearestNeighbor(dist_metric=CosineDistance(), k=3), NearestNeighbor(dist_metric=NormalizedCorrelation(), k=3), NearestNeighbor(dist_metric=ChiSquareDistance(), k=3), NearestNeighbor(dist_metric=HistogramIntersection(), k=3), NearestNeighbor(dist_metric=L1BinRatioDistance(), k=3), NearestNeighbor(dist_metric=ChiSquareBRD(), k=3), ) def test_one(idx): tt, pt, res_list = test_one_method(input_faces, test_faces, m[idx], classifiers[idx], True) print tt, ",", pt
def model_rebuild(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 os.path.isfile(model_fn): os.remove(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)
if len(sys.argv) < 2: 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)
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"
"fisher": Fisherfaces, "fisher10": Fisherfaces, "pca": PCA, "pca10": PCA, "lda": LDA, "spatial": SpatialHistogram, "LPQ": SpatialHistogram } if feature_parameter in m: if feature_parameter == 'LPQ': feature = SpatialHistogram(LPQ()) elif feature_parameter == 'fisher80': feature = Fisherfaces(80) elif feature_parameter == 'pca80': feature = PCA(80) else: feature = m[feature_parameter]() # 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)