Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vp facenet #1162

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/align/align_dataset_mtcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import argparse
import tensorflow as tf
import numpy as np
import facenet
import align.detect_face
import facenet.src.facenet as facenet
import detect_face
import random
from time import sleep

Expand All @@ -45,23 +45,23 @@ def main(args):
src_path,_ = os.path.split(os.path.realpath(__file__))
facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
dataset = facenet.get_dataset(args.input_dir)

print('Creating networks and loading parameters')

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, log_device_placement=False))
with sess.as_default():
pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

minsize = 20 # minimum size of face
threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
factor = 0.709 # scale factor

# Add a random key to the filename to allow alignment using multiple processes
random_key = np.random.randint(0, high=99999)
bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)

with open(bounding_boxes_filename, "w") as text_file:
nrof_images_total = 0
nrof_successfully_aligned = 0
Expand Down Expand Up @@ -92,8 +92,8 @@ def main(args):
if img.ndim == 2:
img = facenet.to_rgb(img)
img = img[:,:,0:3]
bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)

bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0]
if nrof_faces>0:
det = bounding_boxes[:,0:4]
Expand Down Expand Up @@ -157,3 +157,11 @@ def parse_arguments(argv):

if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))

# python align_dataset_mtcnn.py
# D:\BDBA\SEMESTER_2\Case_Study_1\Datasets\lfw\raw
# D:\BDBA\SEMESTER_2\Case_Study_1\Datasets\lfw\lfw_mtcnnpy_160
# --image_size 160
# --margin 32
# --random_order
# --gpu_memory_fraction 0.25
105 changes: 105 additions & 0 deletions src/create_face_embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# MIT License
#
# Copyright (c) 2016 David Sandberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np
import argparse
import facenet
import lfw
import os
import sys
import math
import tqdm
from sklearn import metrics
from scipy.optimize import brentq
from scipy import interpolate
from imutils import paths
import pickle
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')]
print("loading the dataset")
# imagePaths = list(paths.list_images(args.dataset_dir))
imagepaths = list(paths.list_files(args.dataset_dir))
# 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(imagepaths):

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('C:/Users/Dell/PycharmProjects/facenet/src/Create_face_embeddings/extracted_dict.pickle','wb') as f:
pickle.dump(extracted_dict,f)



def parse_arguments(argv):
parser = argparse.ArgumentParser()

#adding a new argument to provide the dataset path
parser.add_argument('--dataset_dir', type=str ,
help='Path to the dataset containing the images')
parser.add_argument('--lfw_batch_size', type=int,
help='Number of images to process in a batch in the LFW test set.', default=100)
# parser.add_argument('--model', type=str,default='/home/gpuuser/vinayak/models/facenet/src/ckpt/20170512-110547',
parser.add_argument('--model', type=str,default='D:/BDBA/SEMESTER_2/Case_Study_1/model/20180402-114759',
help='Could be either a directory containing the meta_file and ckpt_file or a model protobuf (.pb) file')
parser.add_argument('--image_size', type=int,
help='Image size (height, width) in pixels.', default=160)

return parser.parse_args(argv)

