def verify(align_imgs): """Verify images after align @input: image after align @output: distance """ g = get_graph() with g.as_default(): # Get input and output tensors images_placeholder = g.get_tensor_by_name("input:0") embeddings = g.get_tensor_by_name("embeddings:0") phase_train_placeholder = g.get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: align_imgs, phase_train_placeholder: False } sess = get_session() emb = sess.run(embeddings, feed_dict=feed_dict) dist = np.sqrt(np.sum(np.square(np.substract(emb[0, :], emb[1, :])))) print('distance: %1.4f' % dist) return '%1.4f' % dist
def init_session(): global _sess # single session for facenet _sess = tf.Session(graph=get_graph()) # load_model(sess, model_dir) exp_model_path = os.path.expanduser(_model_path) load_model(_sess, exp_model_path)
def init(): # measure time_used for init start = time.time() graph = tfgraph.get_graph() # sess = tf.Session(graph=graph) sess = tfsession.get_session() return jsonify(time_used=time.time() - start)
def load_model(sess, model_dir): """Load facenet model """ # measure time used for loading model start = time.time() model_dir_exp = os.path.expanduser(model_dir) meta_file, ckpt_file = get_model_filenames(model_dir) meta_file_full_path = os.path.join(model_dir_exp, meta_file) ckpt_file_full_path = os.path.join(model_dir_exp, ckpt_file) print('Model meta file: ', meta_file_full_path) print('Model ckpt file: ', ckpt_file_full_path) print('Loading models. Waiting...') from ngface.tfgraph import get_graph g = get_graph() with g.as_default(): saver = tf.train.import_meta_graph(meta_file_full_path) saver.restore(sess, ckpt_file_full_path) print('Models loaded. time_used: ', time.time() - start)
def verify(): # verify time_used start = time.time() # read image from request img_list = utils.get_images_from_request(request.files, ['img1', 'img2']) np_images = load_and_align_data(img_list) dist_str = '' dist = 0 graph = tfgraph.get_graph() with graph.as_default(): # Get input and output tensors images_placeholder = graph.get_tensor_by_name("input:0") embeddings = graph.get_tensor_by_name("embeddings:0") phase_train_placeholder = graph.get_tensor_by_name("phase_train:0") # Run forward pass to calculate embeddings feed_dict = { images_placeholder: np_images, phase_train_placeholder: False } sess = tfsession.get_session() emb = sess.run(embeddings, feed_dict=feed_dict) # nrof_images = len(args.image_files) dist = np.sqrt(np.sum(np.square(np.subtract(emb[0, :], emb[1, :])))) dist_str = '%1.4f' % dist print('Distance: ', dist_str) threshold = 0.9 print('Threshold: ', threshold) time_used = time.time() - start return jsonify(is_same_person=str(dist < threshold), time_used=str(time_used))