-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_verification.py
39 lines (30 loc) · 1.43 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
import argparse
import numpy as np
import cv2 as cv
from insightface.app import FaceAnalysis
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
def face_verification(image_1, image_2, threshold):
image_1 = cv.imread(image_1)
image_1 = cv.cvtColor(image_1, cv.COLOR_BGR2RGB)
image_2 = cv.imread(image_2)
image_2 = cv.cvtColor(image_2, cv.COLOR_BGR2RGB)
result_1 = app.get(image_1)
result_2 = app.get(image_2)
if len(result_1) == 1 and len(result_2) == 1:
result_1 = result_1[0]['embedding']
result_2 = result_2[0]['embedding']
distance = np.sqrt(np.sum((result_1 - result_2)) ** 2)
if distance < threshold:
print(f'Face Verification: True, Distance: {distance}')
else:
print(f'Face Verification: False, Distance: {distance}')
else:
print('Images no have face or have more than one face')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--image_1', type=str, help='Image 1 path', default='./verification_test_images/img_1.jpg')
parser.add_argument('--image_2', type=str, help='Image 2 path', default='./verification_test_images/img_2.jpg')
parser.add_argument('--threshold', type=int, help='Threshold for similar faces', default=25)
args = parser.parse_args()
face_verification(args.image_1, args.image_2, args.threshold)