InferencetrainingAI commited on
Commit
4f6d94e
1 Parent(s): 8f7c994

Upload FaceAuth.py

Browse files

FaceAuth utilizing the Insight-face Model

Files changed (1) hide show
  1. FaceAuth.py +123 -0
FaceAuth.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ from insightface.app import FaceAnalysis
4
+ import torch
5
+
6
+
7
+
8
+ # prompt: compare face embediggs
9
+
10
+
11
+
12
+
13
+ class FaceRec:
14
+ def __init__(self):
15
+ self.foldername = '/home/emmanuel/Pictures/Webcam'
16
+ self.files = []
17
+ self.embeds = []
18
+ self.diff = []
19
+ self.ground_mathches = []
20
+ self.sampling = None
21
+
22
+
23
+
24
+ def folder(self, attempt=True, folder='/home/emmanuel/Pictures/Webcam'):
25
+ if attempt:
26
+ for file in os.listdir(folder):
27
+ self.files.append(file)
28
+
29
+ self.image_pair = list(zip(self.files[0:len(self.files)//2], self.files[len(self.files)//2:]))
30
+ print(self.image_pair)
31
+
32
+
33
+ else:
34
+ self.foldername = '/home/emmanuel/Pictures/webcam'
35
+ self.files = []
36
+ self.folder(attempt=True, folder=self.foldername)
37
+
38
+
39
+
40
+
41
+ def embeddings(self, image):
42
+ app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
43
+ app.prepare(ctx_id=0, det_size=(640, 640))
44
+ image1 = cv2.imread(image)
45
+ faces = app.get(image1)
46
+
47
+ faceid_embeds = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
48
+ return(torch.Tensor(faceid_embeds))
49
+
50
+
51
+
52
+ def face_embed(self, face, face1):
53
+ # Load the two images and get their face embeddings.
54
+ face_encodings = self.embeddings(face)
55
+ face_encodings1 = self.embeddings(face1)
56
+ return(torch.nn.functional.cosine_similarity(face_encodings, face_encodings1))
57
+
58
+
59
+
60
+ def closeness(self):
61
+ self.embeds = []
62
+ for faces in self.image_pair:
63
+ self.embeds.append(self.face_embed(self.foldername+'/'+faces[0], self.foldername+'/'+faces[1]))
64
+
65
+ return(0)
66
+
67
+
68
+ def compare(self, attempt=True):
69
+ self.diff = []
70
+ for diffs in list(zip(self.embeds[0:len(self.embeds)//2], self.embeds[len(self.embeds)//2:])):
71
+ self.diff.append(torch.nn.functional.pairwise_distance(diffs[0], diffs[1]))
72
+
73
+
74
+
75
+
76
+ def expectation(self):
77
+ mean, std = torch.mean(torch.Tensor(self.diff[0:])), torch.std(torch.Tensor(self.diff[0:]))
78
+ distribute = torch.distributions.Normal(mean, std)
79
+ self.sampling = distribute.sample(sample_shape=(10,))
80
+
81
+
82
+
83
+ def model(self):
84
+ self.closeness()
85
+ return(self.compare())
86
+
87
+
88
+
89
+ def verify(self):
90
+ self.folder()
91
+ self.model()
92
+ self.expectation()
93
+ self.folder(attempt=False)
94
+ self.model()
95
+
96
+ fails = 0
97
+ success = 0
98
+ max_itter = 10
99
+ while max_itter >= 0:
100
+ for samples in self.sampling:
101
+ if self.diff[0] <= samples:
102
+ success = success+1
103
+
104
+ else:
105
+ fails = fails+1
106
+
107
+ max_itter = max_itter-1
108
+
109
+
110
+ if fails > success:
111
+ return(False)
112
+
113
+ else:
114
+ return(True)
115
+
116
+
117
+
118
+
119
+
120
+
121
+ Recognition = FaceRec()
122
+ print(Recognition.verify())
123
+