if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))
15 changes: 15 additions & 0 deletions src/facenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ def load_data(image_paths, do_random_crop, do_random_flip, image_size, do_prewhi
images[i,:,:,:] = img
return images

def load_image(img, do_random_crop, do_random_flip, image_size, do_prewhiten=True):
#nrof_samples = len(image_paths)
images = np.zeros((1, image_size, image_size, 3))
#for i in range(nrof_samples):
img = misc.imread(img)
#img = misc.imresize(img,(160,160,3))
if img.ndim == 2:
img = to_rgb(img)
if do_prewhiten:
img = prewhiten(img)
img = crop(img, do_random_crop, image_size)
img = flip(img, do_random_flip)
images[:,:,:,:] = img
return images

def get_label_batch(label_data, batch_size, batch_index):
nrof_examples = np.size(label_data, 0)
j = batch_index*batch_size % nrof_examples
Expand Down
80 changes: 80 additions & 0 deletions src/generate_pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#! encoding: utf-8

import os
import random

class GeneratePairs:
"""
Generate the pairs.txt file that is used for training face classifier when calling python `src/train_softmax.py`.
Or others' python scripts that needs the file of pairs.txt.

Doc Reference: http://vis-www.cs.umass.edu/lfw/README.txt
"""

def __init__(self, data_dir, pairs_filepath, img_ext):
"""
Parameter data_dir, is your data directory.
Parameter pairs_filepath, where is the pairs.txt that belongs to.
Parameter img_ext, is the image data extension for all of your image data.
"""
self.data_dir = data_dir
self.pairs_filepath = pairs_filepath
self.img_ext = img_ext


def generate(self):
self._generate_matches_pairs()
self._generate_mismatches_pairs()


def _generate_matches_pairs(self):
"""
Generate all matches pairs
"""
for name in os.listdir(self.data_dir):
if name == ".DS_Store":
continue

a = []
for file in os.listdir(self.data_dir + name):
if file == ".DS_Store":
continue
a.append(file)

with open(self.pairs_filepath, "a") as f:
for i in range(3):
temp = random.choice(a).split("_") # This line may vary depending on how your images are named.
w = temp[0] + "_" + temp[1]
l = random.choice(a).split("_")[2].lstrip("0").rstrip(self.img_ext)
r = random.choice(a).split("_")[2].lstrip("0").rstrip(self.img_ext)
f.write(w + "\t" + l + "\t" + r + "\n")


def _generate_mismatches_pairs(self):
"""
Generate all mismatches pairs
"""
for i, name in enumerate(os.listdir(self.data_dir)):
if name == ".DS_Store":
continue

remaining = os.listdir(self.data_dir)
remaining = [f_n for f_n in remaining if f_n != ".DS_Store"]
# del remaining[i] # deletes the file from the list, so that it is not chosen again
other_dir = random.choice(remaining)
with open(self.pairs_filepath, "a") as f:
for i in range(3):
file1 = random.choice(os.listdir(self.data_dir + name))
file2 = random.choice(os.listdir(self.data_dir + other_dir))
f.write(name + "\t" + file1.split("_")[2].lstrip("0").rstrip(self.img_ext) + "\t")
f.write("\n")


if __name__ == '__main__':
# data_dir = "D:/BDBA/SEMESTER_2/Case_Study_1/Datasets/my_dataset/lfw_mtcnnpy_train_160/"
data_dir = "D:/BDBA/SEMESTER_2/Case_Study_1/Datasets/my_dataset/lfw_mtcnnpy_test_160/"
pairs_filepath = "C:/Users/Dell/PycharmProjects/facenet/src/pairs.txt"
img_ext = ".jpg"
generatePairs = GeneratePairs(data_dir, pairs_filepath, img_ext)
generatePairs.generate()

2 changes: 1 addition & 1 deletion src/lfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_paths(lfw_dir, pairs):
else:
nrof_skipped_pairs += 1
if nrof_skipped_pairs>0:
print('Skipped %d image pairs' % nrof_skipped_pairs)
print('Skipped %d image pairs hahahaha' % nrof_skipped_pairs)

return path_list, issame_list

Expand Down
76 changes: 76 additions & 0 deletions src/rest-server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!flask/bin/python
################################################################################################################################
#------------------------------------------------------------------------------------------------------------------------------
# This file implements the REST layer. It uses flask micro framework for server implementation. Calls from front end reaches
# here as json and being branched out to each projects. Basic level of validation is also being done in this file. #
#-------------------------------------------------------------------------------------------------------------------------------
################################################################################################################################
from flask import Flask, jsonify, abort, request, make_response, url_for,redirect, render_template
# from flask.ext.httpauth import HTTPBasicAuth
from flask_httpauth import HTTPBasicAuth
from werkzeug.utils import secure_filename
import os
import sys
import random
from tensorflow.python.platform import gfile
from six import iteritems
sys.path.append('..')
import numpy as np
import retrieve
import detect_face
import tensorflow as tf
import pickle
from tensorflow.python.platform import gfile
app = Flask(__name__, static_url_path = "")

auth = HTTPBasicAuth()

#==============================================================================================================================
#
# Loading the stored face embedding vectors for image retrieval
#
#
#==============================================================================================================================
# with open('../lib/src/face_embeddings.pickle','rb') as f:
# feature_array = pickle.load(f)

with open('C:/Users/Dell/PycharmProjects/facenet/src/Create_face_embeddings/extracted_dict.pickle','rb') as f:
feature_array = pickle.load(f)

# model_exp = '../lib/src/ckpt/20170512-110547'
# graph_fr = tf.Graph()
# sess_fr = tf.Session(graph=graph_fr)
# with graph_fr.as_default():
# saverf = tf.train.import_meta_graph(os.path.join(model_exp, 'model-20170512-110547.meta'))
# saverf.restore(sess_fr, os.path.join(model_exp, 'model-20170512-110547.ckpt-250000'))
# pnet, rnet, onet = detect_face.create_mtcnn(sess_fr, None)

model_exp = 'D:/BDBA/SEMESTER_2/Case_Study_1/model/20180402-114759'
graph_fr = tf.Graph()
sess_fr = tf.Session(graph=graph_fr)
with graph_fr.as_default():
saverf = tf.train.import_meta_graph(os.path.join(model_exp, 'model-20180402-114759.meta'))
saverf.restore(sess_fr, os.path.join(model_exp, 'model-20180402-114759.ckpt-275.ckpt-275'))
pnet, rnet, onet = detect_face.create_mtcnn(sess_fr, None)
#==============================================================================================================================
#
# This function is used to do the face recognition from video camera
#
#
#==============================================================================================================================
@app.route('/facerecognitionLive', methods=['GET', 'POST'])
def face_det():

retrieve.recognize_face(sess_fr,pnet, rnet, onet,feature_array)

#==============================================================================================================================
#
# Main function #
#
#==============================================================================================================================
@app.route("/")
def main():

return render_template("main.html")
if __name__ == '__main__':
app.run(debug = True, host= '0.0.0.0')
Loading