sface class

This commit is contained in:
Sefik Ilkin Serengil 2022-05-10 22:39:54 +01:00
parent b15294e0d6
commit 631cf64236
4 changed files with 51 additions and 56 deletions

View File

@ -190,7 +190,7 @@ Face recognition, facial attribute analysis and vector representation functions
**Command Line Interface**
DeepFace comes with a command line interface as well. You are able to access its functions in command line in the command line as shown below. It expects the function name as 1st argument and function arguments respectively.
DeepFace comes with a command line interface as well. You are able to access its functions in command line as shown below. The command deepface expects the function name as 1st argument and function arguments thereafter.
```shell
deepface verify -img1_path tests/dataset/img1.jpg -img2_path tests/dataset/img2.jpg

View File

@ -13,7 +13,7 @@ from tqdm import tqdm
import pickle
import fire
from deepface.basemodels import VGGFace, OpenFace, Facenet, Facenet512, FbDeepFace, DeepID, DlibWrapper, ArcFace, Boosting, SFaceWrapper
from deepface.basemodels import VGGFace, OpenFace, Facenet, Facenet512, FbDeepFace, DeepID, DlibWrapper, ArcFace, SFace, Boosting
from deepface.extendedmodels import Age, Gender, Race, Emotion
from deepface.commons import functions, realtime, distance as dst
@ -47,7 +47,7 @@ def build_model(model_name):
'DeepID': DeepID.loadModel,
'Dlib': DlibWrapper.loadModel,
'ArcFace': ArcFace.loadModel,
'SFace': SFaceWrapper.load_model,
'SFace': SFace.load_model,
'Emotion': Emotion.loadModel,
'Age': Age.loadModel,
'Gender': Gender.loadModel,

View File

@ -0,0 +1,48 @@
import os
import numpy as np
import cv2 as cv
import gdown
from deepface.commons import functions
class _Layer:
input_shape = (None, 112, 112, 3)
output_shape = (None, 1, 128)
class SFaceModel:
def __init__(self, model_path):
self.model = cv.FaceRecognizerSF.create(
model = model_path,
config = "",
backend_id = 0,
target_id = 0)
self.layers = [_Layer()]
def predict(self, image):
# Preprocess
input_blob = (image[0] * 255).astype(np.uint8) # revert the image to original format and preprocess using the model
# Forward
embeddings = self.model.feature(input_blob)
return embeddings
def load_model(url = "https://github.com/opencv/opencv_zoo/raw/master/models/face_recognition_sface/face_recognition_sface_2021dec.onnx"):
home = functions.get_deepface_home()
file_name = home + '/.deepface/weights/face_recognition_sface_2021dec.onnx'
if not os.path.isfile(file_name):
print("sface weights will be downloaded...")
gdown.download(url, file_name, quiet=False)
model = SFaceModel(model_path = file_name)
return model

View File

@ -1,53 +0,0 @@
import os
import numpy as np
import cv2 as cv
import gdown
from deepface.commons import functions
class _Layer:
input_shape = (None, 112, 112, 3)
output_shape = (None, 1, 128)
class SFace:
def __init__(self, model_path, backend_id=0, target_id=0):
self._modelPath = model_path
self._backendId = backend_id
self._targetId = target_id
self._model = cv.FaceRecognizerSF.create(
model=self._modelPath,
config="",
backend_id=self._backendId,
target_id=self._targetId)
self.layers = [_Layer()]
def _preprocess(self, image, bbox):
if bbox is None:
return image
else:
return self._model.alignCrop(image, bbox)
def predict(self, image, bbox=None, **kwargs):
# Preprocess
image = (image[0] * 255).astype(np.uint8) # revert the image to original format and preprocess using the model
input_blob = self._preprocess(image, bbox)
# Forward
features = self._model.feature(input_blob)
return features
def load_model(url = "https://github.com/opencv/opencv_zoo/raw/master/models/face_recognition_sface/face_recognition_sface_2021dec.onnx", *args, **kwargs):
home = functions.get_deepface_home()
file_name = home + '/.deepface/weights/face_recognition_sface_2021dec.onnx'
if not os.path.isfile(file_name):
print("sface weights will be downloaded...")
output = file_name
gdown.download(url, output, quiet=False)
model = SFace(file_name, 0, 0)
return model