Example #1
0
def main(args):
  
    with tf.Graph().as_default():

        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
            
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")

            tpr, fpr, accuracy, val, val_std, far = lfw.validate(sess, 
                paths, actual_issame, args.seed, 60, 
                images_placeholder, phase_train_placeholder, embeddings, nrof_folds=args.lfw_nrof_folds)
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
Example #2
0
def main(args):
    align = align_dlib.AlignDlib(os.path.expanduser(args.dlib_face_predictor))
    image_paths = [args.image1, args.image2]
    landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE

    with tf.Graph().as_default():

        with tf.Session() as sess:
      
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
    
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            image_size = int(images_placeholder.get_shape()[1])

            # Run forward pass to calculate embeddings
            images = load_and_align_data(image_paths, image_size, align, landmarkIndices)
            feed_dict = { images_placeholder: images, phase_train_placeholder: False }
            emb = sess.run(embeddings, feed_dict=feed_dict)
            dist = np.sqrt(np.mean(np.square(np.subtract(emb[0,:], emb[1,:]))))
            print('Distance between the embeddings: %3.6f' % dist)
Example #3
0
def main(args):
  
    images, cout_per_image, nrof_samples = load_and_align_data(args.image_files,args.image_size, args.margin, args.gpu_memory_fraction)
    with tf.Graph().as_default():

       with tf.Session() as sess:
      
            # Load the model
                facenet.load_model(args.model)
            # Get input and output tensors
                images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
                phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
                feed_dict = { images_placeholder: images , phase_train_placeholder:False}
                emb = sess.run(embeddings, feed_dict=feed_dict)
                classifier_filename_exp = os.path.expanduser(args.classifier_filename)
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)
                print('Loaded classifier model from file "%s"\n' % classifier_filename_exp)
                predictions = model.predict_proba(emb)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                k=0     
	    #print predictions       
                for i in range(nrof_samples):
                    print("\npeople in image %s :" %(args.image_files[i]))
                    for j in range(cout_per_image[i]):
                        print('%s: %.3f' % (class_names[best_class_indices[k]], best_class_probabilities[k]))
                        k+=1
Example #4
0
def loadArg():
	path1 = './shape_predictor_68_face_landmarks.dat'
	path2 = './20160514-234418/model.ckpt-500000'
	align = align_dlib.AlignDlib(os.path.expanduser(path1))
	landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE
	# Load the model
	print("load model")
	facenet.load_model(path2)
	return align,landmarkIndices
Example #5
0
def main(args):
    train_set = facenet.get_dataset(args.data_dir)
    image_list, label_list = facenet.get_image_paths_and_labels(train_set)
    label_strings = [name for name in os.listdir(os.path.expanduser(args.data_dir)) if os.path.isdir(os.path.join(os.path.expanduser(args.data_dir), name))]

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Load the model
            facenet.load_model(args.model_dir)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
            nrof_images = len(image_list)
            print('Number of images: ', nrof_images)
            batch_size = args.image_batch
            if nrof_images % batch_size == 0:
                nrof_batches = nrof_images // batch_size
            else:
                nrof_batches = (nrof_images // batch_size) + 1
            print('Number of batches: ', nrof_batches)
            embedding_size = embeddings.get_shape()[1]
            emb_array = np.zeros((nrof_images, embedding_size))
            start_time = time.time()

            for i in range(nrof_batches):
                if i == nrof_batches -1:
                    n = nrof_images
                else:
                    n = i*batch_size + batch_size
                # Get images for the batch
                if args.is_aligned is True:
                    images = facenet.load_data(image_list[i*batch_size:n], False, False, args.image_size)
                else:
                    images = load_and_align_data(image_list[i*batch_size:n], args.image_size, args.margin, args.gpu_memory_fraction)
                feed_dict = { images_placeholder: images, phase_train_placeholder:False }
                # Use the facenet model to calcualte embeddings
                embed = sess.run(embeddings, feed_dict=feed_dict)
                emb_array[i*batch_size:n, :] = embed
                print('Completed batch', i+1, 'of', nrof_batches)

            run_time = time.time() - start_time
            print('Run time: ', run_time)

            #   export emedings and labels
            label_list  = np.array(label_list)

            np.save(args.embeddings_name, emb_array)
            np.save(args.labels_name, label_list)
            np.save(args.labels_strings_name, label_strings)
Example #6
0
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            
            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]
        
            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0*nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches):
                start_index = i*batch_size
                end_index = min((i+1)*batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False, image_size)
                feed_dict = { images_placeholder:images, phase_train_placeholder:False }
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
        
            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array, 
                actual_issame, nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))

            auc = metrics.auc(fpr, tpr)
            print('Area Under Curve (AUC): %1.3f' % auc)
            eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.)
            print('Equal Error Rate (EER): %1.3f' % eer)
def main(args):

	with tf.Graph().as_default():

		with tf.Session() as sess:

			# create output directory if it doesn't exist
			output_dir = os.path.expanduser(args.output_dir)
			if not os.path.isdir(output_dir):
				os.makedirs(output_dir)

			# load the model
			print("Loading trained model...\n")
			meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.trained_model_dir))
			facenet.load_model(args.trained_model_dir, meta_file, ckpt_file)

			# grab all image paths and labels
			print("Finding image paths and targets...\n")
			data = load_files(args.data_dir, load_content=False, shuffle=False)
			labels_array = data['target']
			paths = data['filenames']

			# Get input and output tensors
			images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
			embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
			phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

			image_size = images_placeholder.get_shape()[1]
			embedding_size = embeddings.get_shape()[1]

			# Run forward pass to calculate embeddings
			print('Generating embeddings from images...\n')
			start_time = time.time()
			batch_size = args.batch_size
			nrof_images = len(paths)
			nrof_batches = int(np.ceil(1.0*nrof_images / batch_size))
			emb_array = np.zeros((nrof_images, embedding_size))
			for i in xrange(nrof_batches):
				start_index = i*batch_size
				end_index = min((i+1)*batch_size, nrof_images)
				paths_batch = paths[start_index:end_index]
				images = facenet.load_data(paths_batch, do_random_crop=False, do_random_flip=False, image_size=image_size, do_prewhiten=True)
				feed_dict = { images_placeholder:images, phase_train_placeholder:False}
				emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)

			time_avg_forward_pass = (time.time() - start_time) / float(nrof_images)
			print("Forward pass took avg of %.3f[seconds/image] for %d images\n" % (time_avg_forward_pass, nrof_images))

			print("Finally saving embeddings and gallery to: %s" % (output_dir))
			# save the gallery and embeddings (signatures) as numpy arrays to disk
			np.save(os.path.join(output_dir, "gallery.npy"), labels_array)
			np.save(os.path.join(output_dir, "signatures.npy"), emb_array)
Example #8
0
def main(argv=None):
  
    pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs))
    paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs)
    
    with tf.Graph().as_default():

        with tf.Session() as sess:
            
            # Load the model
            print('Loading model "%s"' % FLAGS.model_file)
            facenet.load_model(FLAGS.model_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            image_size = images_placeholder.get_shape()[1]
            
            # Run forward pass to calculate embeddings
            nrof_images = len(paths)
            nrof_batches = int(nrof_images / FLAGS.batch_size)
            emb_list = []
            for i in range(nrof_batches):
                start_time = time.time()
                paths_batch = paths[i*FLAGS.batch_size:(i+1)*FLAGS.batch_size]
                images = facenet.load_data(paths_batch, False, False, image_size)
                feed_dict = { images_placeholder: images, phase_train_placeholder: False }
                emb_list += sess.run([embeddings], feed_dict=feed_dict)
                duration = time.time() - start_time
                print('Calculated embeddings for batch %d of %d: time=%.3f seconds' % (i+1,nrof_batches, duration))
            emb_array = np.vstack(emb_list)  # Stack the embeddings to a nrof_examples_per_epoch x 128 matrix

            # Calculate evaluation metrics
            thresholds = np.arange(0, 4, 0.01)
            embeddings1 = emb_array[0::2]
            embeddings2 = emb_array[1::2]
            tpr, fpr, accuracy = facenet.calculate_roc(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), FLAGS.seed)
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            thresholds = np.arange(0, 4, 0.001)
            val, val_std, far = facenet.calculate_val(thresholds, embeddings1, embeddings2, np.asarray(actual_issame), 1e-3, FLAGS.seed)
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            facenet.plot_roc(fpr, tpr, 'NN4')
Example #9
0
def main(args):

    images = load_and_align_data(args.image_files, args.image_size, args.margin, args.gpu_memory_fraction)
    with tf.Graph().as_default():

        with tf.Session() as sess:
      
            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
    
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            #phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
            feed_dict = { images_placeholder: images} #phase_train_placeholder:False }
            emb = sess.run(embeddings, feed_dict=feed_dict)
            
            nrof_images = len(args.image_files)

            print('Images:')
            for i in range(nrof_images):
                print('%1d: %s' % (i, args.image_files[i]))
            print('')
            
            # Print distance matrix
            print('Distance matrix')
            print('    ', end='')
            for i in range(nrof_images):
                print('    %1d     ' % i, end='')
            print('')
            for i in range(nrof_images):
                print('%1d  ' % i, end='')
                for j in range(nrof_images):
                    dist = np.sqrt(np.sum(np.square(np.subtract(emb[i,:], emb[j,:]))))
                    print('  %1.4f  ' % dist, end='')
                print('')
Example #10
0
def main(args):
    align = align_dlib.AlignDlib(os.path.expanduser(args.dlib_face_predictor))
    #image_paths = [args.image1, args.image2]
    landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE
    images_list = get_image_paths(args.image_folder)



    with tf.Graph().as_default():

        with tf.Session() as sess:
      
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
 
            while True:   
              # Get input and output tensors
              #image_paths = [args.image1, args.image2]
              #image_paths = [images_list[0],images_list[1],images_list[2]]
              image_paths = images_list
              pdb.set_trace()
              images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
              phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
              embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
              #image_size = int(images_placeholder.get_shape()[1])
              image_size = 96           
             
              # Run forward pass to calculate embeddings
              images = load_and_align_data(image_paths, image_size, align, landmarkIndices)
              feed_dict = { images_placeholder: images, phase_train_placeholder: False }
              emb = sess.run(embeddings, feed_dict=feed_dict)
              #dist = np.sqrt(np.sum(np.square(np.subtract(emb[0,:], emb[1,:]))))
              n = len(image_paths)
              dist = np.zeros([n,n])
              for i in range(n):
                 for j in range(n):
                   dist[i,j] = np.dot(emb[i,:],emb[j,:])
              print('Cosine distance between the embeddings: ')
              print(dist)
Example #11
0
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
            
            image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
            control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
            phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
 
            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                        dtypes=[tf.string, tf.int32, tf.int32],
                                        shapes=[(1,), (1,), (1,)],
                                        shared_name=None, name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(eval_input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)
     
            # Load the model
            input_map = {'image_batch': image_batch, 'label_batch': label_batch, 'phase_train': phase_train_placeholder}
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
#              
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess, eval_enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder,
                embeddings, label_batch, paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, args.distance_metric, args.subtract_mean,
                args.use_flipped_images, args.use_fixed_image_standardization)
def main(args):
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Load the model metagraph and checkpoint
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            output_node_names = 'embeddings'
            whitelist_names = []
            for node in sess.graph.as_graph_def().node:
                if node.name.startswith('InceptionResnetV1') or node.name.startswith('embeddings') or node.name.startswith('phase_train'):
                    print(node.name)
                    whitelist_names.append(node.name)

            output_graph_def = graph_util.convert_variables_to_constants(
                sess, sess.graph.as_graph_def(), output_node_names.split(","), 
                variable_names_whitelist=whitelist_names)
        
        with tf.gfile.GFile(args.output_file, 'wb') as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node)) #pylint: disable=no-member
