Beispiel #1
0
def main(args):
    with open(args.pickle,'rb') as f:
        feature_array = pickle.load(f, encoding='utf-8') 

        #model_exp = '../lib/src/ckpt/20170512-110547'
        model_exp = args.model
        graph_fr = tf.Graph()
        sess_fr = tf.Session(graph=graph_fr)
        with graph_fr.as_default():
            with sess_fr.as_default():
                #saverf = tf.train.import_meta_graph(os.path.join(model_exp, 'model-20180408-102900.meta'))
                #saverf.restore(sess_fr, os.path.join(model_exp, 'model-20180408-102900.ckpt-90'))
                load_model(model_exp)
                pnet, rnet, onet = detect_face.create_mtcnn(sess_fr, None)
                retrieve.recognize_face(sess_fr,pnet, rnet, onet,feature_array)
    return render_template("index.html")


@app.route("/predict")
def predict_page():
    """Renders the 'predict.html' page for manual image file uploads for prediction."""
    return render_template("predict.html")


if __name__ == '__main__':
    """Server and FaceNet Tensorflow configuration."""

    # Load FaceNet model and configure placeholders for forward pass into the FaceNet model to calculate embeddings
    model_path = 'model/20170512-110547/20170512-110547.pb'
    facenet_model = load_model(model_path)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    image_size = 160
    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")

    # Initiate persistent FaceNet model in memory
    facenet_persistent_session = tf.Session(graph=facenet_model, config=config)

    # Create Multi-Task Cascading Convolutional (MTCNN) neural networks for Face Detection
    pnet, rnet, onet = detect_face.create_mtcnn(sess=facenet_persistent_session, model_path=None)

    # Start flask application on waitress WSGI server
    serve(app=app, host='0.0.0.0', port=5000)
Beispiel #3
0
#    Loading the stored face embedding vectors for image retrieval
#
#
#==============================================================================================================================
with open('../lib/src/face_embeddings.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)


#==============================================================================================================================
#
#  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)


#==============================================================================================================================
Beispiel #4
0
def main(input_dir, output_dir, image_size, margin, random_order,
         gpu_memory_fraction, detect_multiple_faces):
    sleep(random.random())
    output_dir = os.path.expanduser(output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    #src_path,_ = os.path.split(os.path.realpath(__file__))
    src_path = input_dir
    facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(input_dir)

    print('Creating networks and loading parameters')

    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():
            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)

    if 1:

        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if random_order:
            random.shuffle(dataset)
        for cls in dataset:
            print("cls.name", cls.name)
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            #text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        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]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                if detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (
                                        det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                                  det[:, 1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([
                                        (det[:, 0] + det[:, 2]) / 2 -
                                        img_center[1],
                                        (det[:, 1] + det[:, 3]) / 2 -
                                        img_center[0]
                                    ])
                                    offset_dist_squared = np.sum(
                                        np.power(offsets, 2.0), 0)
                                    index = np.argmax(
                                        bounding_box_size -
                                        offset_dist_squared * 2.0
                                    )  # some extra weight on the centering
                                    det_arr.append(det[index, :])
                            else:
                                det_arr.append(np.squeeze(det))

                            for i, det in enumerate(det_arr):
                                det = np.squeeze(det)
                                bb = np.zeros(4, dtype=np.int32)
                                bb[0] = np.maximum(det[0] - margin / 2, 0)
                                bb[1] = np.maximum(det[1] - margin / 2, 0)
                                bb[2] = np.minimum(det[2] + margin / 2,
                                                   img_size[1])
                                bb[3] = np.minimum(det[3] + margin / 2,
                                                   img_size[0])
                                cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                                scaled = misc.imresize(
                                    cropped, (image_size, image_size),
                                    interp='bilinear')
                                nrof_successfully_aligned += 1
                                output_filename_n = "{}_{}.{}".format(
                                    output_filename.split('.')[0], i,
                                    output_filename.split('.')[-1])
                                misc.imsave(output_filename_n, scaled)
                                #text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3]))
                        else:
                            print('Unable to align "%s"' % image_path)
                            #text_file.write('%s\n' % (output_filename))

    os.remove(os.path.join(settings.BASE_DIR,
                           f'media/datab/revision_info.txt'))
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)
Beispiel #5
0
def recognize(argv):
    dt = datetime.datetime

    #detector = dlib.get_frontal_face_detector()
    #detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    use_haar = False  #True
    use_dlib = False
    with open(argv.pickle, 'rb') as f:
        sys.stderr.write("will load feature\n")
        feature_array = pickle.load(f, encoding='utf-8')
        model_exp = argv.model
        graph_fr = tf.Graph()
        sess_fr = tf.Session(graph=graph_fr)
        with graph_fr.as_default():
            with sess_fr.as_default():
                image_size = (160, 160)
                sys.stderr.write("will load model\n")
                #sys.stderr.write("did load model\n")

                if not use_dlib and not use_haar:
                    pnet, rnet, onet = detect_face.create_mtcnn(sess_fr, None)
                load_model(model_exp)
                #sys.stderr.write("will get placeholders\n")
                images_placeholder = sess_fr.graph.get_tensor_by_name(
                    "input:0")
                images_placeholder = tf.image.resize_images(
                    images_placeholder, image_size)
                embeddings = sess_fr.graph.get_tensor_by_name("embeddings:0")
                phase_train_placeholder = sess_fr.graph.get_tensor_by_name(
                    "phase_train:0")

                downsample = shared['downsample']
                shared['recog_ready'] = True
                while True:
                    t = dt.utcnow()
                    img = get_img()
                    if img is not None:

                        downsampleShape = (int(img.shape[1] / downsample),
                                           int(img.shape[0] / downsample))
                        #img = np.asarray(Image.fromarray(img).resize(downsampleShape, resample=Image.BILINEAR))
                        if downsample > 1:
                            img = cv2.resize(img, downsampleShape)
                        if use_dlib:
                            result = retrieve.recognize_hog(
                                images_placeholder, phase_train_placeholder,
                                embeddings, sess_fr, feature_array, img,
                                detector)
                        elif use_haar:
                            result = retrieve.recognize_haar(
                                images_placeholder, phase_train_placeholder,
                                embeddings, sess_fr, feature_array, img,
                                detector)
                        else:
                            result = []
                            for roi in shared['candidate_area']:
                                bbox = roi[0]
                                cropped = roi[1]
                                #print("align in {},{}: {},{}".format(bbox[0], bbox[1], bbox[2], bbox[3]))
                                r = retrieve.recognize_mtcnn(
                                    images_placeholder,
                                    phase_train_placeholder, embeddings,
                                    sess_fr, pnet, rnet, onet, feature_array,
                                    cropped)
                                for i, face in enumerate(r):
                                    b = face['box']
                                    b[0] += bbox[0]
                                    b[1] += bbox[1]
                                    b[2] += bbox[0]
                                    b[3] += bbox[1]
                                    r[i]['box'] = b
                                    print("face: {},{},{},{}".format(
                                        b[0], b[1], b[2], b[3]))
                                result.extend(r)
                        update_result(result, t)