-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceNet.py
75 lines (63 loc) · 2.17 KB
/
FaceNet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
from imageio import imread
from skimage.transform import resize
from scipy.spatial import distance
from matplotlib import pyplot as plt
import pickle
import pandas as pd
from tqdm import tqdm
from sklearn.metrics.pairwise import euclidean_distances
cascade_path = 'model/cv2/haarcascade_frontalface_alt2.xml'
def prewhiten(x):
if x.ndim == 4:
axis = (1, 2, 3)
size = x[0].size
elif x.ndim == 3:
axis = (0, 1, 2)
size = x.size
else:
raise ValueError('Dimension should be 3 or 4')
mean = np.mean(x, axis=axis, keepdims=True)
std = np.std(x, axis=axis, keepdims=True)
std_adj = np.maximum(std, 1.0/np.sqrt(size))
y = (x - mean) / std_adj
return y
def l2_normalize(x, axis=-1, epsilon=1e-10):
output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True), epsilon))
return output
images_error = []
def scale(img_path,margin=10):
image_size = 160
aligned_images=[]
cascade = cv2.CascadeClassifier(cascade_path)
img = imread(img_path)
faces = cascade.detectMultiScale(img,
scaleFactor=1.1,
minNeighbors=3)
(x, y, w, h) = faces[0]
cropped = img[y-margin//2:y+h+margin//2,
x-margin//2:x+w+margin//2, :]
aligned = resize(cropped, (image_size, image_size), mode='reflect')
aligned_images.append(aligned)
return np.array(aligned_images)
def facenet(img_path, model, margin,graph):
try:
aligned_images = prewhiten(scale(img_path,margin))
#embs = model.predict(aligned_images)
print('------------')
print(aligned_images.shape)
if aligned_images.shape[-1]==4:
aligned_images=aligned_images[:,:,:,:-1]
with graph.as_default():
embs = model.predict(aligned_images)
embs = l2_normalize(embs)
return embs
except:
images_error.append(img_path)
print(img_path,' : visage non détecté')