Example #13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', help='Path of the video you want to test on.', default=0)
    args = parser.parse_args()
    
    # Cai dat cac tham so can thiet
    MINSIZE = 20
    THRESHOLD = [0.6, 0.7, 0.7]
    FACTOR = 0.709
    IMAGE_SIZE = 182
    INPUT_IMAGE_SIZE = 160
    CLASSIFIER_PATH = 'Models/facemodel.pkl'
    VIDEO_PATH = args.path
    FACENET_MODEL_PATH = 'Models/20180402-114759.pb'

    # Load model da train de nhan dien khuon mat - thuc chat la classifier
    with open(CLASSIFIER_PATH, 'rb') as file:
        model, class_names = pickle.load(file)
    print("Custom Classifier, Successfully loaded")

    with tf.Graph().as_default():

        # Cai dat GPU neu co
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))

        with sess.as_default():

            # Load model MTCNN phat hien khuon mat
            print('Loading feature extraction model')
            facenet.load_model(FACENET_MODEL_PATH)

            # Lay tensor input va output
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Cai dat cac mang con
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, "src/align")

            people_detected = set()
            person_detected = collections.Counter()

            # Lay hinh anh tu file video
            cap = cv2.VideoCapture(VIDEO_PATH)

            while (cap.isOpened()):
                # Doc tung frame
                ret, frame = cap.read()

                # Phat hien khuon mat, tra ve vi tri trong bounding_boxes
                bounding_boxes, _ = align.detect_face.detect_face(frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

                faces_found = bounding_boxes.shape[0]
                try:
                    # Neu co it nhat 1 khuon mat trong frame
                    if faces_found > 0:
                        det = bounding_boxes[:, 0:4]
                        bb = np.zeros((faces_found, 4), dtype=np.int32)
                        for i in range(faces_found):
                            bb[i][0] = det[i][0]
                            bb[i][1] = det[i][1]
                            bb[i][2] = det[i][2]
                            bb[i][3] = det[i][3]

                            # Cat phan khuon mat tim duoc
                            cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :]
                            scaled = cv2.resize(cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                                interpolation=cv2.INTER_CUBIC)
                            scaled = facenet.prewhiten(scaled)
                            scaled_reshape = scaled.reshape(-1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                            feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False}
                            emb_array = sess.run(embeddings, feed_dict=feed_dict)

                            # Dua vao model de classifier
                            predictions = model.predict_proba(emb_array)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[
                                np.arange(len(best_class_indices)), best_class_indices]

                            # Lay ra ten va ty le % cua class co ty le cao nhat
                            best_name = class_names[best_class_indices[0]]
                            print("Name: {}, Probability: {}".format(best_name, best_class_probabilities))

                            # Ve khung mau xanh quanh khuon mat
                            cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                            text_x = bb[i][0]
                            text_y = bb[i][3] + 20

                            # Neu ty le nhan dang > 0.5 thi hien thi ten
                            if best_class_probabilities > 0.5:
                                name = class_names[best_class_indices[0]]
                            else:
                                # Con neu <=0.5 thi hien thi Unknow
                                name = "Unknown"

                            # Viet text len tren frame
                            cv2.putText(frame, name, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255), thickness=1, lineType=2)
                            cv2.putText(frame, str(round(best_class_probabilities[0], 3)), (text_x, text_y + 17),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255), thickness=1, lineType=2)
                            person_detected[best_name] += 1
                except:
                    pass

                # Hien thi frame len man hinh
                cv2.imshow('Face Recognition', frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            cap.release()
            cv2.destroyAllWindows()
Example #14
0
def restore_facenet_model(model_path):
    # load the model
    print('Loading feature extraction model')
    facenet.load_model(model_path)
Example #15
0
        threshold = [0.6, 0.7, 0.7]  # three steps's threshold
        factor = 0.709  # scale factor
        margin = 44
        frame_interval = 3
        batch_size = 1000
        image_size = 182
        input_image_size = 160

        # # classes = ['circle', 'diamond', 'egg', 'long', 'polygon', 'square', 'triangle']    #train human name
        # with open(config.classes_map, 'rb') as f:
        #     class_names = pickle.load(f)
        #     print(class_names)

        print('Loading feature extraction model')
        modeldir = args.model_params
        facenet.load_model(resource_path(modeldir))

        images_placeholder = tf.get_default_graph().get_tensor_by_name(
            "input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name(
            "phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        classifier_filename = os.path.join(args.clf_dir, args.clf_name)
        classifier_filename_exp = resource_path(
            os.path.expanduser(classifier_filename))
        with open(classifier_filename_exp, 'rb') as infile:
            (model, class_names) = pickle.load(infile)
            print('load classifier file-> %s' % classifier_filename_exp)
Example #16
0
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:

            load_start = time.time()
            np.random.seed(seed=args.seed)
            
            if args.use_split_dataset:
                dataset_tmp = facenet.get_dataset(args.data_dir)
                train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class)
                if (args.mode=='TRAIN'):
                    dataset = train_set
                elif (args.mode=='CLASSIFY'):
                    dataset = test_set
            else:
                dataset = facenet.get_dataset(args.data_dir)

            # Check that there are at least one training image per class
            for cls in dataset:
                assert len(cls.image_paths)>0, 'There must be at least one image for each class in the dataset'          

                 
            paths, labels = facenet.get_image_paths_and_labels(dataset)
            
            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(args.model)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            
            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(math.ceil(1.0*nrof_images / args.batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                start_index = i*args.batch_size
                end_index = min((i+1)*args.batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False, args.image_size)
                feed_dict = { images_placeholder:images, phase_train_placeholder:False }
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
            
            classifier_filename_exp = os.path.expanduser(args.classifier_filename)
            
            print(f"Loaded in {time.time() - load_start}s")
            print("=======================================\n")
            if (args.mode=='TRAIN'):
                print("# Training data....... ")
                print('Number of classes: %d' % len(dataset))
                print('Number of images: %d\n' % len(paths))
                train_start = time.time()
                # Train classifier
                print('Training classifier')
                model = SVC(kernel='linear', probability=True)
                model.fit(emb_array, labels)
            
                # Create a list of class names
                class_names = [ cls.name.replace('_', ' ') for cls in dataset]

                # Saving classifier model
                with open(classifier_filename_exp, 'wb') as outfile:
                    pickle.dump((model, class_names), outfile)
                print('Saved classifier model to file "%s"' % classifier_filename_exp)
                print(f"Trained in {time.time()-train_start}s")

            elif (args.mode=='CLASSIFY'):
                # Classify images
                classify_time = time.time()
                print('Testing classifier')
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                print('Loaded classifier model from file "%s"' % classifier_filename_exp)
                predictions = model.predict_proba(emb_array)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                
                for i in range(len(best_class_indices)):
                    print('%4d  %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i]))
                    
                accuracy = np.mean(np.equal(best_class_indices, labels))
                print('Accuracy: %.3f' % accuracy)
                print("=============================================")
                print(f"finished in {time.time()-classify_time}s")
def main(argv=None):
    
    pairs = read_pairs(os.path.expanduser(FLAGS.lfw_pairs))
    paths, actual_issame = get_paths(os.path.expanduser(FLAGS.lfw_dir), pairs)
    
    with tf.Graph().as_default():

        with tf.Session() as sess:
    
            # Load the model
            print('Loading model "%s"' % FLAGS.model_file)
            facenet.load_model(FLAGS.model_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            image_size = int(images_placeholder.get_shape()[1])
            
            # Run test on LFW to check accuracy for different horizontal/vertical translations of input images
            if True:
                offsets = np.arange(-30, 31, 3)
                horizontal_offset_accuracy = [None] * len(offsets)
                for idx, offset in enumerate(offsets):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, translate_images, (offset,0))
                    print('Hoffset: %1.3f  Accuracy: %1.3f+-%1.3f' % (offset, np.mean(accuracy), np.std(accuracy)))
                    horizontal_offset_accuracy[idx] = np.mean(accuracy)
                vertical_offset_accuracy = [None] * len(offsets)
                for idx, offset in enumerate(offsets):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, translate_images, (0,offset))
                    print('Voffset: %1.3f  Accuracy: %1.3f+-%1.3f' % (offset, np.mean(accuracy), np.std(accuracy)))
                    vertical_offset_accuracy[idx] = np.mean(accuracy)
                fig = plt.figure(1)
                plt.plot(offsets, horizontal_offset_accuracy, label='Horizontal')
                plt.plot(offsets, vertical_offset_accuracy, label='Vertical')
                plt.legend()
                plt.grid(True)
                plt.title('Translation invariance on LFW')
                plt.xlabel('Offset [pixels]')
                plt.ylabel('Accuracy')
                plt.show()
                result_dir = os.path.expanduser(FLAGS.model_dir)
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_translation.png'))
                save_result(offsets, horizontal_offset_accuracy, os.path.join(result_dir, 'invariance_translation_horizontal.txt'))
                save_result(offsets, vertical_offset_accuracy, os.path.join(result_dir, 'invariance_translation_vertical.txt'))

            # Run test on LFW to check accuracy for different rotation of input images
            if True:
                angles = np.arange(-30, 31, 3)
                rotation_accuracy = [None] * len(angles)
                for idx, angle in enumerate(angles):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, rotate_images, angle)
                    print('Angle: %1.3f  Accuracy: %1.3f+-%1.3f' % (angle, np.mean(accuracy), np.std(accuracy)))
                    rotation_accuracy[idx] = np.mean(accuracy)
                fig = plt.figure(2)
                plt.plot(angles, rotation_accuracy)
                plt.grid(True)
                plt.title('Rotation invariance on LFW')
                plt.xlabel('Angle [deg]')
                plt.ylabel('Accuracy')
                plt.show()
                result_dir = os.path.expanduser(FLAGS.model_dir)
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_rotation.png'))
                save_result(angles, rotation_accuracy, os.path.join(result_dir, 'invariance_rotation.txt'))

            # Run test on LFW to check accuracy for different scaling of input images
            if True:
                scales = np.arange(0.5, 1.5, 0.05)
                scale_accuracy = [None] * len(scales)
                for scale_idx, scale in enumerate(scales):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, paths, actual_issame, scale_images, scale)
                    print('Scale: %1.3f  Accuracy: %1.3f+-%1.3f' % (scale, np.mean(accuracy), np.std(accuracy)))
                    scale_accuracy[scale_idx] = np.mean(accuracy)
                fig = plt.figure(3)
                plt.plot(scales, scale_accuracy)
                plt.grid(True)
                plt.title('Scale invariance on LFW')
                plt.xlabel('Scale')
                plt.ylabel('Accuracy')
                plt.show()
                result_dir = os.path.expanduser(FLAGS.model_dir)
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_scale.png'))
                save_result(scales, scale_accuracy, os.path.join(result_dir, 'invariance_scale.txt'))
Example #18
0
 def __init__(self):
     self.sess = tf.Session()
     with self.sess.as_default():
         facenet.load_model(facenet_model_checkpoint)
Example #19
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Get the paths for the corresponding images

            vinayak = [
                'datasets/kar_Vin_aligned/vinayak/' + f
                for f in os.listdir('datasets/kar_Vin_aligned/vinayak')
            ]
            karthik = [
                'datasets/kar_Vin_aligned/karthik/' + f
                for f in os.listdir('datasets/kar_Vin_aligned/karthik')
            ]
            ashish = [
                'datasets/kar_Vin_aligned/Ashish/' + f
                for f in os.listdir('datasets/kar_Vin_aligned/Ashish')
            ]
            saurabh = [
                'datasets/kar_Vin_aligned/Saurabh/' + f
                for f in os.listdir('datasets/kar_Vin_aligned/Saurabh')
            ]
            hari = [
                'datasets/kar_Vin_aligned/Hari/' + f
                for f in os.listdir('datasets/kar_Vin_aligned/Hari')
            ]
            paths = vinayak + karthik + ashish + saurabh + hari
            #np.save("images.npy",paths)
            # Load the model
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            images_placeholder = tf.image.resize_images(
                images_placeholder, (160, 160))
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")

            image_size = args.image_size
            embedding_size = embeddings.get_shape()[1]
            extracted_dict = {}

            # Run forward pass to calculate embeddings
            for i, filename in enumerate(paths):

                images = facenet.load_image(filename, False, False, image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                feature_vector = sess.run(embeddings, feed_dict=feed_dict)
                extracted_dict[filename] = feature_vector
                if (i % 100 == 0):
                    print("completed", i, " images")

            with open('extracted_dict.pickle', 'wb') as f:
                pickle.dump(extracted_dict, f)
def main(args, q1, q2, q3):
    minsize = 20
    threshold = [0.6, 0.7, 0.7]
    factor = 0.709
    image_size = 182
    input_image_size = 160
    global fg, fg2
    fg2 = 405
    fg = 404

    # comment out these lines if you do not want video recording
    # USE FOR RECORDING VIDEO
    fourcc = cv2.VideoWriter_fourcc(*'X264')

    # Get the path of the classifier and load it
    project_root_folder = os.path.join(os.path.abspath(__file__), "..\\..")
    classifier_path = project_root_folder + "\\trained_classifier\\our_newglint_classifier201015.pkl"
    print(classifier_path)
    with open(classifier_path, 'rb') as f:
        (model, class_names) = pickle.load(f)
        print("Loaded classifier file")

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            # Bounding box
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, project_root_folder + "\\src\\align")
            # Get the path of the facenet model and load it
            facenet_model_path = project_root_folder + "\\facenet_model\\20170512-110547\\20170512-110547.pb"
            facenet.load_model(facenet_model_path)

            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Start video capture
            people_detected = set()

            person_detected = collections.Counter()

            cv2.namedWindow('Face Detection and Identification', 0)
            width = 640
            height = 480

            video_recording = cv2.VideoWriter(args.respath + '/res_video/output.avi', fourcc, 7,
                                              (int(width), int(height)))

            total_frames_passed = 0
            unknowpeople = 0
            knowpeople = 0
            while True:
                try:
                    if len(q1) == 0:
                        continue
                    frame = q1.pop()
                    if args.flip == True:
                        frame = cv2.flip(frame, 1)
                except Exception as e:
                    break

                # Skip frames if video is to be sped up
                if args.video_speedup:
                    total_frames_passed += 1
                    if total_frames_passed % args.video_speedup != 0:
                        continue

                bounding_boxes, points = align.detect_face.detect_face(frame, minsize, pnet, rnet, onet, threshold, factor)
                faces_found = bounding_boxes.shape[0]


                ## 识别框
                # cv2.putText(frame, 'Identification Box', (190, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                #             1, (0, 0, 255),
                #             thickness=2,
                #             lineType=1)
                windowname = "测温请靠近!"
                frame = cv2ImgAddText(frame, windowname, 235, 60, (155, 0, 0), 25)
                cv2.rectangle(frame, (185, 95), (430, 400), (165, 245, 25), 2)

                if faces_found > 0:
                    det = bounding_boxes[:, 0:4]

                    bb = np.zeros((faces_found, 4), dtype=np.int32)
                    for i in range(faces_found):
                        bb[i][0] = det[i][0]
                        bb[i][1] = det[i][1]
                        bb[i][2] = det[i][2]
                        bb[i][3] = det[i][3]

                        cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (255, 150, 0),
                                      2)  # boxing face
                        # inner exception
                        if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][2] >= len(frame[0]) or bb[i][3] >= len(frame):
                            print('face is inner of range!')
                            continue

                        cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :]  # 这里是把检测到的人脸进行裁减出来
                        scaled = cv2.resize(cropped, (input_image_size, input_image_size),  # 将裁剪出来的人脸resize成统一大小
                                            interpolation=cv2.INTER_CUBIC)
                        # cv2.imshow("Cropped and scaled", scaled)
                        # cv2.waitKey(1)
                        scaled = facenet.prewhiten(scaled)  # 这里应该是把裁剪出来的人脸进行图像处理的结果
                        # cv2.imshow("\"Whitened\"", scaled)
                        # cv2.waitKey(1)

                        scaled_reshape = scaled.reshape(-1, input_image_size, input_image_size, 3)
                        feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False}
                        emb_array = sess.run(embeddings, feed_dict=feed_dict)
                        predictions = model.predict_proba(emb_array)
                        #print(predictions)
                        best_class_indices = np.argmax(predictions, axis=1)
                        person_id = best_class_indices[0]
                        # print(predictions[0][x])
                        best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                        best_name = class_names[best_class_indices[0]]
                        print("Name_all: {}, Probability: {}".format(best_name, best_class_probabilities))


                        #这里是找到识别区域中最大的脸,进行测温
                        if args.use_tem == True:
                            if bb[i][0]>180 and bb[i][2]<435 and bb[i][1]>70 and bb[i][3]<408:
                                xmin = bb[i][0].astype(int)
                                xmax = bb[i][2].astype(int)
                                ymin = bb[i][1].astype(int)
                                ymax = bb[i][3].astype(int)
                                h = ymax - ymin
                                w = xmax - xmin
                                area = h * w
                                print('-----------------------------------------------面积', area)
                                if area >= 245*305*0.08:# 245*305*0.08 = 5978
                                    try:
                                        tems = q2.pop()
                                        diss = q3.pop()
                                    except:
                                        tems = 0
                                        diss = 0
                                    #tems, diss, jz = temperature()
                                    if tems > 20:
                                        font = cv2.FONT_HERSHEY_COMPLEX_SMALL
                                        cv2.putText(frame, '{}'.format(tems), (bb[i][0]+5, bb[i][1]+35),
                                                    font,
                                                    2, (55, 255, 30), thickness=2, lineType=2)
                                        #if 100 < diss < 500:
                                        if tems <= 37:
                                            fg = 0
                                            #print('----------------------------------main fg:',fg)
                                        else:
                                            fg = 1
                                            # print('----------------------------------main fg:',fg)
                                        # else:
                                        #     fg = 404
                                else:
                                    fg = 404

                            else:
                                fg = 404
                                #print('----------------------------------main fg:', fg)



                        #在检测框上标注是别人id及score
                        if best_class_probabilities > 0.3:
                            #print("{} : {}".format(person_id, str(best_class_probabilities[0])[0:4]))
                            id_score = "{}:{}".format(person_id, str(best_class_probabilities[0])[0:4])
                            font = cv2.FONT_HERSHEY_COMPLEX_SMALL  # cv2.FONT_HERSHEY_SCRIPT_COMPLEX #cv2.FONT_HERSHEY_COMPLEX_SMALL,
                            cv2.putText(frame, id_score, (bb[i][0] + 5, bb[i][1] - 3),
                                        font,
                                        1, (0, 0, 200), thickness=2, lineType=3)
                        #进行人脸识别
                        if best_class_probabilities > args.knowperson_threshold:  # 0.90:
                            # cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0),
                            #               2)  # boxing face
                            knowpeople += 1
                            print("Name_True_rec: {}, Probability: {}".format(best_name, best_class_probabilities))
                            text_xmin = bb[i][0]
                            text_y = bb[i][3] + 20
                            text_ymax = bb[i][3] + 30
                            text_ymin = bb[i][3]
                            text_xmax = bb[i][2]
                            cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0),
                                          2)  # boxing face
                            # cv2.rectangle(frame, (text_xmin, text_ymin), (text_xmax, text_ymax), (0, 255, 0),
                            #               cv2.FILLED)  # name box
                            font = cv2.FONT_HERSHEY_COMPLEX_SMALL  # cv2.FONT_HERSHEY_SCRIPT_COMPLEX #cv2.FONT_HERSHEY_COMPLEX_SMALL,
                            cv2.putText(frame, class_names[best_class_indices[0]], (text_xmin+5, text_y),
                                        font,
                                        1, (14, 173, 238), thickness=2, lineType=3)
                            person_detected[best_name] += 1

                            # 设置签到门槛,确保不错误识别
                            if best_class_probabilities > 0.95:
                                signin(class_names[best_class_indices[0]], frame)
                                if bb[i][0] > 180 and bb[i][2] < 435 and bb[i][1] > 70 and bb[i][3] < 408:
                                    fg2 = 2
                                    #print('----------------------------------main fg:', fg)
                                else:
                                    fg2 = 405
                                    #print('----------------------------------main fg:', fg)



                        #进行陌生人判断
                        elif best_class_probabilities < args.unknowperson_threshold:  # 0.6:
                            # cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0),
                            #               2)  # boxing face

                            #这里对unknow进行干扰过滤,防止误触发
                            unknowpeople += 1 #这行和下五行的可行性待验证!!!
                            if knowpeople == 20:
                                knowpeople = 0
                                unknowpeople = 0
                            #print('unknowpeople:',unknowpeople)
                            if unknowpeople % 1 == 0:
                                text_x = bb[i][0]
                                text_y = bb[i][3] + 20
                                cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 0, 255),
                                              2)  # boxing face
                                cv2.putText(frame, 'unknow', (text_x, text_y),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            1, (0, 0, 200), thickness=1, lineType=2)
                                print("Name_unknow_rec: unknow, Probability: {}".format(best_class_probabilities))
                                unknowpeople = 0
                                # 陌生人脸帧留档
                                if args.record_unknow == True:
                                    for x in range(20):
                                        if not x % 10 == 0:
                                            continue
                                        if best_class_probabilities < args.stranger_threshold:  # 0.5:
                                            strange_in_time = str(time.strftime('%Y-%m-%d_%H-%M-%S'))
                                            # print(strange_in_time)
                                            cv2.imwrite(args.respath + "/stranger/" + strange_in_time + ".jpg", frame)

                    for person, count in person_detected.items():
                        if count > 4:
                            print("Person Detected: {}, Count: {}".format(person, count))
                            people_detected.add(person)

                # cv2.putText(frame, "People detected so far:", (20, 20), cv2.FONT_HERSHEY_PLAIN,
                #             1, (255, 0, 0), thickness=1, lineType=2)
                fg2 = 405
                mainwindowname = "人脸验证及测温系统"
                frame = cv2ImgAddText(frame, mainwindowname, 20, 20, (255, 255, 155), 20)

                if args.beauty == True:
                    frame = beautiful(frame)
                cv2.imshow("Face Detection and Identification", frame)
                if args.record == True:
                    video_recording.write(frame)
                if cv2.waitKey(1) & 0xFF == 27:
                    break

                # #当前线程数
                # xcs = len(threading.enumerate())
                # print("当前线程数:{}".format(xcs))
                print("-----------------------------------------------检测识别")
    video_recording.release()
    #video_capture.release()
    cv2.destroyAllWindows()
