InferencetrainingAI commited on
Commit
eaa5438
1 Parent(s): e9826e7

Update README.md

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