-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_verification.py
executable file
·66 lines (48 loc) · 2.03 KB
/
face_verification.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
from argparse import ArgumentParser, BooleanOptionalAction
from insightface.app import FaceAnalysis
import cv2 as cv
import onnxruntime as ort
import utils as ul
arg = ArgumentParser()
arg.add_argument('--model', type=str, default='buffalo_s',
help='Enter the model')
arg.add_argument('--img1', type=str, required=True,
help='Enter the first image path')
arg.add_argument('--img2', type=str, required=True,
help='Enter the second image path')
arg.add_argument('--thresh', type=float, default=25.,
help='Threshold of compare')
arg.add_argument('--show', type=bool, default=False, action=BooleanOptionalAction,
help='Show the results')
opt = arg.parse_args()
def main():
app = FaceAnalysis(name=opt.model, providers=ort.get_available_providers())
app.prepare(ctx_id=0, det_size=(640, 640))
img1 = cv.imread(opt.img1)
img2 = cv.imread(opt.img2)
img1_rgb = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
img3_rgb = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
result1 = app.get(img1_rgb)[0]
result2 = app.get(img3_rgb)[0]
embedding1 = result1['embedding']
embedding2 = result2['embedding']
img1 = ul.draw_info(img1, result1)
img2 = ul.draw_info(img2, result2)
distance = round(ul.calc_distance(embedding1, embedding2), 4)
text = ''
if distance <= opt.thresh:
text = f'Same Person With This Distance: {distance}'
print('\n\n👍 ' + text)
else:
text = f'Different Person With This Distance: {distance}'
print('\n\n👎 ' + text)
if opt.show:
img1 = cv.resize(img1, (640, 640))
img2 = cv.resize(img2, (640, 640))
main_img = ul.stack_images([img1, img2], 2, 1.)
h, w = main_img.shape[:2]
cv.putText(main_img, text, (w // 2 - 350, h // 2 + 150), cv.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
cv.imshow('result', main_img)
cv.waitKey()
if __name__ == '__main__':
main()