Example #21
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # create output directory if it doesn't exist
            output_dir = os.path.expanduser(args.output_dir)
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)

            # load the model
            print("Loading trained model...\n")
            meta_file, ckpt_file = facenet.get_model_filenames(
                os.path.expanduser(args.trained_model_dir))
            facenet.load_model(args.trained_model_dir)

            # grab all image paths and labels
            print("Finding image paths and targets...\n")
            data = load_files(args.data_dir, load_content=False, shuffle=False)
            labels_array = data['target']
            paths = data['filenames']

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")

            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Generating embeddings from images...\n')
            start_time = time.time()
            batch_size = args.batch_size
            nrof_images = len(paths)
            nrof_batches = int(np.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches):
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch,
                                           do_random_crop=False,
                                           do_random_flip=False,
                                           image_size=image_size,
                                           do_prewhiten=True)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            time_avg_forward_pass = (time.time() -
                                     start_time) / float(nrof_images)
            print(
                "Forward pass took avg of %.3f[seconds/image] for %d images\n"
                % (time_avg_forward_pass, nrof_images))

            for emb_idx, emb in enumerate(emb_array):
                norm_emb = emb / np.linalg.norm(emb)
                print(norm_emb)
                encoded_word = ""
                q = 30
                for i, wi in enumerate(norm_emb):
                    val = wi * q
                    val_abs = math.fabs(val)
                    prefix = ("F" if val > 0 else "f")
                    if (val_abs < 0.5):
                        val = 0
                    else:
                        val = math.floor(val_abs)
                    for j in range(val):
                        encoded_word += prefix + str(i)

                print("----> " + str(emb_idx))
                print(encoded_word)
