Ejemplo n.º 1
0
 def load_model(self, model_dir_path):
     self.model = faceRecoModel(input_shape=(3, 96, 96))
     print("Total Params:", self.model.count_params())
     self.model.compile(optimizer='adam',
                        loss=triplet_loss,
                        metrics=['accuracy'])
     load_weights_from_FaceNet(self.model, model_dir_path)
Ejemplo n.º 2
0
def load_weights(FRmodel) -> None:
    """Load weights from FaceNet model to avoid training cost.

    Argeuments:
    FRmodel -- """
    FRmodel = faceRecoModel(input_shape=(3, 96, 96))
    FRmodel.compile(optimizer='adam', loss=triplet_loss, metrics=['accuracy'])
    load_weights_from_FaceNet(FRmodel)
Ejemplo n.º 3
0
def load_feature_model(model_file):
    if os.path.exists(model_file):
        # https://github.com/keras-team/keras/issues/5916
        # import keras.losses
        # keras.losses.custom_loss = triplet_loss
        # FRmodel = load_model(model_file)
        start = time.time()
        model = load_model(model_file,
                           custom_objects={'triplet_loss': triplet_loss})
        done = time.time()
        print(f"model loaded in {done-start} seconds")
    else:
        model = faceRecoModel(input_shape=(3, model_width, model_height))
        model.compile(optimizer='adam',
                      loss=triplet_loss,
                      metrics=['accuracy'])
        load_weights_from_FaceNet(model)
        model.save(model_file)
    print("Total Params:", model.count_params())
    return model
Ejemplo n.º 4
0
def main():
    start_time = time.time()
    np.set_printoptions(threshold=np.nan)
    FRmodel = faceRecoModel(input_shape=(3, 96, 96))
    print("Total Params:", FRmodel.count_params())
    FRmodel.compile(optimizer='adam', loss=triplet_loss, metrics=['accuracy'])
    load_weights_from_FaceNet(FRmodel)

    # reate database
    database = dict()
    database["younes"] = img_to_encoding("images/younes.jpg", FRmodel)
    database["kevin"] = img_to_encoding("images/kevin.jpg", FRmodel)
    database["andrew"] = img_to_encoding("images/andrew.jpg", FRmodel)
    database["sebastiano"] = img_to_encoding("images/sebastiano.jpg", FRmodel)
    database["moi"] = img_to_encoding("images/moi3.jpg", FRmodel)

    verify("images/camera_0.jpg", "younes", database, FRmodel)

    print('')
    print("---- camera_0 ----")
    who_is_it("images/camera_0.jpg", database, FRmodel)

    print('')
    print("---- moi1 ----")
    who_is_it("images/moi1.jpg", database, FRmodel, display=True)

    print('')
    print("---- moi2 ----")
    who_is_it("images/moi2.jpg", database, FRmodel, display=True)

    print('')
    print("---- moi4 ----")
    who_is_it("images/moi4.jpg", database, FRmodel, display=True)

    print('')
    print("---- moi5 ----")
    who_is_it("images/moi5.jpg", database, FRmodel, display=True)

    print('')
    print("--- %s seconds ---" % (time.time() - start_time))
Ejemplo n.º 5
0
    else:
        print("it's " + str(identity) + ", the distance is " + str(min_dist))

    return min_dist, identity


def resize_image(url, width, height):
    with open(url, 'r+b') as f:
        with Image.open(f) as image:
            cover = resizeimage.resize_cover(image, [width, height])
            cover.save(url, image.format)


if __name__ == '__main__':
    K.set_image_data_format('channels_first')
    FRmodel = faceRecoModel(input_shape=(3, 96, 96))

    load_weights(FRmodel)

    database = dict()

    resize_image("/Users/markmc/Repos/RecogNet/faces/liis_01.jpg", 96, 96)
    resize_image("/Users/markmc/Repos/RecogNet/faces/mark_01.jpg", 96, 96)
    resize_image("/Users/markmc/Repos/RecogNet/faces/liis_02.jpg", 96, 96)
    resize_image("/Users/markmc/Repos/RecogNet/faces/mark_02.jpg", 96, 96)

    database["lisa"] = img_to_encoding(
        "/Users/markmc/Repos/RecogNet/faces/liis_03.jpg", FRmodel)
    database["mark"] = img_to_encoding(
        "/Users/markmc/Repos/RecogNet/faces/mark_05.jpg", FRmodel)
Ejemplo n.º 6
0
def initModel():
    model = faceRecoModel(input_shape=INPUT_SHAPE)
    utils.load_weights_from_FaceNet(model)
    return model
Ejemplo n.º 7
0
def myMainMethod():

    FRmodel = inception_blocks_v2.faceRecoModel(input_shape=(3, 96, 96))

    encoding = fr_utils.img_path_to_encoding("crop_frame.jpg", FRmodel)
    print(encoding)
Ejemplo n.º 8
0
# import fr_utils				as fr_utils
# import inception_blocks_v2	as inception_blocks_v2
import fr_utils
import inception_blocks_v2
# 3 つのライブラリを読み込む
# import importlib
# libs = ['fr_utils', 'inception_blocks_v2']
# for lib in libs:
#	importlib.import_module(lib)

PADDING = 50
ready_to_detect_identity = True
#windows10_voice_interface = wincl.Dispatch("SAPI.SpVoice")

print( f"create faceRecModel...")
FRmodel = inception_blocks_v2.faceRecoModel(input_shape=(3, 96, 96))
print( f"create faceRecModel...done")

def triplet_loss(y_true, y_pred, alpha = 0.3):
	"""
	Implementation of the triplet loss as defined by formula (3)

	Arguments:
	y_pred -- python list containing three objects:
		anchor -- the encodings for the anchor images, of shape (None, 128)
		positive -- the encodings for the positive images, of shape (None, 128)
		negative -- the encodings for the negative images, of shape (None, 128)

	Returns:
		loss -- real number, value of the loss
	"""
Ejemplo n.º 9
0
 def createmdl_img(self, input_shape):
     self.FRmodel = inception_blocks_v2.faceRecoModel(input_shape)
     print("model is created and Total Params:" +
           str(self.FRmodel.count_params()))
     return self.FRmodel
Ejemplo n.º 10
0
    return min_dist, identity


def img_to_encoding(image_path, model):
    img1 = cv2.imread(image_path, 1)
    img = img1[..., ::-1]
    img = np.around(np.transpose(img, (2, 0, 1)) / 255.0, decimals=12)
    x_train = np.array([img])
    encoding = model.predict_on_batch(x_train)
    return encoding


# Main Code

FRmodel = ib.faceRecoModel(input_shape=(3, 93, 93))

us.load_weights_from_FaceNet(FRmodel)

database = {}
database["Negar"] = img_to_encoding("images/Negar1.jpg", FRmodel)
database["MySister"] = img_to_encoding("images/Nazanin.jpg", FRmodel)
print()
print('Verify if the new image is Negar:')
face_verification("images/Negar4.jpg", "Negar", database, FRmodel)
print('*********************************')
face_verification("images/Nazanin.jpg", "Negar", database, FRmodel)

print('*********************************')
print('Recognize who is in the picture:')