JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
sface.py
Go to the documentation of this file.
1# This file is part of OpenCV Zoo project.
2# It is subject to the license terms in the LICENSE file found in the same directory.
3#
4# Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5# Third party copyrights are property of their respective owners.
6
7import numpy as np
8import cv2 as cv
9
10from _testcapi import FLT_MIN
11
12class SFace:
13 def __init__(self, modelPath, disType=0, backendId=0, targetId=0):
14 self._modelPath = modelPath
15 self._backendId = backendId
16 self._targetId = targetId
17 self._model = cv.FaceRecognizerSF.create(
18 model=self._modelPath,
19 config="",
20 backend_id=self._backendId,
21 target_id=self._targetId)
22
23 self._disType = disType # 0: cosine similarity, 1: Norm-L2 distance
24 assert self._disType in [0, 1], "0: Cosine similarity, 1: norm-L2 distance, others: invalid"
25
26 self._threshold_cosine = 0.363
27 self._threshold_norml2 = 1.128
28
29 @property
30 def name(self):
31 return self.__class__.__name__
32
33 def setBackend(self, backendId):
34 self._backendId = backendId
35 self._model = cv.FaceRecognizerSF.create(
36 model=self._modelPath,
37 config="",
38 backend_id=self._backendId,
39 target_id=self._targetId)
40
41 def setTarget(self, targetId):
42 self._targetId = targetId
43 self._model = cv.FaceRecognizerSF.create(
44 model=self._modelPath,
45 config="",
46 backend_id=self._backendId,
47 target_id=self._targetId)
48
49 def _preprocess(self, image, bbox):
50 if bbox is None:
51 return image
52 else:
53 return self._model.alignCrop(image, bbox)
54
55 def infer(self, image, bbox=None):
56 # Preprocess
57 inputBlob = self._preprocess(image, bbox)
58
59 # Forward
60 features = self._model.feature(inputBlob)
61 return features
62
63 def match(self, image1, face1, image2, face2):
64 feature1 = self.infer(image1, face1)
65 feature2 = self.infer(image2, face2)
66
67 if self._disType == 0: # COSINE
68 cosine_score = self._model.match(feature1, feature2, self._disType)
69 return 1 if cosine_score >= self._threshold_cosine else 0
70 else: # NORM_L2
71 norml2_distance = self._model.match(feature1, feature2, self._disType)
72 return 1 if norml2_distance <= self._threshold_norml2 else 0
73
setBackend(self, backendId)
Definition sface.py:33
_preprocess(self, image, bbox)
Definition sface.py:49
_threshold_cosine
Definition sface.py:26
setTarget(self, targetId)
Definition sface.py:41
__init__(self, modelPath, disType=0, backendId=0, targetId=0)
Definition sface.py:13
infer(self, image, bbox=None)
Definition sface.py:55
name(self)
Definition sface.py:30
match(self, image1, face1, image2, face2)
Definition sface.py:63
_threshold_norml2
Definition sface.py:27