def main(args):
  
    network = importlib.import_module(args.model_def)
    image_size = (args.image_size, args.image_size)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    stat_file_name = os.path.join(log_dir, 'stat.h5')

    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(log_dir, 'arguments.txt'))
        
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    dataset = facenet.get_dataset(args.data_dir)
    if args.filter_filename:
        dataset = filter_dataset(dataset, os.path.expanduser(args.filter_filename),
            args.filter_percentile, args.filter_min_nrof_images_per_class)
        
    if args.validation_set_split_ratio>0.0:
        train_set, val_set = facenet.split_dataset(dataset, args.validation_set_split_ratio, args.min_nrof_val_images_per_class, 'SPLIT_IMAGES')
    else:
        train_set, val_set = dataset, []
        
    nrof_classes = len(train_set) #类目数
    
    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)
    
    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        # Get the paths for the corresponding images
    #lfw_paths为两两比较的列表,列表的元素为有2个元素的元祖,actual_issame为列表,表示每个元素是否为同一个人
        lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
    
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)
        
        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        assert len(image_list)>0, 'The training set should not be empty'
        
        val_image_list, val_label_list = facenet.get_image_paths_and_labels(val_set)

        # Create a queue that produces indices into the image_list and label_list 
        labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
        range_size = array_ops.shape(labels)[0]
        index_queue = tf.train.range_input_producer(range_size, num_epochs=None,
                             shuffle=True, seed=None, capacity=32) #capacity代表队列容量
        
        index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue')
        
        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')
        batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size')
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths')
        labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels')
        control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control')
        
        nrof_preprocess_threads = 4
        input_queue = data_flow_ops.FIFOQueue(capacity=2000000,
                                    dtypes=[tf.string, tf.int32, tf.int32],
                                    shapes=[(1,), (1,), (1,)],
                                    shared_name=None, name=None)
        enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder, control_placeholder], name='enqueue_op')
        image_batch, label_batch = facenet.create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batch_size_placeholder)

        image_batch = tf.identity(image_batch, 'image_batch')
        image_batch = tf.identity(image_batch, 'input')
        label_batch = tf.identity(label_batch, 'label_batch')
        
        print('Number of classes in training set: %d' % nrof_classes)
        print('Number of examples in training set: %d' % len(image_list))

        print('Number of classes in validation set: %d' % len(val_set))
        print('Number of examples in validation set: %d' % len(val_image_list))
        
        print('Building training graph')
        
        # Build the inference graph
        prelogits, _ = network.inference(image_batch, args.keep_probability, 
            phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, 
        weight_decay=args.weight_decay)
        logits = slim.fully_connected(prelogits, len(train_set), activation_fn=None, 
                weights_initializer=slim.initializers.xavier_initializer(), 
                scope='Logits', reuse=False)
        
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        # Norm for the prelogits
        eps = 1e-4
        prelogits_norm = tf.reduce_mean(tf.norm(tf.abs(prelogits)+eps, ord=args.prelogits_norm_p, axis=1))
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor)

        # Add center loss
        prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, nrof_classes)
        tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor)

        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step,
            args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)
        
        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch, logits=logits, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        tf.add_to_collection('losses', cross_entropy_mean)
        
        correct_prediction = tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32)
        accuracy = tf.reduce_mean(correct_prediction)
        
        # Calculate the total losses
        regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer, 
            learning_rate, args.moving_average_decay, tf.global_variables(), args.log_histograms)
        
        # Create a saver
        saver_out = tf.train.Saver(tf.trainable_variables(), max_to_keep=3) #max_to_keep代表保存最近的3个模型;如果设置为0或None,则代表保存每一个模型,会占用很大内存(不推荐)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) #设置GPU显存占用率
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
    # tensorflow中的多进程管理,下面两条语句通常配套使用
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                facenet.load_model(pretrained_model)

            # Training and validation loop
            print('Running softmax training')
            nrof_steps = args.max_nrof_epochs*args.epoch_size
            nrof_val_samples = int(math.ceil(args.max_nrof_epochs / args.validate_every_n_epochs))   # Validate every validate_every_n_epochs as well as in the last epoch
            stat = {
                'loss': np.zeros((nrof_steps,), np.float32),
                'center_loss': np.zeros((nrof_steps,), np.float32),
                'reg_loss': np.zeros((nrof_steps,), np.float32),
                'xent_loss': np.zeros((nrof_steps,), np.float32),
                'prelogits_norm': np.zeros((nrof_steps,), np.float32),
                'accuracy': np.zeros((nrof_steps,), np.float32),
                'val_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_xent_loss': np.zeros((nrof_val_samples,), np.float32),
                'val_accuracy': np.zeros((nrof_val_samples,), np.float32),
                'lfw_accuracy': np.zeros((args.max_nrof_epochs,), np.float32),
                'lfw_valrate': np.zeros((args.max_nrof_epochs,), np.float32),
                'learning_rate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_train': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_validate': np.zeros((args.max_nrof_epochs,), np.float32),
                'time_evaluate': np.zeros((args.max_nrof_epochs,), np.float32),
                'prelogits_hist': np.zeros((args.max_nrof_epochs, 1000), np.float32),
              }
            for epoch in range(1,args.max_nrof_epochs+1):
                step = sess.run(global_step, feed_dict=None)
                # Train for one epoch
                t = time.time()
                cont = train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder,
                    learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, global_step, 
                    total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file,
                    stat, cross_entropy_mean, accuracy, learning_rate,
                    prelogits, prelogits_center_loss, args.random_rotate, args.random_crop, args.random_flip, prelogits_norm, args.prelogits_hist_max, args.use_fixed_image_standardization)
                stat['time_train'][epoch-1] = time.time() - t
                
                if not cont:
                    break
                  
                t = time.time()
                if len(val_image_list)>0 and ((epoch-1) % args.validate_every_n_epochs == args.validate_every_n_epochs-1 or epoch==args.max_nrof_epochs):
                    validate(args, sess, epoch, val_image_list, val_label_list, enqueue_op, image_paths_placeholder, labels_placeholder, control_placeholder,
                        phase_train_placeholder, batch_size_placeholder, 
                        stat, total_loss, regularization_losses, cross_entropy_mean, accuracy, args.validate_every_n_epochs, args.use_fixed_image_standardization)
                stat['time_validate'][epoch-1] = time.time() - t

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver_out, summary_writer, model_dir, subdir, epoch)

                # Evaluate on LFW
                t = time.time()
                if args.lfw_dir:
                    evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, control_placeholder, 
                        embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer, stat, epoch, 
                        args.lfw_distance_metric, args.lfw_subtract_mean, args.lfw_use_flipped_images, args.use_fixed_image_standardization)
                stat['time_evaluate'][epoch-1] = time.time() - t

                print('Saving statistics')
                with h5py.File(stat_file_name, 'w') as f:
                    for key, value in stat.items():
                        f.create_dataset(key, data=value)
    
    return model_dir
Example #23
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs)

            image_paths_placeholder = tf.placeholder(tf.string,
                                                     shape=(None, 1),
                                                     name='image_paths')
            labels_placeholder = tf.placeholder(tf.int32,
                                                shape=(None, 1),
                                                name='labels')
            batch_size_placeholder = tf.placeholder(tf.int32,
                                                    name='batch_size')
            control_placeholder = tf.placeholder(tf.int32,
                                                 shape=(None, 1),
                                                 name='control')
            phase_train_placeholder = tf.placeholder(tf.bool,
                                                     name='phase_train')

            nrof_preprocess_threads = 4
            image_size = (args.image_size, args.image_size)
            eval_input_queue = data_flow_ops.FIFOQueue(
                capacity=2000000,
                dtypes=[tf.string, tf.int32, tf.int32],
                shapes=[(1, ), (1, ), (1, )],
                shared_name=None,
                name=None)
            eval_enqueue_op = eval_input_queue.enqueue_many(
                [
                    image_paths_placeholder, labels_placeholder,
                    control_placeholder
                ],
                name='eval_enqueue_op')
            image_batch, label_batch = facenet.create_input_pipeline(
                eval_input_queue, image_size, nrof_preprocess_threads,
                batch_size_placeholder)

            # Load the model
            input_map = {
                'image_batch': image_batch,
                'label_batch': label_batch,
                'phase_train': phase_train_placeholder
            }
            facenet.load_model(args.model, input_map=input_map)

            # Get output tensor
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            #
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord, sess=sess)

            evaluate(sess, eval_enqueue_op, image_paths_placeholder,
                     labels_placeholder, phase_train_placeholder,
                     batch_size_placeholder, control_placeholder, embeddings,
                     label_batch, paths, actual_issame, args.lfw_batch_size,
                     args.lfw_nrof_folds, args.distance_metric,
                     args.subtract_mean, args.use_flipped_images,
                     args.use_fixed_image_standardization)
Example #24
0
def main(args, q1, q2, q3):
    input_image_size = 160
    global fg, fg2, fg3, temwork
    fg = 404
    fg2 = 405
    fg3 = 406
    temwork = 0

    # comment out these lines if you do not want video recording
    # USE FOR RECORDING VIDEO
    fourcc = cv2.VideoWriter_fourcc(*'X264')

    # Get the path of the classifier and load it
    project_root_folder = os.path.join(os.path.abspath(__file__), "..\\..")
    # classifier_path = project_root_folder + "\\trained_classifier\\our_newglint_classifier_512_201028.pkl"  # 512维facenet人脸识别模型
    classifier_path = project_root_folder + "\\trained_classifier\\our_newglint_classifier_128_201029-5.pkl"  # 128维facenet人脸识别模型
    # print(classifier_path)

    with open(classifier_path, 'rb') as f:
        (model, class_names) = pickle.load(f)
        print("Loaded classifier file")

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            # Get the path of the facenet model and load it
            # facenet_model_path = project_root_folder + "\\facenet_model\\20180402-114759\\20180402-114759.pb"  # 512维facenet预训练模型
            facenet_model_path = project_root_folder + "\\facenet_model\\20170512-110547\\20170512-110547.pb"  # 128维facenet预训练模型
            facenet.load_model(facenet_model_path)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            # embedding_size = embeddings.get_shape()[1]

            # Start video capture
            people_detected = set()
            person_detected = collections.Counter()

            cv2.namedWindow('Face Detection and Identification', 0)
            while True:
                if len(q1) == 0:
                    continue
                frame = q1.pop()
                height = frame.shape[0]
                width = frame.shape[1]
                break
            video_recording = cv2.VideoWriter(
                args.respath + '/res_video/output_{}.avi'.format(
                    time.strftime('%Y-%m-%d_%H-%M-%S')), fourcc, 10,
                (width, height))
            total_frames_passed = 0
            unknowpeople = 0
            knowpeople = 0

            # 循环读取缓存序列中的视频帧
            while True:
                # time.sleep(0.05)
                try:
                    if len(q1) == 0:
                        continue
                    frame = q1.pop()
                    h = frame.shape[0]
                    w = frame.shape[1]
                    # print(h, w)
                    if args.flip == True:
                        frame = cv2.flip(frame, 1)
                except Exception as e:
                    break

                # Skip frames if video is to be sped up
                if args.video_speedup:
                    total_frames_passed += 1
                    if total_frames_passed % args.video_speedup != 0:
                        continue
                # bounding_boxes返回[class_id, conf, xmin, ymin, xmax, ymax]
                start = time.time()
                bounding_boxes = detect_face(frame,
                                             conf_thresh=0.5,
                                             iou_thresh=0.4,
                                             target_shape=(260, 260),
                                             draw_result=False,
                                             show_result=False)
                # print(bounding_boxes)
                end = time.time()
                # print("detect face: ", end-start)
                faces_found = len(bounding_boxes)

                ## 识别框
                windowname = "测温请靠近!"
                xmin = int(w / 2 - 120)
                ymin = int(h / 2 - 180)
                xmax = int(w / 2 + 120)
                ymax = int(h / 2 + 120)
                frame2 = cv2ImgAddText(frame, windowname, xmin + 38, ymin - 80,
                                       (155, 0, 0), 25)
                cv2.rectangle(frame2, (xmin, ymin - 30), (xmax, ymax),
                              (165, 245, 25), 1)
                window_area = (xmax - xmin) * (ymax - ymin + 30)

                det = bounding_boxes
                if faces_found > 0:
                    temwork = 1
                    bb = np.zeros((faces_found, 4), dtype=np.int32)
                    for i in range(faces_found):
                        bb[i][0] = det[i][2]
                        bb[i][1] = det[i][3]
                        bb[i][2] = det[i][4]
                        bb[i][3] = det[i][5]
                        class_id = det[i][0]  # 戴口罩判断
                        conf = det[i][1]  # 口罩检测分数

                        cv2.rectangle(frame2, (bb[i][0], bb[i][1]),
                                      (bb[i][2], bb[i][3]), (255, 150, 0),
                                      2)  # boxing face
                        if class_id == 0:
                            color = (0, 255, 0)
                            fg3 = 406
                        else:
                            color = (0, 0, 255)
                            fg3 = 3
                        # cv2.putText(frame, "%s:%.2f" % (id2class[class_id], conf), (bb[i][0], bb[i][3]+40),
                        #             cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness=1, lineType=2)
                        cv2.putText(frame2,
                                    "%s" % (id2class[class_id]),
                                    (bb[i][0], bb[i][1] - 20),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    0.8,
                                    color,
                                    thickness=1,
                                    lineType=2)

                        # inner exception
                        if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][2] >= len(
                                frame[0]) or bb[i][3] >= len(frame):
                            # print('face is inner of range!')
                            continue

                        cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i]
                                        [2], :]  # 这里是把检测到的人脸进行裁减出来
                        # cv2.imshow('cropped', cropped)
                        # cv2.waitKey(1)
                        scaled = cv2.resize(
                            cropped,
                            (input_image_size,
                             input_image_size),  # 将裁剪出来的人脸resize成统一大小
                            interpolation=cv2.INTER_CUBIC)
                        scaled = facenet.prewhiten(
                            scaled)  # 这里应该是把裁剪出来的人脸进行图像处理的结果
                        scaled_reshape = scaled.reshape(
                            -1, input_image_size, input_image_size, 3)
                        feed_dict = {
                            images_placeholder: scaled_reshape,
                            phase_train_placeholder: False
                        }
                        emb_array = sess.run(embeddings, feed_dict=feed_dict)
                        pre_start = time.time()
                        predictions = model.predict_proba(emb_array)
                        # print(predictions)
                        pre_end = time.time()
                        # print("predict face: ", pre_end-pre_start)
                        best_class_indices = np.argmax(
                            predictions, axis=1)  # 按行比较数组的大小,返回最大数的索引
                        person_id = best_class_indices[0]
                        # print(predictions[0][x])
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)),
                            best_class_indices]
                        best_name = class_names[best_class_indices[0]]
                        # print("Name_all: {}, Probability: {}".format(best_name, best_class_probabilities))

                        # 这里是找到识别区域中最大的脸,进行测温
                        if args.use_tem == True:
                            if bb[i][0] > xmin - 80 and bb[i][
                                    2] < xmax + 80 and bb[i][
                                        1] > ymin - 80 and bb[i][3] < ymax + 80:
                                box_xmin = bb[i][0].astype(int)
                                box_xmax = bb[i][2].astype(int)
                                box_ymin = bb[i][1].astype(int)
                                box_ymax = bb[i][3].astype(int)
                                h = box_ymax - box_ymin
                                w = box_xmax - box_xmin
                                area = h * w
                                # print('-----------------------------------------------面积', area)
                                if area >= window_area * 0.2:
                                    try:
                                        tems = q2.pop()
                                        diss = q3.pop()
                                    except:
                                        if len(q2) == 0:
                                            tems = tems
                                        if len(q3) == 0:
                                            diss = diss
                                        # tems = 0  # round(random.uniform(36.1, 36.5), 1)
                                    # tems, diss, jz = temperature()
                                    if tems > 20:
                                        font = cv2.FONT_HERSHEY_COMPLEX_SMALL
                                        cv2.putText(
                                            frame2,
                                            '{}'.format(tems),
                                            (bb[i][0] + 5, bb[i][1] + 30),
                                            font,
                                            2, (55, 255, 30),
                                            thickness=2,
                                            lineType=1)
                                        if 100 < diss < 900:
                                            if tems <= 37:
                                                fg = 0
                                                # print('----------------------------------main fg:',fg)
                                                create_dir_not_exist(
                                                    args.respath +
                                                    "/high_temperature")
                                            else:
                                                cv2.putText(frame2,
                                                            '{}'.format(tems),
                                                            (bb[i][0] + 5,
                                                             bb[i][1] + 30),
                                                            font,
                                                            2, (55, 30, 255),
                                                            thickness=2,
                                                            lineType=1)
                                                fg = 1
                                                high_tem_time = str(
                                                    time.strftime(
                                                        '%Y-%m-%d_%H-%M-%S'))
                                                cv2.imwrite(
                                                    args.respath +
                                                    "/high_temperature/" +
                                                    high_tem_time + ".jpg",
                                                    frame2)
                                                # print('----------------------------------main fg:',fg)
                                        else:
                                            fg = 404
                                else:
                                    pass  # fg = 404

                            else:
                                fg = 404
                                # print('----------------------------------main fg:', fg)

                        # 在检测框上标注是别人id及score
                        if best_class_probabilities > 0.3:
                            # print("{} : {}".format(person_id, str(best_class_probabilities[0])[0:4]))
                            # id_score = "{}:{}".format(person_id, str(best_class_probabilities[0])[0:4])
                            id_score = "%d:%.2f" % (
                                person_id, best_class_probabilities[0])
                            font = cv2.FONT_HERSHEY_COMPLEX_SMALL  # cv2.FONT_HERSHEY_SCRIPT_COMPLEX #cv2.FONT_HERSHEY_COMPLEX_SMALL,
                            cv2.putText(frame2,
                                        id_score, (bb[i][0], bb[i][1] - 3),
                                        font,
                                        0.8, (0, 0, 200),
                                        thickness=1,
                                        lineType=2)

                        # 进行人脸识别
                        if best_class_probabilities > args.knowperson_threshold:  # 0.90:
                            # cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0),
                            #               2)  # boxing face
                            knowpeople += 1
                            # print("Name_True_rec: {}, Probability: {}".format(best_name, best_class_probabilities))
                            text_xmin = bb[i][0]
                            text_y = bb[i][3] + 20
                            text_ymax = bb[i][3] + 30
                            text_ymin = bb[i][3]
                            text_xmax = bb[i][2]
                            cv2.rectangle(frame2, (bb[i][0], bb[i][1]),
                                          (bb[i][2], bb[i][3]), (0, 255, 0),
                                          2)  # boxing face
                            # cv2.rectangle(frame, (text_xmin, text_ymin), (text_xmax, text_ymax), (0, 255, 0),
                            #               cv2.FILLED)  # name box
                            font = cv2.FONT_HERSHEY_COMPLEX_SMALL  # cv2.FONT_HERSHEY_SCRIPT_COMPLEX #cv2.FONT_HERSHEY_COMPLEX_SMALL,
                            cv2.putText(frame2,
                                        class_names[best_class_indices[0]],
                                        (text_xmin, text_y),
                                        font,
                                        0.8, (14, 173, 238),
                                        thickness=1,
                                        lineType=2)
                            person_detected[best_name] += 1

                            # 设置签到门槛,确保不错误识别
                            if best_class_probabilities > 0.95:
                                signin(class_names[best_class_indices[0]])
                                if bb[i][0] > xmin - 80 and bb[i][
                                        2] < xmax + 80 and bb[i][
                                            1] > ymin - 80 and bb[i][
                                                3] < ymax + 80:
                                    fg2 = 2
                                    # print('----------------------------------main fg:', fg)
                                else:
                                    fg2 = 405
                                    # print('----------------------------------main fg:', fg)
                            else:
                                fg2 = 405
                                # print('----------------------------------main fg:', fg)

                        # 进行陌生人判断
                        elif best_class_probabilities < args.unknowperson_threshold:  # 0.6:
                            # cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0),
                            #               2)  # boxing face
                            # 这里对unknow进行干扰过滤,防止误触发
                            unknowpeople += 1  # 这行和下五行的可行性待验证!!!
                            if knowpeople == 20:
                                knowpeople = 0
                                unknowpeople = 0
                            # print('unknowpeople:',unknowpeople)
                            if unknowpeople % 1 == 0:
                                text_x = bb[i][0]
                                text_y = bb[i][3] + 20
                                cv2.rectangle(frame2, (bb[i][0], bb[i][1]),
                                              (bb[i][2], bb[i][3]),
                                              (0, 0, 255), 2)  # boxing face
                                cv2.putText(frame2,
                                            'Unknow', (text_x, text_y),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            0.8, (0, 0, 200),
                                            thickness=1,
                                            lineType=2)
                                # print("Name_unknow_rec: unknow, Probability: {}".format(best_class_probabilities))
                                unknowpeople = 0
                                # 陌生人脸帧留档
                                create_dir_not_exist(args.respath +
                                                     "/stranger")
                                create_dir_not_exist(args.respath +
                                                     "/stranger_with_box")
                                if args.record_unknow == True:
                                    for x in range(20):
                                        if not x % 10 == 0:
                                            continue
                                        if best_class_probabilities < args.stranger_threshold:  # 0.5:
                                            strange_in_time = str(
                                                time.strftime(
                                                    '%Y-%m-%d_%H-%M-%S'))
                                            # print(strange_in_time)
                                            cv2.imwrite(
                                                args.respath + "/stranger/" +
                                                strange_in_time + ".jpg",
                                                frame)
                                            cv2.imwrite(
                                                args.respath +
                                                "/stranger_with_box/" +
                                                strange_in_time + ".jpg",
                                                frame2)

                    for person, count in person_detected.items():
                        if count > 4:
                            # print("Person Detected: {}, Count: {}".format(person, count))
                            people_detected.add(person)
                else:
                    temwork = 0
                    fg2 = 405
                    fg3 = 406
                mainwindowname = "人脸验证及测温系统"
                frame2 = cv2ImgAddText(frame2, mainwindowname, 20, 20,
                                       (255, 255, 155), 20)

                # 加入美颜输出视频
                if args.beauty == True:
                    frame2 = beautiful(frame2)
                cv2.imshow("Face Detection and Identification", frame2)
                if args.record == True:
                    video_recording.write(frame2)
                if cv2.waitKey(1) & 0xFF == 27:
                    break

                # #当前线程数
                # xcs = len(threading.enumerate())
                # print("当前线程数:{}".format(xcs))
                # print("-----------------------------------------------检测识别")
    video_recording.release()
    # video_capture.release()
    cv2.destroyAllWindows()
