深度学习 Face Recognition for the Happy House - v2

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
from keras.models import Sequential
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
from keras.models import Model
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.merge import Concatenate
from keras.layers.core import Lambda, Flatten, Dense
from keras.initializers import glorot_uniform
from keras.engine.topology import Layer
from keras import backend as K
K.set_image_data_format('channels_first')
import cv2
import os
import numpy as np
from numpy import genfromtxt
import pandas as pd
import tensorflow as tf
from fr_utils import *
from inception_blocks import *
np.set_printoptions(threshold=np.nan)
FRmodel=faceRecoModel(input_shape=(3,96,96))
# print("Total Params:",FRmodel.count_params())
def triplet_loss(y_true,y_pred,alpha=0.2):
    anchor,positive,negative=y_pred[0],y_pred[1],y_pred[2]
    post_dist=tf.reduce_sum(tf.square(tf.subtract(anchor,positive)))
    neg_dist=tf.reduce_sum(tf.square(tf.subtract(anchor,negative)))
    basic_loss=tf.add(tf.subtract(post_dist,neg_dist),alpha)
    loss=tf.reduce_mean(tf.maximum(basic_loss,0.0))
    return loss
# with tf.Session() as test:
#     tf.set_random_seed(1)
#     y_true = (None, None, None)
#     y_pred = (tf.random_normal([3, 128], mean=6, stddev=0.1, seed=1),
#               tf.random_normal([3, 128], mean=1, stddev=1, seed=1),
#               tf.random_normal([3, 128], mean=3, stddev=4, seed=1))
#     loss = triplet_loss(y_true, y_pred)
#     print("loss = " + str(loss.eval()))
FRmodel.compile(optimizer='adam',loss=triplet_loss,metrics=['accuracy'])
load_weights_from_FaceNet(FRmodel)
database = {}
database["danielle"] = img_to_encoding("images/danielle.png", FRmodel)
database["younes"] = img_to_encoding("images/younes.jpg", FRmodel)
database["tian"] = img_to_encoding("images/tian.jpg", FRmodel)
database["andrew"] = img_to_encoding("images/andrew.jpg", FRmodel)
database["kian"] = img_to_encoding("images/kian.jpg", FRmodel)
database["dan"] = img_to_encoding("images/dan.jpg", FRmodel)
database["sebastiano"] = img_to_encoding("images/sebastiano.jpg", FRmodel)
database["bertrand"] = img_to_encoding("images/bertrand.jpg", FRmodel)
database["kevin"] = img_to_encoding("images/kevin.jpg", FRmodel)
database["felix"] = img_to_encoding("images/felix.jpg", FRmodel)
database["benoit"] = img_to_encoding("images/benoit.jpg", FRmodel)
database["arnaud"] = img_to_encoding("images/arnaud.jpg", FRmodel)

def verify(image_path,identity,database,model):
    encoding=img_to_encoding(image_path,model)
    # dist=np.sqrt(np.sum(np.square(np.subtract(encoding,database[identity]))))
    dist=np.linalg.norm(np.subtract(encoding,database[identity]))
    if dist<0.7:
        door_open=True
        print("It's"+str(identity)+",welcome home! ("+str(dist)+" "+str(door_open)+")")
    else:
        door_open=False
        print("It's not"+str(identity)+",please go away ("+str(dist)+" "+str(door_open)+")")
    return dist,door_open
# verify("images/camera_0.jpg", "younes", database, FRmodel)
def who_is_it(image_path,database,model):
    encoding=img_to_encoding(image_path,model)
    min_dist=100
    for(name,db_enc) in database.items():
        dist=np.linalg.norm(np.subtract(encoding,db_enc))
        if dist<min_dist:
            min_dist=dist
            identity=name
    if min_dist>0.7:
        print("Not in the database.")
    else:
        print("it's"+str(identity)+",the distance is"+str(min_dist))
    return min_dist,identity
who_is_it("images/camera_0.jpg",database,FRmodel)
运行结果:
it'syounes,the distance is0.6710074

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值