def __init__(self,type,database,partition_percentage,num_components=None): if not num_components==None: if type==1: self.model = cv.createEigenFaceRecognizer(num_components) else: self.model = cv.createFisherFaceRecognizer(num_components) else: if type==1: self.model = cv.createEigenFaceRecognizer() else: self.model = cv.createFisherFaceRecognizer() self.manager = External_DB_Manager(partition_percentage) self.database = database
def __init__(self): self.node_name = "train_faces_fisher" rospy.init_node(self.node_name) rospy.on_shutdown(self.cleanup) self.bridge = CvBridge() self.size = 4 face_haar = 'haarcascade_frontalface_default.xml' self.haar_cascade = cv2.CascadeClassifier(face_haar) self.face_dir = 'face_data_fisher' self.face_name = sys.argv[1] self.path = os.path.join(self.face_dir, self.face_name) self.model = cv2.createFisherFaceRecognizer() # self.model = cv2.createEigenFaceRecognizer() self.cp_rate = 5 if not os.path.isdir(self.path): os.mkdir(self.path) self.count = 0 self.train_img_sub = rospy.Subscriber("/usb_cam/image_raw", Image, self.img_callback) # self.train_img_pub = rospy.Publisher('train_face', Image, queue_size=10) rospy.loginfo("Capturing data...")
def __init__(self): """ Create a Face Recognizer Class using Fisher Face Recognizer. Uses OpenCV's FaceRecognizer class. Currently supports Fisher Faces. """ self.supported = True self.model = None self.train_imgs = None self.train_labels = None self.csvfiles = [] self.imageSize = None self.labels_dict = {} self.labels_set = [] self.int_labels = [] self.labels_dict_rev = {} # Not yet supported # self.eigenValues = None # self.eigenVectors = None # self.mean = None try: import cv2 self.model = cv2.createFisherFaceRecognizer() except ImportError, AttributeError: self.supported = False warnings.warn("Fisher Recognizer is supported by OpenCV >= 2.4.4")
def __init__(self): self.node_name = "face_recog_fisher" rospy.init_node(self.node_name) rospy.on_shutdown(self.cleanup) self.bridge = CvBridge() self.face_names = StringArray() self.all_names = StringArray() self.size = 4 face_haar = 'haarcascade_frontalface_default.xml' self.haar_cascade = cv2.CascadeClassifier(face_haar) self.face_dir = 'face_data_fisher' self.model = cv2.createFisherFaceRecognizer() # self.model = cv2.createEigenFaceRecognizer() (self.im_width, self.im_height) = (112, 92) rospy.loginfo("Loading data...") # self.fisher_train_data() self.load_trained_data() rospy.sleep(3) # self.img_sub = rospy.Subscriber("/asus/rgb/image_raw", Image, self.img_callback) self.img_sub = rospy.Subscriber("/usb_cam/image_raw", Image, self.img_callback) # self.img_pub = rospy.Publisher('face_img', Image, queue_size=10) self.name_pub = rospy.Publisher('face_names', StringArray, queue_size=10) self.all_names_pub = rospy.Publisher('all_face_names', StringArray, queue_size=10) rospy.loginfo("Detecting faces...")
def __init__(self): self.node_name = "face_recog_fisher" rospy.init_node(self.node_name) rospy.on_shutdown(self.cleanup) self.bridge = CvBridge() self.face_names = StringArray() self.all_names = StringArray() self.size = 4 face_haar = "haarcascade_frontalface_default.xml" self.haar_cascade = cv2.CascadeClassifier(face_haar) self.face_dir = "face_data_fisher" self.model = cv2.createFisherFaceRecognizer() # self.model = cv2.createEigenFaceRecognizer() (self.im_width, self.im_height) = (112, 92) rospy.loginfo("Loading data...") # self.fisher_train_data() self.load_trained_data() rospy.sleep(3) # self.img_sub = rospy.Subscriber("/asus/rgb/image_raw", Image, self.img_callback) self.img_sub = rospy.Subscriber("/usb_cam/image_raw", Image, self.img_callback) # self.img_pub = rospy.Publisher('face_img', Image, queue_size=10) self.name_pub = rospy.Publisher("face_names", StringArray, queue_size=10) self.all_names_pub = rospy.Publisher("all_face_names", StringArray, queue_size=10) rospy.loginfo("Detecting faces...")
def __init__(self, recognition, modelType=None, threshold=None): """ Create a FaceRecognizer and train it on the given images """ self._recognition = recognition self.config = ConfigParser.ConfigParser() self.config.read("config.ini") if threshold is None: self.threshold = int(self.config.get("Alg Parameters", "threshold")) else: self.threshold = threshold if self._recognition: if modelType is None: modelType = self.config.get("Alg Parameters", "recognitionModel") if modelType == self.EIGEN_MODEL: self.model = cv2.createEigenFaceRecognizer() elif modelType == self.FISHER_MODEL: self.model = cv2.createFisherFaceRecognizer() elif modelType == self.LBPH_MODEL: self.model = cv2.createLBPHFaceRecognizer() print "Inizializing face recognizer model: " + modelType + "..." self.model, self.nameList = self.trainModel(self.model) self._matchCounter = Counter() # captured subjects or unknowns self._bestUsersMatch = Counter() self._totalMatches = 0 self._countMatchElem = 0 self.frameFaceCounter = 0
def train_fc(person, faces_db_set): def train_test_border(some_set, coeff): return int(math.ceil(len(some_set) * coeff)) data = [ (x.Mark.id if x.Mark else 0, x.FileName, loadImgToRec(x.FileName)) for x in faces_db_set ] person_id = person.id samples = map(lambda x: x[2], data) labels = map(lambda x: 1 if x[0] == person_id else 0, data) tol = 1 fc = None while tol > 0.05: pos_idx = map(lambda x:x[0], filter(lambda x: x[1] == 1, enumerate(labels))) neg_idx = map(lambda x:x[0], filter(lambda x: x[1] != 1, enumerate(labels))) random.shuffle(pos_idx) random.shuffle(neg_idx) neg_idx = neg_idx[:int((2 * len(pos_idx)) % len(neg_idx))] border_p = train_test_border(pos_idx, 0.2) border_n = train_test_border(neg_idx, 0.2) train_idx = pos_idx[border_p:] + neg_idx[border_n:] test_idx = pos_idx[:border_p] + neg_idx[:border_n] train_samples = [samples[i] for i in train_idx] train_labels = np.array([labels[i] for i in train_idx]) test_samples = [samples[i] for i in test_idx] test_labels = [labels[i] for i in test_idx] fc = cv2.createFisherFaceRecognizer() fc.train(train_samples, train_labels) tol = 0.0 print 'test' print [data[i][1] for i in test_idx]
def trainingModel(self): print("training") data = self.getData() print(data) self.model = cv.createFisherFaceRecognizer() self.model.train(array(data[0]), array(data[1])) print("Training over")
def __init__(self): #publish string so other nodes can subctibe to it self.image_pub = rospy.Publisher("face_recognition", String, queue_size=10) self.bridge = CvBridge() self.image_sub = rospy.Subscriber("/usb_cam/image_raw", Image, self.callback) #self.path = os.path.join('/home/generic/catkin_ws/src/image_test/src/datasets', '/home/generic/catkin_ws/src/image_test/src/lam') self.datasets = "/home/turtlebot/picture/" self.width = 130 self.height = 100 (self.images, self.lables, self.names, self.id) = ([], [], {}, 0) for (subdirs, dirs, files) in os.walk(self.datasets): for subdir in dirs: self.names[self.id] = subdir subjectpath = os.path.join(self.datasets, subdir) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename lable = self.id self.images.append(cv2.imread(path, 0)) self.lables.append(int(lable)) self.id += 1 (self.images, self.lables) = [ numpy.array(lis) for lis in [self.images, self.lables] ] self.model = cv2.createFisherFaceRecognizer() self.model.train(self.images, self.lables) self.faces = None self.gray = None self.seen = False self.NAME = None self.notrec = True
def predict(cv_image): faces = detect_faces(cv_image) result = None # print len(faces) if len(faces) > 0: cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100,100)) model = cv2.createFisherFaceRecognizer() #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) #print prediction result = { 'face': { 'name': model_names[prediction[0]], 'distance': prediction[1], 'coords': { 'x': str(faces[0][0]), 'y': str(faces[0][1]), 'width': str(faces[0][2]), 'height': str(faces[0][3]) } } } result=[prediction[0], prediction[1]] print "Found ", result return result
def predict(cv_image): faces = detect_faces(cv_image) result = None if len(faces) > 0: cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100, 100)) model = cv2.createFisherFaceRecognizer() #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) result = { 'face': { 'name': model_names[prediction[0]], 'distance': prediction[1], 'coords': { 'x': str(faces[0][0]), 'y': str(faces[0][1]), 'width': str(faces[0][2]), 'height': str(faces[0][3]) } } } #if (prediction[1]<1400): print "Found ", model_names[ prediction[0]], " with reliability metric", exp( -prediction[1] / 1400) * 100 return result
def Fisher_recognizer(model_dir): model_path = os.path.join(model_dir, 'Fisher.yml') face_recognizer = cv2.createFisherFaceRecognizer() print 'loading model in {0}'.format(model_path) face_recognizer.load(model_path) print 'load model finished.' return face_recognizer
def train_opencv(train_i, img_h, img_w, total_image_num, person_num, datacsv_file): lbphrecognizer = cv2.createLBPHFaceRecognizer() eigenrecognizer = cv2.createEigenFaceRecognizer() fisherrecognizer = cv2.createFisherFaceRecognizer() images, labels = get_images_and_labels(data_csvfile=datacsv_file, img_h=img_h, img_w=img_w, total_image_num=total_image_num) train_data = numpy.empty(((train_i * person_num, img_h, img_w)), dtype=int) train_label = numpy.empty(train_i * person_num, dtype=int) num_set = total_image_num / person_num for s in range(20): train_data[s * train_i:s * train_i + train_i] = images[s * num_set:s * num_set + train_i] train_label[s * train_i:s * train_i + train_i] = labels[s * num_set:s * num_set + train_i] lbphrecognizer.train(train_data, numpy.array(train_label)) lbphrecognizer.save("./LBPH_train_num_%s.xml" % (str(train_i))) fisherrecognizer.train(train_data, numpy.array(train_label)) fisherrecognizer.save("./fisher_train_num_%s.xml" % (str(train_i))) eigenrecognizer.train(train_data, numpy.array(train_label)) eigenrecognizer.save("./eigen_train_num_%s.xml" % (str(train_i)))
def predict(cv_image): faces = detect_faces(cv_image) result = None if len(faces) > 0: cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100,100)) model = cv2.createFisherFaceRecognizer() #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) #result = { # 'face': { # 'name': model_names[prediction[0]], # 'distance': prediction[1], # 'coords': { # 'x': str(faces[0][0]), # 'y': str(faces[0][1]), # 'width': str(faces[0][2]), # 'height': str(faces[0][3]) # } # } #} if (prediction[1]<1400): print "Found ", model_names[prediction[0]], " with reliability metric", exp(-prediction[1]/1400)*100 result = [prediction[0], prediction[1]] return result
def model(algorithm, thresh): # set the choosen algorithm model = None if not is_cv3(): # OpenCV version renamed the face module if algorithm == 1: model = cv2.face.createLBPHFaceRecognizer(threshold=thresh) elif algorithm == 2: model = cv2.face.createFisherFaceRecognizer(threshold=thresh) elif algorithm == 3: model = cv2.face.createEigenFaceRecognizer(threshold=thresh) else: print("WARNING: face algorithm must be in the range 1-3") os._exit(1) else: if algorithm == 1: model = cv2.createLBPHFaceRecognizer(threshold=thresh) elif algorithm == 2: model = cv2.createFisherFaceRecognizer(threshold=thresh) elif algorithm == 3: model = cv2.createEigenFaceRecognizer(threshold=thresh) else: print("WARNING: face algorithm must be in the range 1-3") os._exit(1) return model
def genTemplate(dir, file): def walk_files(directory, match="*"): for root, dirs, files in os.walk(directory): for filename in fnmatch.filter(files, match): yield os.path.join(root, filename) def prepare_image(filename): return cv2.resize(cv2.imread(filename, cv2.IMREAD_GRAYSCALE), (92, 112), interpolation=cv2.INTER_LANCZOS4) faces = [] labels = [] pos_count = 0 neg_count = 0 for filename in walk_files(dir + "/positive", "*.pgm"): faces.append(prepare_image(filename)) pos_count += 1 labels.append(0) for filename in walk_files(dir + "/negative", "*.pgm"): faces.append(prepare_image(filename)) neg_count += 1 labels.append(1) model = cv2.createFisherFaceRecognizer() model.train(faces, np.asarray(labels)) model.save(file)
def main(): parser = argparse.ArgumentParser() lfwDefault = os.path.expanduser( "~/openface/data/lfw/dlib.affine.sz:96.OuterEyesAndNose") parser.add_argument('--lfwAligned', type=str, default=lfwDefault, help='Location of aligned LFW images') parser.add_argument('--networkModel', type=str, help="Path to Torch network model.", default=os.path.join(openfaceModelDir, 'nn4.small2.v1.t7')) parser.add_argument('--largeFont', action='store_true') parser.add_argument('workDir', type=str, help='The work directory where intermediate files and results are kept.') args = parser.parse_args() # print(args) if args.largeFont: font = {'family': 'normal', 'size': 20} mpl.rc('font', **font) mkdirP(args.workDir) print("Getting lfwPpl") lfwPplCache = os.path.join(args.workDir, 'lfwPpl.pkl') lfwPpl = cacheToFile(lfwPplCache)(getLfwPplSorted)(args.lfwAligned) print("Eigenfaces Experiment") cls = cv2.createEigenFaceRecognizer() cache = os.path.join(args.workDir, 'eigenFacesExp.pkl') eigenFacesDf = cacheToFile(cache)(opencvExp)(lfwPpl, cls) print("Fisherfaces Experiment") cls = cv2.createFisherFaceRecognizer() cache = os.path.join(args.workDir, 'fisherFacesExp.pkl') fishFacesDf = cacheToFile(cache)(opencvExp)(lfwPpl, cls) print("LBPH Experiment") cls = cv2.createLBPHFaceRecognizer() cache = os.path.join(args.workDir, 'lbphExp.pkl') lbphFacesDf = cacheToFile(cache)(opencvExp)(lfwPpl, cls) print("OpenFace CPU/SVM Experiment") net = openface.TorchNeuralNet(args.networkModel, 96, cuda=False) cls = SVC(kernel='linear', C=1) cache = os.path.join(args.workDir, 'openface.cpu.svm.pkl') openfaceCPUsvmDf = cacheToFile(cache)(openfaceExp)(lfwPpl, net, cls) print("OpenFace GPU/SVM Experiment") net = openface.TorchNeuralNet(args.networkModel, 96, cuda=True) cache = os.path.join(args.workDir, 'openface.gpu.svm.pkl') openfaceGPUsvmDf = cacheToFile(cache)(openfaceExp)(lfwPpl, net, cls) plotAccuracy(args.workDir, args.largeFont, eigenFacesDf, fishFacesDf, lbphFacesDf, openfaceCPUsvmDf, openfaceGPUsvmDf) plotTrainingTime(args.workDir, argrs.largeFont, eigenFacesDf, fishFacesDf, lbphFacesDf, openfaceCPUsvmDf, openfaceGPUsvmDf) plotPredictionTime(args.workDir, args.largeFont, eigenFacesDf, fishFacesDf, lbphFacesDf, openfaceCPUsvmDf, openfaceGPUsvmDf)
def refresh_data(): # Get path to all images path = '../rsc/images' image_paths = [os.path.join(path, f) for f in os.listdir(path)] # Initialize empty lists for these images and labels images = [] labels = [] for path in image_paths: images.append(cv2.imread(path, cv2.CV_LOAD_IMAGE_GRAYSCALE)) labels.append(int(os.path.split(path)[1].split("_")[0])) labels = array(labels) # Create recognizers eigen_recognizer = cv2.createEigenFaceRecognizer() fisher_recognizer = cv2.createFisherFaceRecognizer() lbhp_recognizer = cv2.createLBPHFaceRecognizer() # Train recognizers eigen_recognizer.train(images, labels) fisher_recognizer.train(images, labels) lbhp_recognizer.train(images, labels) # Save results eigen_recognizer.save(EIGEN_RECOGNIZER_PATH) fisher_recognizer.save(FISHER_RECOGNIZER_PATH) lbhp_recognizer.save(LBHP_RECOGNIZER_PATH)
def __init__(self): """ Create a Face Recognizer Class using Fisher Face Recognizer. Uses OpenCV's FaceRecognizer class. Currently supports Fisher Faces. """ self.supported = True self.model = None self.train_imgs = None self.train_labels = None self.csvfiles = [] self.image_size = None self.labels_dict = {} self.labels_set = [] self.int_labels = [] self.labels_dict_rev = {} self.trained = False # Not yet supported # self.eigenValues = None # self.eigenVectors = None # self.mean = None try: self.model = cv2.createFisherFaceRecognizer() except AttributeError: self.supported = False warnings.warn("Fisher Recognizer is supported by OpenCV >= 2.4.4")
def predict(cv_image): faces = detect_faces(cv_image) result = None # print len(faces) if len(faces) > 0: cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100, 100)) model = cv2.createFisherFaceRecognizer() #model = cv2.createEigenFaceRecognizer() model.load(MODEL_FILE) prediction = model.predict(resized) #print prediction result = { 'face': { 'name': model_names[prediction[0]], 'distance': prediction[1], 'coords': { 'x': str(faces[0][0]), 'y': str(faces[0][1]), 'width': str(faces[0][2]), 'height': str(faces[0][3]) } } } result = [prediction[0], prediction[1]] print "Found ", result return result
def learnCollectedFaces(preprocessedFaces, faceLabels, facerecAlgorithm, recognizer): print "Learning the collected faces using the {0} algorithm...".format(facerecAlgorithm) # Make sure the "contrib" module is dynamically loaded at runtime # Requires OpenCV v2.4.1 or later (from June 2012), otherwise the FaceRecognizer will not compile or run # haveContribModule = # if not haveContribModule: # print "contrib module is needed for facerecognizer" # sys.exit() # create recognizer depending on the defined algorithm faceLabels = np.array(faceLabels) if facerecAlgorithm == 'Fisherfaces': recognizer = cv2.createFisherFaceRecognizer() elif facerecAlgorithm == 'Eigenfaces': recognizer = cv2.createEigenFaceRecognizer() else: recognizer = cv2.createLBPHFaceRecognizer() # faceLabels = np.array(faceLabels) print np.unique(faceLabels) recognizer.train(preprocessedFaces, faceLabels) return recognizer
def __init__(self, type, database, partition_percentage, num_components=None): if not num_components == None: if type == 1: self.model = cv.createEigenFaceRecognizer(num_components) else: self.model = cv.createFisherFaceRecognizer(num_components) else: if type == 1: self.model = cv.createEigenFaceRecognizer() else: self.model = cv.createFisherFaceRecognizer() self.manager = External_DB_Manager(partition_percentage) self.database = database
def train_model(images, labels): try: model = cv2.createFisherFaceRecognizer() model.train(images, labels) model.save(MODEL_FILE) except: print 'Unexpected error:', sys.exc_info()[0] raise
def initFaceRec(): global person_dict, gender_dict, personmodel, sexmodel, face_cascade #face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml') face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml') #sexmodel: sexmodel = cv2.createFisherFaceRecognizer() #[imgs,labels] = csv.readCsv("/home/pi/mirror/bilder/gender.csv") [imgs,person_labels,gender_labels, person_dict, gender_dict] = lab.getLabels() labels = np.asarray(gender_labels, dtype=np.int32) sexmodel.train(np.asarray(imgs), np.asarray(labels)) #personmodel: personmodel = cv2.createFisherFaceRecognizer(threshold=300.0) #personmodel = cv2.createEigenFaceRecognizer(threshold=4500.0) #personmodel = cv2.createLBPHFaceRecognizer(threshold=100.0) #[imgs,labels] = csv.readCsv("/home/pi/mirror/bilder/person.csv") labels = np.asarray(person_labels, dtype=np.int32) personmodel.train(np.asarray(imgs), np.asarray(labels))
def initFaceRec(): global person_dict, gender_dict, personmodel, sexmodel, face_cascade # face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml') face_cascade = cv2.CascadeClassifier("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml") # sexmodel: sexmodel = cv2.createFisherFaceRecognizer() # [imgs,labels] = csv.readCsv("/home/pi/mirror/bilder/gender.csv") [imgs, person_labels, gender_labels, person_dict, gender_dict] = lab.getLabels() labels = np.asarray(gender_labels, dtype=np.int32) sexmodel.train(np.asarray(imgs), np.asarray(labels)) # personmodel: personmodel = cv2.createFisherFaceRecognizer(threshold=300.0) # personmodel = cv2.createEigenFaceRecognizer(threshold=4500.0) # personmodel = cv2.createLBPHFaceRecognizer(threshold=100.0) # [imgs,labels] = csv.readCsv("/home/pi/mirror/bilder/person.csv") labels = np.asarray(person_labels, dtype=np.int32) personmodel.train(np.asarray(imgs), np.asarray(labels))
def useopencv(testcsv_file, train_i): csv_file = open(testcsv_file) data = csv.reader(csv_file) count_incorrect_fisher = 0.0 count_incorrect_eigen = 0.0 count_incorrect_lbph = 0.0 total_num_face = 0 fisherrecognizer = cv2.createFisherFaceRecognizer() lbphrecognizer = cv2.createLBPHFaceRecognizer() eigenrecognizer = cv2.createEigenFaceRecognizer() eigenrecognizer.load("./eigen_train_num_%s.xml" % (str(train_i))) lbphrecognizer.load("./LBPH_train_num_%s.xml" % (str(train_i))) fisherrecognizer.load("./fisher_train_num_%s.xml" % (str(train_i))) for csvdata2, csvlabel2 in data: # print csvdata # print csvlabel total_num_face = total_num_face + 1 f = csvdata2 l = int(csvlabel2) predict_image_pil = Image.open(f).convert('L') predict_image = numpy.array(predict_image_pil, 'uint8') nbr_predicted_fisher, conf2_fisher = fisherrecognizer.predict( predict_image[:]) nbr_predicted_eigen, conf2_eigen = eigenrecognizer.predict( predict_image[:]) nbr_predicted_lbph, conf2_lbph = lbphrecognizer.predict( predict_image[:]) nbr_actual = l if nbr_actual != nbr_predicted_fisher: #print "{} is Correctly Recognized with confidence {}".format(nbr_actual, conf) #else: #print "fisherrecognizer {}is Incorrect Recognized as {}".format(nbr_actual2, nbr_predicted2) count_incorrect_fisher = count_incorrect_fisher + 1.0 if nbr_actual != nbr_predicted_eigen: count_incorrect_eigen = count_incorrect_eigen + 1.0 if nbr_actual != nbr_predicted_lbph: count_incorrect_lbph = count_incorrect_lbph + 1.0 percentage_fisher = 100 - (count_incorrect_fisher / total_num_face) * 100 percentage_lbph = 100 - (count_incorrect_lbph / total_num_face) * 100 percentage_eigen = 100 - (count_incorrect_eigen / total_num_face) * 100 print "correct rate {} % in fisherrecognizer with {} trainning face".format( percentage_fisher, train_i) print "correct rate {} % in lbphrecognizer with {} trainning face".format( percentage_lbph, train_i) print "correct rate {} % in eigenrecognizer with {} trainning face".format( percentage_eigen, train_i)
def recognize_person(fname, persons): fcs = [] res = [] for person in persons: fc = cv2.createFisherFaceRecognizer() fc.load(person.ClassifierName) fcs.append(fc) res.append((person, ) + fc.predict(loadImgToRec(fname))) return res
def createRecognizer(templatefile): model = cv2.createFisherFaceRecognizer() model.load(templatefile) def recognizer(img): label, confidence = model.predict(img) return confidence, label return recognizer
def load(self, db_file_name): """ Update the face models data structure from a file. :type db_file_name: string :param db_file_name: the name of the file containing the dump of the face models data structure :rtype: boolean :returns: True if loading was successful """ if db_file_name is None: ''' Set the name of database. Algorithm : LBP (Local Binary Pattern) ''' db_file_name = self._db_name + '-' + self._algorithm tags_file_name = db_file_name + '-Tags' algorithm = ce.FACE_MODEL_ALGORITHM if self._params is not None: algorithm = self._params[ce.FACE_MODEL_ALGORITHM_KEY] model = None if algorithm == 'Eigenfaces': model = cv2.createEigenFaceRecognizer() elif algorithm == 'Fisherfaces': model = cv2.createFisherFaceRecognizer() elif algorithm == 'LBP': model = cv2.createLBPHFaceRecognizer() ok = False if os.path.isfile(db_file_name) and (os.path.isfile(tags_file_name)): if(not((ce.USE_TRACKING or ce.SIM_TRACKING or ce.USE_SLIDING_WINDOW) and ce.LOAD_IND_FRAMES_RESULTS)): model.load(db_file_name) if not(model is None): self.model = model self._tags = utils.load_YAML_file(tags_file_name) ok = True print('\n### DB LOADED ###\n') return ok
class recognise(): size = 4 haar_file = 'coin_cascade.xml' datasets = 'Datasets' # Part 1: Create fisherRecognizer print('Training...') # Create a list of images and a list of corresponding names (images, labels, names, id) = ([], [], {}, 0) for (subdirs, dirs, files) in os.walk(datasets): for subdir in dirs: names[id] = subdir subjectpath = os.path.join(datasets, subdir) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename label = id images.append(cv2.imread(path, 0)) labels.append(int(label)) id += 1 (width, height) = (30, 30) # Create a Numpy array from the two lists above (images, labels) = [numpy.array(lis) for lis in [images, labels]] model = cv2.createFisherFaceRecognizer() model.train(images, labels) # Part 2: Use fisherRecognizer on camera stream face_cascade = cv2.CascadeClassifier(haar_file) webcam = cv2.VideoCapture(0) f = 0 while True: (_, im) = webcam.read() gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2) face = gray[y:y + h, x:x + w] face_resize = cv2.resize(face, (width, height)) #Try to recognize the face prediction = model.predict(face_resize) cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3) if prediction < 500: cv2.putText(im, '%s - %.0f' % (names[prediction], prediction), (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0)) else: cv2.putText(im, 'not recognized', (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0)) cv2.imshow('OpenCV', im) key = cv2.waitKey(10) if key == 27: break print "Verified"
def load_model(self,mode,source,num_components=None): """Loads a stored face recognition model. If it doesn't exists, returns None""" name = "eigenfaces_%s.yaml"%(source) model = None if mode==1 and num_components==None: model = cv.createEigenFaceRecognizer() elif mode==1: model = cv.createFisherFaceRecognizer(num_components) return model if mode==2 and num_components==None: model = cv.createFisherFaceRecognizer() elif mode==2: model = cv.createFisherFaceRecognizer(num_components) return model try: model.load(self.rec_path+name) return model except: return None
def load_model(self, mode, source, num_components=None): """Loads a stored face recognition model. If it doesn't exists, returns None""" name = "eigenfaces_%s.yaml" % (source) model = None if mode == 1 and num_components == None: model = cv.createEigenFaceRecognizer() elif mode == 1: model = cv.createFisherFaceRecognizer(num_components) return model if mode == 2 and num_components == None: model = cv.createFisherFaceRecognizer() elif mode == 2: model = cv.createFisherFaceRecognizer(num_components) return model try: model.load(self.rec_path + name) return model except: return None
def __init__(self, algorithm='fisherface'): self.algorithm = algorithm if (algorithm == 'fisherface'): self.model = cv2.createFisherFaceRecognizer() elif (algorithm == 'eigenface'): self.model = cv2.createEigenFaceRecognizer() elif (algorithm == 'lbphf'): self.model = cv2.createLBPHFaceRecognizer() else: raise ValueError('Unknown algorithm: "%s"!' % (algorithm,))
def prepareFacialModel(): faceRecModel = cv2.createFisherFaceRecognizer() faceRecModelFile = Path("./face-model.xml") if (faceRecModelFile.is_file()): faceRecModel.load("./face-model.xml") else: faceRecModel = trainFaceRec() return faceRecModel
def __init__(self): cascPath = "haarcascade_frontalface_default.xml" self.face_cascade = cv2.CascadeClassifier(cascPath) self.face_dir = 'face_data' self.face_name = sys.argv[1] self.path = os.path.join(self.face_dir, self.face_name) if not os.path.isdir(self.path): os.mkdir(self.path) self.model = cv2.createFisherFaceRecognizer() self.count_captures = 0 self.count_timer = 0
def main(): arg = ([], []) os.path.walk(sys.argv[1], output, arg) print "Training Fisher" r = cv2.createFisherFaceRecognizer() r.train(arg[0], numpy.array(arg[1])) r.save('ricbit.fisher.xml') print "Training Eigen" s = cv2.createEigenFaceRecognizer(num_components=9) s.train(arg[0], numpy.array(arg[1])) s.save('ricbit.eigen.xml')
def predict_emotion(cropped_face): fishface = cv2.createFisherFaceRecognizer() try: fishface.load(CLASSIFIER_DATA_FILE) except: print("classifier data file not found") prediction, confidence = fishface.predict(cropped_face) print("predicted emotion: " + emotions[prediction], " (confidence: " + str(confidence) + ")") return emotions[prediction]
def create_and_train_model_from_dict(label_matrix, type): # Create and train eigenface model if type == 'lbph': model = cv2.createLBPHFaceRecognizer() elif type == 'eigen': model = cv2.createEigenFaceRecognizer() elif type == 'fisher': model = cv2.createFisherFaceRecognizer() images = label_matrix.values() labels = numpy.array(label_matrix.keys()) model.train(images, labels) return model
def Fisher(data_dir, out_dir): face_recognizer = cv2.createFisherFaceRecognizer() print 'start reading images in {0}'.format(data_dir) images, labels, names = read_images(data_dir, resize=True) save_names(names, out_dir) print 'start training face recognizer' face_recognizer.train(images, labels) model_path = os.path.join(out_dir, 'Fisher.yml')# LBPH.yml, Eigen.yml, Fisher.yml print 'save trained model to {0}'.format(model_path) face_recognizer.save(model_path)
def _recognizer(self): recognizer = None if self.model_name == 'Eigen': recognizer = cv2.createEigenFaceRecognizer() elif self.model_name == 'Fisher': #self.size = 300,300 recognizer = cv2.createFisherFaceRecognizer() elif self.model_name == 'LBPH': recognizer = cv2.createLBPHFaceRecognizer(threshold=200) else: raise Exception('Unknown face recognition model "%s"' % (self.model_name)) return recognizer
def __init__(self, threadID, name, counter): ''' this launches a thread so we're not blocking the server code ''' threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter print "setup Abe" self.cam = Cam(0) self.img, self.label = [], [] self.id = 0 self.model = cv2.createFisherFaceRecognizer()
def __init__(self, face_type): self._type = ['eigen', 'fisher', 'lbph'] if face_type not in self._type: raise ValueError("you must be chose in ['eigen', 'fisher', 'lbph']") if face_type == 'eigen': self._model = cv2.createEigenFaceRecognizer(threshold=1000) if face_type == 'fisher': self._model = cv2.createFisherFaceRecognizer(threshold=1000) if face_type == 'lbph': self._model = cv2.createLBPHFaceRecognizer(threshold=100)
def load(self): threshold = self.get_threshold(self.algorithm) if self.algorithm == 1: self.model = cv2.createLBPHFaceRecognizer(threshold=threshold) self.training_file = "training_lbp.xml" elif self.algorithm == 2: self.model = cv2.createFisherFaceRecognizer(threshold=threshold) self.training_file = "training_fisher.xml" else: self.model = cv2.createEigenFaceRecognizer(threshold=threshold) self.training_file = "training_eigen.xml" self.model.load( os.path.join(self.training_file_dir, self.training_file))
def get_recognizer(): key = "global_recognizer_key" if key in global_recognizer: return global_recognizer[key] recognizer = cv2.createFisherFaceRecognizer() try: recognizer.load(RECOGNIZER_FILENAME) except cv2.error: print "Starting training...", recognizer = train_recognizer(recognizer) print "finished" global_recognizer[key] = recognizer recognizer.save(RECOGNIZER_FILENAME) return recognizer
def set_classifier(self, classifier="eigenfaces"): ''' Set classifier for face recognition. ''' if classifier == "eigenfaces": self._logger.log(Logger.INFO, "Creating eigenface recognizer.") self.recognizer = cv2.createEigenFaceRecognizer(80) elif classifier == "fisherfaces": self._logger.log(Logger.INFO, "Creating fisher face recognizer.") self.recognizer = cv2.createFisherFaceRecognizer(self.total_labels) else: self._logger.log(Logger.INFO, "Creating LBPH face recognizer.") self.recognizer = cv2.createLBPHFaceRecognizer() self.classifier = classifier
def gender(par): fisherrecognizer = cv2.createFisherFaceRecognizer() path_g_check = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir)) + "%s" % (str(par)) input_pic = Image.open(path_g_check) detected_face = detect(input_pic) if (detected_face != None) and detected_face != 0: repic_img = numpy.array(detected_face, 'uint8') xmlpath = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir)) + '/genderclass.xml' fisherrecognizer.load(xmlpath) predicted_fisher, conf2_fisher = fisherrecognizer.predict(repic_img[:]) return predicted_fisher
def __init__(self, faces, threshold=2000000000): self.size = 1 (images, images2, self.labels, self.names, id) = ([], [], [], {}, 0) self.faces = faces # Load user face models fn_dir = os.path.join(settings.PROJECT_ROOT,"run","models") for face in self.faces: if face.active: self.names[id] = face.name subjectpath = os.path.join(fn_dir, face.name) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename label = id #images.append(cv2.imread(path, 0)) images.append(self.tantriggs(cv2.imread(path, 0))) self.labels.append(int(label)) id += 1 # Load default models fn_dir = os.path.join(settings.PROJECT_ROOT,"run","att_models") for (subdirs, dirs, files) in os.walk(fn_dir): for subdir in dirs: self.names[id] = '' subjectpath = os.path.join(fn_dir, subdir) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename label = id #images.append(cv2.imread(path, 0)) images.append(self.tantriggs(cv2.imread(path, 0))) self.labels.append(int(label)) id += 1 # Prepare recognizer (self.im_width, self.im_height) = (112, 92) (self.images, self.labels) = [np.array(lis) for lis in [images, self.labels]] self.model = cv2.createFisherFaceRecognizer() self.model.setDouble("threshold",threshold) self.model2 = cv2.createLBPHFaceRecognizer() self.model2.setDouble("threshold",threshold) if id > 1: self.model.train(self.images, self.labels) self.model2.train(self.images, self.labels) # Prepare face cascade self.faceCascade = cv2.CascadeClassifier(os.path.join(os.path.dirname(os.path.realpath(__file__)),"haarcascade_frontalface_default.xml")) self.framenr = 0
def load_model(self, db_file_name): """ Update the face models data structure for a single person from a file. :type db_file_name: string :param db_file_name: the name of the file containing the dump of the face models data structure :rtype: boolean :returns: True if loading was successful """ ok = False if db_file_name is None: print "No db file was provided" else: algorithm = ce.FACE_MODEL_ALGORITHM if self._params is not None: algorithm = self._params[ce.FACE_MODEL_ALGORITHM_KEY] model = None if algorithm == 'Eigenfaces': model = cv2.createEigenFaceRecognizer() elif algorithm == 'Fisherfaces': model = cv2.createFisherFaceRecognizer() elif algorithm == 'LBP': model = cv2.createLBPHFaceRecognizer() if os.path.isfile(db_file_name): model.load(db_file_name) if not(model is None): self.model = model ok = True return ok
def __init__(self, config): #self.emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] self._config = config self.emotions = [] for emotion, enabled in self._config["emotions"].items(): if enabled: self.emotions.append(emotion) print(self.emotions) self.faceDet = cv2.CascadeClassifier("%s/haarcascade_frontalface_default.xml" % self._config.cascades) self.faceDet2 = cv2.CascadeClassifier("%s/haarcascade_frontalface_alt2.xml" % self._config.cascades) self.faceDet3 = cv2.CascadeClassifier("%s/haarcascade_frontalface_alt.xml" % self._config.cascades) self.faceDet4 = cv2.CascadeClassifier("%s/haarcascade_frontalface_alt_tree.xml" % self._config.cascades) self.fishface = cv2.createFisherFaceRecognizer() self.data = {}
def __init__(self): print "Recognizer instantiated !" self.lbphModel = cv2.createLBPHFaceRecognizer() self.eigenModel = cv2.createEigenFaceRecognizer() self.fisherModel = cv2.createFisherFaceRecognizer() self.model_path = "models" self.lbph_model_path = os.path.join(self.model_path, "lbphModel.xml") self.eigen_model_path = os.path.join(self.model_path, "eigenModel.xml") self.fisher_model_path = os.path.join(self.model_path, "fisherModel.xml") self.load_models() self.new_faces, self.new_labels = [], [] self.db = db.Database()
def __init__(self, user_interface): # Create and load the classifier, capture the images self.train_csv = "face_data.csv" # sys.argv[1] self.cascade = "haar_cascade/haarcascade_frontalface_default.xml" self.name_csv = "name_data.csv" # sys.argv[2] self.deviceID = 0 # sys.argv[3] self.build_imagecsv() self.images, self.labels = self.read_imagecsv() self.size = self.images[0].shape self.names = self.read_namecsv() #print self.names[43] self.faceCascade = cv2.CascadeClassifier(self.cascade) self.recognizer = cv2.createFisherFaceRecognizer(threshold=800) self.recognizer.train(self.images, self.labels) self.open() self.maxlabel = max(self.labels) self.user_interface = user_interface
def __init__(self, dir): # three algorithms for face comparison self.eigen = cv2.createEigenFaceRecognizer(num_components=80) self.fisher = cv2.createFisherFaceRecognizer() self.lbph = cv2.createLBPHFaceRecognizer() # Viola-Jones algorithm for face-detection w/in training and test images cascadePath = "../face_detection/haarcascade_frontalface_alt2.xml" self.face_cascade = cv2.CascadeClassifier(cascadePath) self.int_names = {} # models need numeric labels for training; correlates labels to actual names self.avg_width = 0 # for image resizing self.avg_height = 0 self.thresholds = [7000, 5000, 250]#[8000,5000,240]#[6000, 5300, 220] # train the models self.train(dir)
def __init__(self, mode=0, csv_url="faces.csv"): self.faces, self.names, self.samples = read_faces(csv_url) self.face_y, self.face_x = self.faces[0].shape self.face_size = self.faces[0].shape print self.faces[0].shape if mode == 0: self.model = cv2.createEigenFaceRecognizer(num_components=80) elif mode == 1: self.model = cv2.createLBPHFaceRecognizer( neighbors=8, grid_x=8, grid_y=8) elif mode == 2: self.model = cv2.createFisherFaceRecognizer(num_components=80) else: print mode self.model.train(self.faces, np.array(range(len(self.names))))
def trainGenderModel(): maleDataSet = '/Users/Wester/Pictures/tmp/Gender-Data/Male-Train/' femaleDataSet = '/Users/Wester/Pictures/tmp/Gender-Data/Female-Train/' fisherFaceRecog = cv2.createFisherFaceRecognizer() allImgs, allSubjects = [], [] [mImgs, mSubjects] = readDataSet(maleDataSet, 1) allImgs.extend(mImgs) allSubjects.extend(mSubjects) [fImgs, fSubjects] = readDataSet(femaleDataSet, 2) allImgs.extend(fImgs) allSubjects.extend(fSubjects) allSubjects = numpy.asarray(allSubjects, dtype=numpy.int32) fisherFaceRecog.train(numpy.asarray(allImgs), numpy.asarray(allSubjects)) return fisherFaceRecog
def predict(cv_image): faces = detect_faces(cv_image) result = None if len(faces) > 0: print 'found a face attempting to predict' cropped = to_grayscale(crop_faces(cv_image, faces)) resized = cv2.resize(cropped, (100,100)) model = cv2.createFisherFaceRecognizer() model.load("model.mdl") prediction = model.predict(resized) result = { 'face': Label.get(Label.id == prediction[0]).name, 'distance': prediction[1] } return result
def process(self): self.logger.info("Training face recognizer...") users = os.listdir("users") self.identifiers = {} images = {} user_idx = 0 for user in users: for room in os.listdir("users/" + user): self.logger.info("Processing room %s user %s", room, user) #identifiers[room] = recognizer for image in self._get_images(user, room): if not room in images: images[room] = [] images[room].append((image, user_idx)) self.labels[user_idx] = user user_idx += 1 for room in images: self.logger.info("Training for room %s", room) #recognizer = cv2.createLBPHFaceRecognizer() recognizer = cv2.createFisherFaceRecognizer() room_images = map(lambda x: x[0], images[room]) largest_image = max(room_images, key=lambda x: x.size) # Resize all of the images to the size of the largest library image resized_room_images = map( lambda x: cv2.resize(x, largest_image.shape[:2]), room_images) self.logger.info("Images: %d", len(resized_room_images)) # Train the face recognizer and provide the labels recognizer.train(resized_room_images, numpy.array(map(lambda x: x[1], images[room]))) self.dimensions[room] = largest_image.shape[:2] self.recognizers[room] = recognizer self.logger.info("Done") self.schedule_timer() return 0