def main(args):
    # filename = '/home/ydwu/tmp/gen_bin/cfp/Protocol/Pair_list_P.txt'
    filename = '/home/ydwu/tmp/gen_bin/cfp/Protocol/Pair_list_F.txt'

    num_list = []
    image_list = []

    with open(filename, 'r') as file_to_read:
        while True:
            lines = file_to_read.readline()  # 整行读取数据
            print(lines)
            if not lines:
                break
                pass

            l = lines.split()
            num_list.append(l[0])
            image_list.append(l[1])

    # fetch the classes (labels as strings) exactly as it's done in get_dataset
    path_exp = os.path.expanduser(args.data_dir)

    classes_name = []
    img_name = []
    for path in os.listdir(path_exp):
        # if os.path.isdir(path):
        for tpath in os.listdir(os.path.join(path_exp, path)):
            classes_name.append(path)
            img_name.append(tpath)

    sess_2 = tf.Session()
    my_head_pose_estimator = CnnHeadPoseEstimator(sess_2)  # Head pose estimation object

    # Load the weights from the configuration folders
    my_head_pose_estimator.load_roll_variables("roll/cnn_cccdd_30k.tf")
    my_head_pose_estimator.load_pitch_variables("pitch/cnn_cccdd_30k.tf")
    my_head_pose_estimator.load_yaw_variables("yaw/cnn_cccdd_30k.tf")


    with tf.Graph().as_default():

        # Start running operations on the Graph.
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))


        # with tf.Session() as sess:
        with sess.as_default():

            # Load the model
            facenet.load_model(args.model_dir)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
            nrof_images = len(image_list)
            print('Number of images: ', nrof_images)
            batch_size = args.image_batch
            if nrof_images % batch_size == 0:
                nrof_batches = nrof_images // batch_size
            else:
                nrof_batches = (nrof_images // batch_size) + 1
            print('Number of batches: ', nrof_batches)
            embedding_size = embeddings.get_shape()[1]
            emb_array = np.zeros((nrof_images, embedding_size))
            pose_array = np.zeros((nrof_images, 1))
            start_time = time.time()

            for i in range(nrof_batches):
                if i == nrof_batches -1:
                    n = nrof_images
                else:
                    n = i*batch_size + batch_size

                # Get images for the batch
                # images = facenet.load_data(image_list[i*batch_size:n], False, False, args.image_size)
                images, pose = facenet.ydwu_load_data_v2(image_list[i * batch_size:n], False, False, args.image_size, my_head_pose_estimator)
                # print("pose = ", pose)
                pose_array[i * batch_size:n, :] = pose

                feed_dict = { images_placeholder: images, phase_train_placeholder:False }

                # Use the facenet model to calcualte embeddings
                embed = sess.run(embeddings, feed_dict=feed_dict)
                # print("embed = ", embed)
                emb_array[i*batch_size:n, :] = embed
                print('Completed batch', i+1, 'of', nrof_batches)


    with tf.Graph().as_default():

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

        with sess.as_default():
            stiched_model_saver = tf.train.import_meta_graph('/home/ydwu/tmp/gen_bin/dream_module/model.ckpt.meta')
            stiched_model_saver.restore(sess, '/home/ydwu/tmp/gen_bin/dream_module/model.ckpt-896000')
            stitch_emb = tf.get_default_graph().get_tensor_by_name('dream/input:0')
            stitch_yaw = tf.get_default_graph().get_tensor_by_name('dream/yaw:0')
            stitch_output = tf.get_default_graph().get_tensor_by_name('dream/output:0')


            # yaw_degree = 50
            yaw = BaseDataset.norm_angle(pose_array)
            mapped_profile_emb = sess.run(stitch_output,
                                          feed_dict={stitch_emb: emb_array,
                                                     stitch_yaw: np.reshape(yaw, newshape=(-1, 1))})


            run_time = time.time() - start_time
            print('Run time: ', run_time)
            feat_dim = 128
            data_num = mapped_profile_emb.shape[0]

            feat_file = '/home/ydwu/tmp/gen_bin/' + args.bin_name
            with open(feat_file, 'wb') as bin_f:
                bin_f.write(st.pack('ii', data_num, feat_dim))
                for j in range(data_num):
                    bin_f.write(st.pack('f' * feat_dim, *tuple(mapped_profile_emb[j, :])))
def main(args):
  
    with tf.Graph().as_default():
      
        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(os.path.expanduser(args.model_dir))
            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            
            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]

            image_size = image_size.value
            # pdb.set_trace()


            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0*nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))

            print('nrof_batches :{}'.format(nrof_batches))

            all_time = 0

            for i in range(nrof_batches):
                start_index = i*batch_size
                end_index = min((i+1)*batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                # pdb.set_trace()
                images = facenet.load_data(paths_batch, False, False, image_size)
                feed_dict = {images_placeholder:images}
                start = time()
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
                end = time()
                all_time += (end - start)
                print('index: {}  time: {}'.format(i, (end-start)))
                # pdb.set_trace()
            print('all_time :', all_time)

            msgpack_numpy.dump((paths, emb_array, actual_issame), open('lfw_feature.p', 'wb'))

            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(emb_array, 
                args.seed, actual_issame, nrof_folds=args.lfw_nrof_folds)


            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            np.random.seed(seed=args.seed)

            if args.use_split_dataset:
                dataset_tmp = facenet.get_dataset(args.data_dir)
                train_set, test_set = split_dataset(dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class)
                if (args.mode == 'TRAIN'):
                    dataset = train_set
                elif (args.mode == 'CLASSIFY'):
                    dataset = test_set
            else:
                dataset = facenet.get_dataset(args.data_dir)

            # Check that there are at least one training image per class
            for cls in dataset:
                assert(len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset')

            paths, labels = facenet.get_image_paths_and_labels(dataset)

            print('Number of classes: %d' % len(dataset))
            print('Number of images: %d' % len(paths))

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / args.batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                start_index = i * args.batch_size
                end_index = min((i + 1) * args.batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False, args.image_size)
                feed_dict = {images_placeholder: images, phase_train_placeholder: False}
                emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict)

            classifier_filename_exp = os.path.expanduser(args.classifier_filename)

            if (args.mode == 'TRAIN'):
                # Train classifier
                print('Training classifier')
                model = SVC(kernel='linear', probability=True)
                model.fit(emb_array, labels)

                # Create a list of class names
                class_names = [cls.name.replace('_', ' ') for cls in dataset]

                # Saving classifier model
                with open(classifier_filename_exp, 'wb') as outfile:
                    pickle.dump((model, class_names), outfile)
                print('Saved classifier model to file "%s"' % classifier_filename_exp)

            elif (args.mode == 'CLASSIFY'):
                # Classify images
                print('Testing classifier')
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                print('Loaded classifier model from file "%s"' % classifier_filename_exp)

                predictions = model.predict_proba(emb_array)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]

                for i in range(len(best_class_indices)):
                    print('%4d  %s: %.3f' % (i, class_names[best_class_indices[i]], best_class_probabilities[i]))

                accuracy = np.mean(np.equal(best_class_indices, labels))
                print('Accuracy: %.3f' % accuracy)
Example #28
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(
                os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

            # Load the model
            print('Model directory: %s' % args.model_dir)
            meta_file, ckpt_file = facenet.get_model_filenames(
                os.path.expanduser(args.model_dir))

            print('Metagraph file: %s' % meta_file)
            print('Checkpoint file: %s' % ckpt_file)
            facenet.load_model(args.model_dir, meta_file, ckpt_file)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")

            image_size = images_placeholder.get_shape()[1]
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches):
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(
                emb_array, actual_issame, nrof_folds=args.lfw_nrof_folds)

            print('Accuracy: %1.3f+-%1.3f' %
                  (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' %
                  (val, val_std, far))

            auc = metrics.auc(fpr, tpr)
            print('AUC=: %1.3f' % auc)
            eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x),
                         0., 1.)
            print('EER=: %1.3f' % eer)

            # Plot ROC curve
            facenet.plot_roc(fpr, tpr, 'NN4')
def main(args):
    
    pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
    paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
    result_dir = '../data/'
    plt.ioff()  # Disable interactive plotting mode
    
    with tf.Graph().as_default():

        with tf.Session() as sess:
    
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            image_size = int(images_placeholder.get_shape()[1])
            
            # Run test on LFW to check accuracy for different horizontal/vertical translations of input images
            if args.nrof_offsets>0:
                step = 3
                offsets = np.asarray([x*step for x in range(-args.nrof_offsets//2+1, args.nrof_offsets//2+1)])
                horizontal_offset_accuracy = [None] * len(offsets)
                for idx, offset in enumerate(offsets):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, 
                        paths, actual_issame, translate_images, (offset,0), 60, args.orig_image_size, args.seed)
                    print('Hoffset: %1.3f  Accuracy: %1.3f+-%1.3f' % (offset, np.mean(accuracy), np.std(accuracy)))
                    horizontal_offset_accuracy[idx] = np.mean(accuracy)
                vertical_offset_accuracy = [None] * len(offsets)
                for idx, offset in enumerate(offsets):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, 
                        paths, actual_issame, translate_images, (0,offset), 60, args.orig_image_size, args.seed)
                    print('Voffset: %1.3f  Accuracy: %1.3f+-%1.3f' % (offset, np.mean(accuracy), np.std(accuracy)))
                    vertical_offset_accuracy[idx] = np.mean(accuracy)
                fig = plt.figure(1)
                plt.plot(offsets, horizontal_offset_accuracy, label='Horizontal')
                plt.plot(offsets, vertical_offset_accuracy, label='Vertical')
                plt.legend()
                plt.grid(True)
                plt.title('Translation invariance on LFW')
                plt.xlabel('Offset [pixels]')
                plt.ylabel('Accuracy')
#                plt.show()
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_translation.png'))
                save_result(offsets, horizontal_offset_accuracy, os.path.join(result_dir, 'invariance_translation_horizontal.txt'))
                save_result(offsets, vertical_offset_accuracy, os.path.join(result_dir, 'invariance_translation_vertical.txt'))

            # Run test on LFW to check accuracy for different rotation of input images
            if args.nrof_angles>0:
                step = 3
                angles = np.asarray([x*step for x in range(-args.nrof_offsets//2+1, args.nrof_offsets//2+1)])
                rotation_accuracy = [None] * len(angles)
                for idx, angle in enumerate(angles):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, 
                        paths, actual_issame, rotate_images, angle, 60, args.orig_image_size, args.seed)
                    print('Angle: %1.3f  Accuracy: %1.3f+-%1.3f' % (angle, np.mean(accuracy), np.std(accuracy)))
                    rotation_accuracy[idx] = np.mean(accuracy)
                fig = plt.figure(2)
                plt.plot(angles, rotation_accuracy)
                plt.grid(True)
                plt.title('Rotation invariance on LFW')
                plt.xlabel('Angle [deg]')
                plt.ylabel('Accuracy')
#                plt.show()
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_rotation.png'))
                save_result(angles, rotation_accuracy, os.path.join(result_dir, 'invariance_rotation.txt'))

            # Run test on LFW to check accuracy for different scaling of input images
            if args.nrof_scales>0:
                step = 0.05
                scales = np.asarray([x*step+1 for x in range(-args.nrof_offsets//2+1, args.nrof_offsets//2+1)])
                scale_accuracy = [None] * len(scales)
                for scale_idx, scale in enumerate(scales):
                    accuracy = evaluate_accuracy(sess, images_placeholder, phase_train_placeholder, image_size, embeddings, 
                        paths, actual_issame, scale_images, scale, 60, args.orig_image_size, args.seed)
                    print('Scale: %1.3f  Accuracy: %1.3f+-%1.3f' % (scale, np.mean(accuracy), np.std(accuracy)))
                    scale_accuracy[scale_idx] = np.mean(accuracy)
                fig = plt.figure(3)
                plt.plot(scales, scale_accuracy)
                plt.grid(True)
                plt.title('Scale invariance on LFW')
                plt.xlabel('Scale')
                plt.ylabel('Accuracy')
#                plt.show()
                print('Saving results in %s' % result_dir)
                fig.savefig(os.path.join(result_dir, 'invariance_scale.png'))
                save_result(scales, scale_accuracy, os.path.join(result_dir, 'invariance_scale.txt'))
def _main():

    args = get_args()
    

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            # pnet, rnet, onet = detect_face.create_mtcnn(sess, './models/')

            minsize = 20  # minimum size of face
            threshold = [0.6, 0.7, 0.7]  # three steps's threshold
            factor = 0.709  # scale factor
            margin = 44
            frame_interval = 3
            batch_size = 1000
            image_size = 182
            input_image_size = 160

            print('Loading feature extraction model')
            modeldir = './models/facenet/20190310-055158'
            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            classifier_filename = './myclassifier/my_classifier.pkl'
            classifier_filename_exp = os.path.expanduser(classifier_filename)
            with open(classifier_filename_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile)
                print('load classifier file-> %s' % classifier_filename_exp)

            video_capture = cv2.VideoCapture(0)
            c = 0

            print('Start Recognition!')
            prevTime = 0
            myYolo = YOLO(args)
            while True:
                ret, frame = video_capture.read()

                # frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)    #resize frame (optional)

                curTime = time.time()    # calc fps
                timeF = frame_interval

                if (c % timeF == 0):
                    find_results = []

                    if frame.ndim == 2:
                        frame = facenet.to_rgb(frame)
                    frame = frame[:, :, 0:3]
                    #print(frame.shape[0])
                    #print(frame.shape[1])
                    
                    image = Image.fromarray(frame)
                    img, bounding_boxes = myYolo.detect_image(image)

                    # Remove the bounding boxes with low confidence
                    nrof_faces = len(bounding_boxes)
                    ## Use MTCNN to get the bounding boxes
                    # bounding_boxes, _ = detect_face.detect_face(frame, minsize, pnet, rnet, onet, threshold, factor)
                    # nrof_faces = bounding_boxes.shape[0]
                    #print('Detected_FaceNum: %d' % nrof_faces)

                    if nrof_faces > 0:
                        # det = bounding_boxes[:, 0:4]
                        img_size = np.asarray(frame.shape)[0:2]

                        # cropped = []
                        # scaled = []
                        # scaled_reshape = []
                        bb = np.zeros((nrof_faces,4), dtype=np.int32)

                        for i in range(nrof_faces):
                            emb_array = np.zeros((1, embedding_size))

                            bb[i][0] = bounding_boxes[i][0]
                            bb[i][1] = bounding_boxes[i][1]
                            bb[i][2] = bounding_boxes[i][2]
                            bb[i][3] = bounding_boxes[i][3]

                            if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][2] >= len(frame[0]) or bb[i][3] >= len(frame):
                                print('face is inner of range!')
                                continue

                            # cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                            # cropped[0] = facenet.flip(cropped[0], False)
                            # scaled.append(misc.imresize(cropped[0], (image_size, image_size), interp='bilinear'))
                            # scaled[0] = cv2.resize(scaled[0], (input_image_size,input_image_size),
                            #                        interpolation=cv2.INTER_CUBIC)
                            # scaled[0] = facenet.prewhiten(scaled[0])
                            # scaled_reshape.append(scaled[0].reshape(-1,input_image_size,input_image_size,3))
                            # feed_dict = {images_placeholder: scaled_reshape[0], phase_train_placeholder: False}

                            cropped = (frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                            print("{0} {1} {2} {3}".format(bb[i][0], bb[i][1], bb[i][2], bb[i][3]))
                            cropped = facenet.flip(cropped, False)
                            scaled = (misc.imresize(cropped, (image_size, image_size), interp='bilinear'))
                            scaled = cv2.resize(scaled, (input_image_size,input_image_size),
                                                interpolation=cv2.INTER_CUBIC)
                            scaled = facenet.prewhiten(scaled)
                            scaled_reshape = (scaled.reshape(-1,input_image_size,input_image_size,3))
                            feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False}

                            emb_array[0, :] = sess.run(embeddings, feed_dict=feed_dict)

                            predictions = model.predict_proba(emb_array)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                            print(best_class_probabilities)
                            cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                            text_x = bb[i][0]
                            text_y = bb[i][3] + 20

                            # for H_i in HumanNames:
                            #     if HumanNames[best_class_indices[0]] == H_i:
                            result_names = class_names[best_class_indices[0]] if best_class_probabilities[0] > 0.45 else "Unknown"
                            #print(result_names)
                            cv2.putText(frame, result_names, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (0, 0, 255), thickness=1, lineType=2)
                    else:
                        print('Unable to align')

                sec = curTime - prevTime
                prevTime = curTime
                fps = 1 / (sec)
                str = 'FPS: %2.3f' % fps
                text_fps_x = len(frame[0]) - 150
                text_fps_y = 20
                cv2.putText(frame, str, (text_fps_x, text_fps_y),
                            cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), thickness=1, lineType=2)
                # c+=1
                cv2.imshow('Video', frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            video_capture.release()
            # #video writer
            # out.release()
            cv2.destroyAllWindows()
def job2():
    print('start job2')
    mode = 'TRAIN'
    # data_dir = 'data_align_en'
    data_dir = './video_image_align_410'
    model = './models/20180408-102900'
    classifier_filename = './models/classifier_align_en_final_0404_1.pkl'
    use_split_dataset = False
    test_data_dir = './test'
    batch_size = 90
    image_size = 160
    seed = 666
    min_nrof_images_per_class = 20
    nrof_train_images_per_class = 10
    with tf.Graph().as_default():
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with tf.Session(config=config) as sess:

            np.random.seed(seed=seed)

            if use_split_dataset:
                dataset_tmp = facenet.get_dataset(data_dir)
                train_set, test_set = split_dataset(
                    dataset_tmp, min_nrof_images_per_class,
                    nrof_train_images_per_class)
                if (mode == 'TRAIN'):
                    dataset = train_set
                elif (mode == 'CLASSIFY'):
                    dataset = test_set
            else:
                dataset = facenet.get_dataset(data_dir)

            # Check that there are at least one training image per class
            for cls in dataset:
                assert (
                    len(cls.image_paths) > 0,
                    'There must be at least one image for each class in the dataset'
                )

            paths, labels = facenet.get_image_paths_and_labels(dataset)

            print('Number of classes: %d' % len(dataset))
            print('Number of images: %d' % len(paths))

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(
                math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches_per_epoch):
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            classifier_filename_exp = os.path.expanduser(classifier_filename)

            if (mode == 'TRAIN'):
                # Train classifier
                print('Training classifier')
                model = SVC(kernel='linear', probability=True)
                model.fit(emb_array, labels)

                # Create a list of class names
                class_names = [cls.name.replace('_', ' ') for cls in dataset]

                # Saving classifier model
                with open(classifier_filename_exp, 'wb') as outfile:
                    pickle.dump((model, class_names), outfile)
                print('Saved classifier model to file "%s"' %
                      classifier_filename_exp)

            elif (mode == 'CLASSIFY'):
                # Classify images
                print('Testing classifier')
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                print('Loaded classifier model from file "%s"' %
                      classifier_filename_exp)

                predictions = model.predict_proba(emb_array)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[
                    np.arange(len(best_class_indices)), best_class_indices]

                for i in range(len(best_class_indices)):
                    print('%4d  %s: %.3f' %
                          (i, class_names[best_class_indices[i]],
                           best_class_probabilities[i]))

                accuracy = np.mean(np.equal(best_class_indices, labels))
                print('Accuracy: %.3f' % accuracy)
Example #32
0
def identify_face(img_path):
    present_ids = []
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, npy)

            minsize = 20  # minimum size of face
            threshold = [0.6, 0.7, 0.7]  # three steps's threshold
            factor = 0.709  # scale factor
            margin = 44
            frame_interval = 3
            batch_size = 1000
            image_size = 182
            input_image_size = 160

            HumanNames = os.listdir(train_img)
            HumanNames.sort()

            print('Loading feature extraction model')
            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            classifier_filename_exp = os.path.expanduser(classifier_filename)
            with open(classifier_filename_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile)

            # video_capture = cv2.VideoCapture("akshay_mov.mp4")
            c = 0

            print('Start Recognition!')
            prevTime = 0
            # ret, frame = video_capture.read()
            frame = cv2.imread(img_path)

            frame = cv2.resize(frame, (0, 0), fx=0.5,
                               fy=0.5)  #resize frame (optional)

            curTime = time.time() + 1  # calc fps
            timeF = frame_interval

            if (c % timeF == 0):
                find_results = []

                if frame.ndim == 2:
                    frame = facenet.to_rgb(frame)
                frame = frame[:, :, 0:3]
                bounding_boxes, _ = detect_face.detect_face(
                    frame, minsize, pnet, rnet, onet, threshold, factor)
                nrof_faces = bounding_boxes.shape[0]
                print('Face Detected: %d' % nrof_faces)

                if nrof_faces > 0:
                    det = bounding_boxes[:, 0:4]
                    img_size = np.asarray(frame.shape)[0:2]

                    cropped = []
                    scaled = []
                    scaled_reshape = []
                    bb = np.zeros((nrof_faces, 4), dtype=np.int32)

                    for i in range(nrof_faces):
                        emb_array = np.zeros((1, embedding_size))

                        bb[i][0] = det[i][0]
                        bb[i][1] = det[i][1]
                        bb[i][2] = det[i][2]
                        bb[i][3] = det[i][3]

                        # inner exception
                        if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][2] >= len(
                                frame[0]) or bb[i][3] >= len(frame):
                            print('face is too close')
                            continue

                        cropped.append(frame[bb[i][1]:bb[i][3],
                                             bb[i][0]:bb[i][2], :])
                        cropped[i] = facenet.flip(cropped[i], False)
                        scaled.append(
                            misc.imresize(cropped[i], (image_size, image_size),
                                          interp='bilinear'))
                        scaled[i] = cv2.resize(
                            scaled[i], (input_image_size, input_image_size),
                            interpolation=cv2.INTER_CUBIC)
                        scaled[i] = facenet.prewhiten(scaled[i])
                        scaled_reshape.append(scaled[i].reshape(
                            -1, input_image_size, input_image_size, 3))
                        feed_dict = {
                            images_placeholder: scaled_reshape[i],
                            phase_train_placeholder: False
                        }
                        emb_array[0, :] = sess.run(embeddings,
                                                   feed_dict=feed_dict)
                        predictions = model.predict_proba(emb_array)
                        print(predictions)
                        best_class_indices = np.argmax(predictions, axis=1)
                        # print(best_class_indices)
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)),
                            best_class_indices]
                        print(best_class_probabilities)

                        cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                      (bb[i][2], bb[i][3]), (0, 255, 0),
                                      2)  #boxing face

                        #plot result idx under box
                        text_x = bb[i][0]
                        text_y = bb[i][3] + 20
                        print('Result Indices: ', best_class_indices[0])
                        present_ids.append(best_class_indices[0])
                        print(HumanNames)
                        for H_i in HumanNames:
                            # print(H_i)
                            if HumanNames[best_class_indices[0]] == H_i:
                                result_names = HumanNames[
                                    best_class_indices[0]]
                                if best_class_probabilities[0] < 0.4:
                                    result_names = 'Unknown'
                                cv2.putText(frame,
                                            result_names, (text_x, text_y),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            0.3, (0, 0, 255),
                                            thickness=1,
                                            lineType=2)
                                cv2.imwrite('./output/full-class.jpg', frame)
                else:
                    print('Unable to align')
            '''
            cv2.imshow('Image', frame)

            if cv2.waitKey(1000000) & 0xFF == ord('q'):
                sys.exit("Thanks")
            cv2.destroyAllWindows()    
            '''
    #print(present_ids)
    return present_ids
Example #33
0
def main(args):

    with tf.Graph().as_default():

        with tf.Session() as sess:

            np.random.seed(seed=args.seed)

            for sub_dir in [x[0] for x in os.walk(args.data_dir)]:
                for file in os.listdir(sub_dir):
                    if not file.startswith(
                            "cropped_") and not sub_dir == args.data_dir:
                        print("removing ", os.path.join(str(sub_dir), file))
                        os.remove(os.path.join(str(sub_dir), file))

            if args.use_split_dataset:
                dataset_tmp = facenet.get_dataset(args.data_dir)
                train_set, test_set = split_dataset(
                    dataset_tmp, args.min_nrof_images_per_class,
                    args.nrof_train_images_per_class)
                if (args.mode == 'TRAIN'):
                    dataset = train_set
                elif (args.mode == 'CLASSIFY'):
                    dataset = test_set
                elif (args.mode == 'CLASSIFY_IMAGE'):
                    dataset = train_set
            else:
                dataset = facenet.get_dataset(args.data_dir)

            label_names = []

            for img_class in dataset:
                label_names.append(img_class.name)
            # Check that there are at least one training image per class
            for cls in dataset:
                assert (
                    len(cls.image_paths) > 0,
                    'There must be at least one image for each class in the dataset'
                )

            paths, labels = facenet.get_image_paths_and_labels(dataset)

            print('Number of classes: %d' % len(dataset))
            print('Number of images: %d' % len(paths))

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Calculating features for images')
            nrof_images = len(paths)
            nrof_batches_per_epoch = int(
                math.ceil(1.0 * nrof_images / args.batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            print("Total number of batches: ", nrof_batches_per_epoch)
            #            for i in range(nrof_batches_per_epoch):
            #                start_index = i*args.batch_size
            #                end_index = min((i+1)*args.batch_size, nrof_images)
            #                paths_batch = paths[start_index:end_index]
            #                images = facenet.load_data(paths_batch, False, False, args.image_size)
            #                feed_dict = { images_placeholder:images, phase_train_placeholder:False }
            #                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
            #                print("Done with batch", i)

            classifier_filename_exp = os.path.expanduser(
                args.classifier_filename)

            if (args.mode == 'TRAIN'):
                # Train classifier
                print('Training classifier')
                #model = SVC(kernel='linear', probability=True)
                model = SVC(C=1, kernel='rbf', probability=True, gamma=2)
                #                with open("training_data.pkl",'wb') as train_file:
                #                    pickle.dump([emb_array,labels],train_file)

                with open("training_data.pkl", 'rb') as train_file:
                    [emb_array, labels] = pickle.load(train_file)
                model.fit(emb_array, labels)

                # Create a list of class names
                class_names = [cls.name.replace('_', ' ') for cls in dataset]

                # Saving classifier model
                with open(classifier_filename_exp, 'wb') as outfile:
                    pickle.dump((model, class_names), outfile)
                print('Saved classifier model to file "%s"' %
                      classifier_filename_exp)

            elif (args.mode == 'CLASSIFY'):
                # Classify images
                print('Testing classifier')
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                print('Loaded classifier model from file "%s"' %
                      classifier_filename_exp)

                predictions = model.predict_proba(emb_array)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[
                    np.arange(len(best_class_indices)), best_class_indices]

                for i in range(len(best_class_indices)):
                    print('%4d  %s: %.3f' %
                          (i, class_names[best_class_indices[i]],
                           best_class_probabilities[i]))

                accuracy = np.mean(np.equal(best_class_indices, labels))

                results = np.equal(best_class_indices, labels)
                for i in range(20):
                    print(
                        100 - i * 5, "percentile in failed classes",
                        np.percentile(best_class_probabilities[results == 0],
                                      100 - i * 5))
                    print(
                        i * 5, "percentile in successful classes",
                        np.percentile(best_class_probabilities[results == 1],
                                      i * 5))
                print('Accuracy: %.3f' % accuracy)

            elif (args.mode == 'CLASSIFY_IMAGE'):
                with tf.Graph().as_default():

                    with tf.Session() as sess:
                        # Load the model
                        print('Loading feature extraction model')
                        facenet.load_model(args.model)
                        for i in range(15):
                            print(
                                classify_image_by_URL(
                                    input("Enter URL of image to classify\n"),
                                    args, sess))
Example #34
0
def main(args):

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) as sess:

            # Read the file containing the pairs used for testing
            if args.lfw_pairs == None:
                paths, actual_issame = crossFire(getImages(os.path.expanduser(args.lfw_dir)))
                print("Crossfire check:", len(actual_issame), "pairs to be checked.")
            else:
                pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

                # Get the paths for the corresponding images
                paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs)
                print("Pair file check:", len(actual_issame), "pairs to be checked.")

            # Load the model
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            #image_size = images_placeholder.get_shape()[1]  # For some reason this doesn't work for frozen graphs
            image_size = args.image_size
            embedding_size = embeddings.get_shape()[1]

            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)
            nrof_batches = int(math.ceil(1.0*nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))
            for i in range(nrof_batches):
                print('Compute batch:', i, '/', nrof_batches, end='\r')
                start_index = i*batch_size
                end_index = min((i+1)*batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data2(paths_batch, False, False, image_size)
                feed_dict = { images_placeholder:images, phase_train_placeholder:False }
                emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)
            print()
            print('--- Evaluate ---')

            tpr, fpr, accuracy, bestThreshold, minThreshold, maxThreshold, val, val_std, far, thresholdAtVal = lfw.evaluate(emb_array,
                actual_issame, nrof_folds=args.lfw_nrof_folds)

            print()
            print('--- Verify using Euclidian distance ---')
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            #print('Accuracy array:', accuracy)
            print('Best threshold: %1.3f' % bestThreshold)
            print('Minimal threshold of hit rate @ 100%%: %1.3f' % minThreshold)
            print('Maximum threshold of false alarm rate @ 0%%: %1.3f' % maxThreshold)
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            print('Threshold @ %1.3f' % thresholdAtVal)
            auc = metrics.auc(fpr, tpr)
            # plt.clf()
            # plt.plot(fpr, tpr, '.')
            # plt.savefig('euclidian_distance_auc.jpg')
            print('Area Under Curve (AUC): %1.3f' % auc)
            eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.)
            print('Equal Error Rate (EER): %1.3f' % eer)

            tpr, fpr, accuracy, bestThreshold, minThreshold, maxThreshold, val, val_std, far, thresholdAtVal = lfw.evaluate(emb_array,
                actual_issame, nrof_folds=args.lfw_nrof_folds, distance_metric=1)

            print()
            print("--- Verify using Cosine Similarity ---")
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            #print('Accuracy array:', accuracy)
            print('Best threshold: %1.3f' % bestThreshold)
            print('Minimal threshold of hit rate @ 100%%: %1.3f' % minThreshold)
            print('Maximum threshold of false alarm rate @ 0%%: %1.3f' % maxThreshold)
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            print('Threshold @ %1.3f' % thresholdAtVal)
            auc = metrics.auc(fpr, tpr)
            # plt.clf()
            # plt.plot(fpr, tpr, '.')
            # plt.savefig('cosine_similarity_auc.jpg')
            print('Area Under Curve (AUC): %1.3f' % auc)
            eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.)
            print('Equal Error Rate (EER): %1.3f' % eer)
Example #35
0
import matplotlib.pyplot as plt

# def realtime_recognition():
minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
image_size = 160

with tf.Graph().as_default():
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            log_device_placement=False))
    with sess.as_default():
        pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
        print('Loading feature extraction model')
        facenet.load_model('/home/wjc/Documents/code/face_test/models')

        images_placeholder = tf.get_default_graph().get_tensor_by_name(
            "input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name(
            "phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        classifier_filename_exp = os.path.expanduser('new_models.pkl')
        with open(classifier_filename_exp, 'rb') as infile:
            (model, class_names) = pickle.load(infile)
            print(class_names)

        print('Loaded classifier model from file "%s"' %
              classifier_filename_exp)
Example #36
0
 def __init__(self):
     self.sess = tf.Session()
     with self.sess.as_default():
         facenet.load_model(facenet_model_checkpoint)
def main():
    model = "20190128-123456/3001w-train.pb"
    pairs = lfw.read_pairs('../database/npairs.txt')
    paths, actual_issame = lfw.get_paths('F:/aface-clean-rename2', pairs)
    true_pairs = []
    false_pairs = []
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            # Load the model
            facenet.load_model(model)
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            for i in range(len(pairs)):
                print(paths[2 * i])
                img1 = cv2.imread(paths[2 * i])
                img2 = cv2.imread(paths[2 * i + 1])
                frame1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
                frame2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
                f1 = []
                f2 = []
                prewhitened1 = facenet.prewhiten(frame1)
                f1.append(prewhitened1)
                prewhitened2 = facenet.prewhiten(frame2)
                f2.append(prewhitened2)
                feed_dict = {
                    images_placeholder: f1,
                    phase_train_placeholder: False
                }
                emb1 = sess.run(embeddings, feed_dict=feed_dict)
                feed_dict = {
                    images_placeholder: f2,
                    phase_train_placeholder: False
                }
                emb2 = sess.run(embeddings, feed_dict=feed_dict)
                sim = dot(emb1, emb2.T)
                sim = sim * 0.5 + 0.5
                if actual_issame[i] == True:
                    true_pairs.append(sim)
                else:
                    false_pairs.append(sim)
    thresholds = np.arange(0, 1, 0.01)
    total_ratelist = []
    total_rightnum = 0
    total_rightnum2 = 0
    f = open('../database/restlt.txt', 'w')
    f.truncate()
    for threshold in thresholds:
        # print(threshold)
        true_num = 0
        false_num = 0
        for i in range(len(true_pairs)):
            if float(true_pairs[i]) > threshold:
                true_num = true_num + 1
        for j in range(len(false_pairs)):
            if float(false_pairs[j]) < threshold:
                false_num = false_num + 1
        if (true_num + false_num) >= total_rightnum2:
            total_rightnum2 = true_num + false_num
            print(total_rightnum2)
            print(threshold, ':',
                  format(float(total_rightnum2) / float(len(pairs)), '.4f'))
        total_rightnum = true_num + false_num
        f.writelines([
            str(threshold), ' ',
            str(format(float(total_rightnum) / float(len(pairs)), '.4f')), '\n'
        ])
    f.close()
Example #38
0
def main(args):
    train_set = facenet.get_dataset(args.data_dir)
    image_list, label_list = facenet.get_image_paths_and_labels(train_set)
    # fetch the classes (labels as strings) exactly as it's done in get_dataset
    path_exp = os.path.expanduser(args.data_dir)
    classes = [path for path in os.listdir(path_exp) \
               if os.path.isdir(os.path.join(path_exp, path))]
    classes.sort()
    # get the label strings
    label_strings = [name for name in classes if \
       os.path.isdir(os.path.join(path_exp, name))]

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Load the model
            facenet.load_model(args.model_dir)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
            nrof_images = len(image_list)
            print('Number of images: ', nrof_images)
            batch_size = args.image_batch
            if nrof_images % batch_size == 0:
                nrof_batches = nrof_images // batch_size
            else:
                nrof_batches = (nrof_images // batch_size) + 1
            print('Number of batches: ', nrof_batches)
            embedding_size = embeddings.get_shape()[1]
            emb_array = np.zeros((nrof_images, embedding_size))
            start_time = time.time()

            for i in range(nrof_batches):
                if i == nrof_batches - 1:
                    n = nrof_images
                else:
                    n = i * batch_size + batch_size
                # Get images for the batch
                if args.is_aligned is True:
                    images = facenet.load_data(image_list[i * batch_size:n],
                                               False, False, args.image_size)
                else:
                    images = load_and_align_data(image_list[i * batch_size:n],
                                                 args.image_size, args.margin,
                                                 args.gpu_memory_fraction)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                # Use the facenet model to calcualte embeddings
                embed = sess.run(embeddings, feed_dict=feed_dict)
                emb_array[i * batch_size:n, :] = embed
                print('Completed batch', i + 1, 'of', nrof_batches)

            run_time = time.time() - start_time
            print('Run time: ', run_time)

            #   export emedings and labels
            label_list = np.array(label_list)

            np.save(args.embeddings_name, emb_array)
            np.save(args.labels_name, label_list)
            label_strings = np.array(label_strings)
            np.save(args.labels_strings_name, label_strings[label_list])
        pnet, rnet, onet = align.detect_face.create_mtcnn(sess, npy)

        minsize = 20  # minimum size of face
        threshold = [0.6, 0.7, 0.7]  # three steps's threshold
        factor = 0.709  # scale factor
        margin = 44
        frame_interval = 3
        batch_size = 1000
        image_size = 183
        input_image_size = 160

        HumanNames = os.listdir(train_img)
        HumanNames.sort()

        print('Loading Modal')
        facenet.load_model(modeldir)
        images_placeholder = tf.get_default_graph().get_tensor_by_name(
            "input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name(
            "phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        classifier_filename_exp = os.path.expanduser(classifier_filename)
        with open(classifier_filename_exp, 'rb') as infile:
            (model, class_names) = pickle.load(infile)

        video_capture = cv2.VideoCapture(1)
        c = 0

        print('Start Recognition')
Example #40
0
def face_detection(image_path, thresh_hold):
    dist = []
    name_tmp = []
    Emb_data = []
    image_tmp = []
    minsize = 20
    with tf.Graph().as_default():
        with tf.Session() as sess:
            #print('开始加载模型')
            # Load the model
            model_checkpoint_path = 'model_check_point/20180408-102900'
            facenet.load_model(model_checkpoint_path)

            #print('建立facenet embedding模型')
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            #print('模型建立完毕!')

            #print('载入人脸库>>>>>>>>')
            #start_time = time.time()
            for items in os.listdir(pic_store):
                emb_data1 = []
                name_tmp.append(items)
                images_tmp, count, pnet, rnet, onet = load_and_align_data(
                    items, 160, 44, 1.0)
                for i in range(9):
                    emb_data = sess.run(embeddings,
                                        feed_dict={
                                            images_placeholder: images_tmp,
                                            phase_train_placeholder: False
                                        })
                    emb_data = emb_data.sum(axis=0)
                    emb_data1.append(emb_data)
                emb_data1 = np.array(emb_data1)
                emb_data = emb_data1.sum(axis=0)
                Emb_data.append(np.true_divide(emb_data, 9 * count))
                #print(len(Emb_data))
            #print('-'*50)
            #nrof_images = len(name_tmp)

            for filename in os.listdir(image_path):
                frame = cv2.imread(image_path + filename)
                print('image {}'.format(image_path + filename))

                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                if gray.ndim == 2:
                    img = to_rgb(gray)

                bounding_boxes, _ = detect_face.detect_face(
                    img, minsize, pnet, rnet, onet, threshold, factor)

                if len(bounding_boxes) < 1:
                    #print('nobody appears :0')
                    return
                else:
                    img_size = np.asarray(frame.shape)[0:2]
                    #nrof_faces = bounding_boxes.shape[0]#一张图中的人脸数目
                    #print('找到人脸数目为:{}'.format(nrof_faces))

                    dist_matrix = np.zeros((1, 2))
                    #print(dist_matrix.shape)

                    for item, face_position in enumerate(bounding_boxes):
                        #print('*'*50)
                        face_position = face_position.astype(int)  # 人脸的位置
                        #print((int(face_position[0]), int( face_position[1])))
                        det = np.squeeze(bounding_boxes[item, 0:4])
                        bb = np.zeros(4, dtype=np.int32)
                        bb[0] = np.maximum(det[0] - 44 / 2, 0)
                        bb[1] = np.maximum(det[1] - 44 / 2, 0)
                        bb[2] = np.minimum(det[2] + 44 / 2, img_size[1])
                        bb[3] = np.minimum(det[3] + 44 / 2, img_size[0])
                        cropped = frame[bb[1]:bb[3], bb[0]:bb[2], :]

                        aligned = misc.imresize(cropped,
                                                (image_size, image_size),
                                                interp='bilinear')  #裁剪人脸在图像中
                        prewhitened = facenet.prewhiten(
                            aligned)  #利用facenet进行矩阵对比
                        image_tmp.append(prewhitened)

                        image = np.stack(image_tmp)

                        emb_data = sess.run(embeddings,
                                            feed_dict={
                                                images_placeholder: image,
                                                phase_train_placeholder: False
                                            })

                        image_tmp.pop()

                        for i in range(len(Emb_data)):
                            dist.append(
                                np.sqrt(
                                    np.sum(
                                        np.square(
                                            np.subtract(
                                                emb_data[len(emb_data) - 1, :],
                                                Emb_data[i])))))

                        dist_np = np.array(dist)
                        dist_np = dist_np.reshape((1, len(Emb_data)))
                        #print(dist_np)
                        dist_matrix = np.concatenate((dist_matrix, dist_np),
                                                     axis=0)
                        dist = []

                    dist_matrix = np.delete(dist_matrix, 0, axis=0)

                    min_index = np.argmin(dist_matrix,
                                          axis=0).tolist()  # [2,2]
                    min_arg = np.min(
                        dist_matrix,
                        axis=0).tolist()  # [0.66275054 0.91318572]
                    #start_time = time.time()
                    if min(min_arg) > thresh_hold:
                        print('i dont know this man')
                    elif max(min_arg) < thresh_hold:
                        if min_index[0] == min_index[1]:
                            a = min_arg.index(min(min_arg))
                            print('he is {}'.format(a + 1))
                        else:
                            print('they are 1 and 2')
                    else:
                        b = min_arg.index(min(min_arg))
                        print('he is {}'.format(b + 1))
                #print('time is {}'.format(time.time()-start_time))
                print('*' * 50)
from sklearn.svm import SVC


with tf.Graph().as_default():

    with tf.Session() as sess:

        datadir = './output/'
        dataset = facenet.get_dataset(datadir)
        paths, labels = facenet.get_image_paths_and_labels(dataset)
        print('Number of classes: %d' % len(dataset))
        print('Number of images: %d' % len(paths))

        print('Loading feature extraction model')
        modeldir = './models/facenet/20190310-055158'
        facenet.load_model(modeldir)

        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        # Run forward pass to calculate embeddings
        print('Calculating features for images')
        batch_size = 1000
        image_size = 160
        nrof_images = len(paths)
        nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / batch_size))
        emb_array = np.zeros((nrof_images, embedding_size))
        for i in range(nrof_batches_per_epoch):
            start_index = i * batch_size
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path',
                        help='Path of the video you want to test on.',
                        default=0)
    args = parser.parse_args()

    MINSIZE = 20
    THRESHOLD = [0.6, 0.7, 0.7]
    FACTOR = 0.709
    IMAGE_SIZE = 182
    INPUT_IMAGE_SIZE = 160
    CLASSIFIER_PATH = 'Models/facemodel.pkl'
    VIDEO_PATH = args.path
    FACENET_MODEL_PATH = 'Models/20180402-114759.pb'

    # Load The Custom Classifier
    with open(CLASSIFIER_PATH, 'rb') as file:
        model, class_names = pickle.load(file)
    print("Custom Classifier, Successfully loaded")

    with tf.Graph().as_default():

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))

        with sess.as_default():

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(FACENET_MODEL_PATH)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            pnet, rnet, onet = align.detect_face.create_mtcnn(
                sess, "src/align")

            people_detected = set()
            person_detected = collections.Counter()

            cap = VideoStream(src=0).start()

            while (True):
                frame = cap.read()
                frame = imutils.resize(frame, width=600)
                frame = cv2.flip(frame, 1)

                bounding_boxes, _ = align.detect_face.detect_face(
                    frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

                faces_found = bounding_boxes.shape[0]
                try:
                    if faces_found > 1:
                        cv2.putText(frame,
                                    "Only one face", (0, 100),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                    elif faces_found > 0:
                        det = bounding_boxes[:, 0:4]
                        bb = np.zeros((faces_found, 4), dtype=np.int32)
                        for i in range(faces_found):
                            bb[i][0] = det[i][0]
                            bb[i][1] = det[i][1]
                            bb[i][2] = det[i][2]
                            bb[i][3] = det[i][3]
                            print(bb[i][3] - bb[i][1])
                            print(frame.shape[0])
                            print((bb[i][3] - bb[i][1]) / frame.shape[0])
                            if (bb[i][3] - bb[i][1]) / frame.shape[0] > 0.25:
                                cropped = frame[bb[i][1]:bb[i][3],
                                                bb[i][0]:bb[i][2], :]
                                scaled = cv2.resize(
                                    cropped,
                                    (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                    interpolation=cv2.INTER_CUBIC)
                                scaled = facenet.prewhiten(scaled)
                                scaled_reshape = scaled.reshape(
                                    -1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                                feed_dict = {
                                    images_placeholder: scaled_reshape,
                                    phase_train_placeholder: False
                                }
                                emb_array = sess.run(embeddings,
                                                     feed_dict=feed_dict)
                                print(emb_array.shape)
                                predictions = model.predict_proba(emb_array)
                                best_class_indices = np.argmax(predictions,
                                                               axis=1)
                                best_class_probabilities = predictions[
                                    np.arange(len(best_class_indices)),
                                    best_class_indices]
                                best_name = class_names[best_class_indices[0]]
                                print("Name: {}, Probability: {}".format(
                                    best_name, best_class_probabilities))

                                if best_class_probabilities > 0.8:
                                    cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                                  (bb[i][2], bb[i][3]),
                                                  (0, 255, 0), 2)
                                    text_x = bb[i][0]
                                    text_y = bb[i][3] + 20

                                    name = class_names[best_class_indices[0]]
                                    cv2.putText(frame,
                                                name, (text_x, text_y),
                                                cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                1, (255, 255, 255),
                                                thickness=1,
                                                lineType=2)
                                    cv2.putText(
                                        frame,
                                        str(
                                            round(best_class_probabilities[0],
                                                  3)), (text_x, text_y + 17),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255),
                                        thickness=1,
                                        lineType=2)
                                    person_detected[best_name] += 1
                                else:
                                    name = "Unknown"

                except:
                    pass

                cv2.imshow('Face Recognition', frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            cap.release()
            cv2.destroyAllWindows()