Skip to content

Commit 0fd77e1

Browse files
committed
facial recognition, detector and demography models are now using interface
1 parent 51bb180 commit 0fd77e1

35 files changed

+1223
-1065
lines changed

deepface/DeepFace.py

+2-66
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import numpy as np
99
import pandas as pd
1010
import tensorflow as tf
11-
from deprecated import deprecated
1211

1312
# package dependencies
1413
from deepface.commons import functions
@@ -23,8 +22,6 @@
2322
realtime,
2423
)
2524

26-
# pylint: disable=no-else-raise, simplifiable-if-expression
27-
2825
logger = Logger(module="DeepFace")
2926

3027
# -----------------------------------
@@ -40,6 +37,8 @@
4037
from keras.models import Model
4138
# -----------------------------------
4239

40+
functions.initialize_folder()
41+
4342

4443
def build_model(model_name: str) -> Union[Model, Any]:
4544
"""
@@ -413,69 +412,6 @@ def extract_faces(
413412
)
414413

415414

416-
# ---------------------------
417-
# deprecated functions
418-
419-
420-
@deprecated(version="0.0.78", reason="Use DeepFace.extract_faces instead of DeepFace.detectFace")
421-
def detectFace(
422-
img_path: Union[str, np.ndarray],
423-
target_size: tuple = (224, 224),
424-
detector_backend: str = "opencv",
425-
enforce_detection: bool = True,
426-
align: bool = True,
427-
) -> Union[np.ndarray, None]:
428-
"""
429-
Deprecated function. Use extract_faces for same functionality.
430-
431-
This function applies pre-processing stages of a face recognition pipeline
432-
including detection and alignment
433-
434-
Parameters:
435-
img_path: exact image path, numpy array (BGR) or base64 encoded image.
436-
Source image can have many face. Then, result will be the size of number
437-
of faces appearing in that source image.
438-
439-
target_size (tuple): final shape of facial image. black pixels will be
440-
added to resize the image.
441-
442-
detector_backend (string): face detection backends are retinaface, mtcnn,
443-
opencv, ssd or dlib
444-
445-
enforce_detection (boolean): function throws exception if face cannot be
446-
detected in the fed image. Set this to False if you do not want to get
447-
an exception and run the function anyway.
448-
449-
align (boolean): alignment according to the eye positions.
450-
451-
grayscale (boolean): extracting faces in rgb or gray scale
452-
453-
Returns:
454-
detected and aligned face as numpy array
455-
456-
"""
457-
logger.warn("Function detectFace is deprecated. Use extract_faces instead.")
458-
face_objs = extract_faces(
459-
img_path=img_path,
460-
target_size=target_size,
461-
detector_backend=detector_backend,
462-
enforce_detection=enforce_detection,
463-
align=align,
464-
grayscale=False,
465-
)
466-
467-
extracted_face = None
468-
if len(face_objs) > 0:
469-
extracted_face = face_objs[0]["face"]
470-
return extracted_face
471-
472-
473-
# ---------------------------
474-
# main
475-
476-
functions.initialize_folder()
477-
478-
479415
def cli() -> None:
480416
"""
481417
command line interface function will be offered in this block

deepface/basemodels/ArcFace.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tensorflow as tf
44
from deepface.commons import functions
55
from deepface.commons.logger import Logger
6+
from deepface.models.FacialRecognition import FacialRecognition
67

78
logger = Logger(module="basemodels.ArcFace")
89

@@ -42,10 +43,25 @@
4243
Dense,
4344
)
4445

46+
# pylint: disable=too-few-public-methods
47+
class ArcFace(FacialRecognition):
48+
"""
49+
ArcFace model class
50+
"""
4551

46-
def loadModel(
52+
def __init__(self):
53+
self.model = load_model()
54+
self.model_name = "ArcFace"
55+
56+
57+
def load_model(
4758
url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5",
4859
) -> Model:
60+
"""
61+
Construct ArcFace model, download its weights and load
62+
Returns:
63+
model (Model)
64+
"""
4965
base_model = ResNet34()
5066
inputs = base_model.inputs[0]
5167
arcface_model = base_model.outputs[0]
@@ -81,7 +97,11 @@ def loadModel(
8197

8298

8399
def ResNet34() -> Model:
84-
100+
"""
101+
ResNet34 model
102+
Returns:
103+
model (Model)
104+
"""
85105
img_input = Input(shape=(112, 112, 3))
86106

87107
x = ZeroPadding2D(padding=1, name="conv1_pad")(img_input)

deepface/basemodels/DeepID.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tensorflow as tf
44
from deepface.commons import functions
55
from deepface.commons.logger import Logger
6+
from deepface.models.FacialRecognition import FacialRecognition
67

78
logger = Logger(module="basemodels.DeepID")
89

@@ -38,10 +39,23 @@
3839

3940
# -------------------------------------
4041

42+
# pylint: disable=too-few-public-methods
43+
class DeepId(FacialRecognition):
44+
"""
45+
DeepId model class
46+
"""
4147

42-
def loadModel(
48+
def __init__(self):
49+
self.model = load_model()
50+
self.model_name = "DeepId"
51+
52+
53+
def load_model(
4354
url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5",
4455
) -> Model:
56+
"""
57+
Construct DeepId model, download its weights and load
58+
"""
4559

4660
myInput = Input(shape=(55, 47, 3))
4761

deepface/basemodels/DlibResNet.py

+21
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@
44
import numpy as np
55
from deepface.commons import functions
66
from deepface.commons.logger import Logger
7+
from deepface.models.FacialRecognition import FacialRecognition
78

89
logger = Logger(module="basemodels.DlibResNet")
910

1011
# pylint: disable=too-few-public-methods
1112

1213

14+
class Dlib(FacialRecognition):
15+
"""
16+
Dlib model class
17+
"""
18+
19+
def __init__(self):
20+
self.model = DlibResNet()
21+
self.model_name = "Dlib"
22+
23+
def find_embeddings(self, img: np.ndarray) -> list:
24+
"""
25+
Custom find embeddings function of Dlib different than FacialRecognition's one
26+
Args:
27+
img (np.ndarray)
28+
Retunrs:
29+
embeddings (list)
30+
"""
31+
return self.model.predict(img)[0].tolist()
32+
33+
1334
class DlibResNet:
1435
def __init__(self):
1536

deepface/basemodels/DlibWrapper.py

-6
This file was deleted.

deepface/basemodels/Facenet.py

+67-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tensorflow as tf
44
from deepface.commons import functions
55
from deepface.commons.logger import Logger
6+
from deepface.models.FacialRecognition import FacialRecognition
67

78
logger = Logger(module="basemodels.Facenet")
89

@@ -42,12 +43,39 @@
4243

4344
# --------------------------------
4445

46+
# pylint: disable=too-few-public-methods
47+
class FaceNet128d(FacialRecognition):
48+
"""
49+
FaceNet-128d model class
50+
"""
51+
52+
def __init__(self):
53+
self.model = load_facenet128d_model()
54+
self.model_name = "FaceNet-128d"
55+
56+
57+
class FaceNet512d(FacialRecognition):
58+
"""
59+
FaceNet-1512d model class
60+
"""
61+
62+
def __init__(self):
63+
self.model = load_facenet512d_model()
64+
self.model_name = "FaceNet-512d"
65+
4566

4667
def scaling(x, scale):
4768
return x * scale
4869

4970

50-
def InceptionResNetV2(dimension=128) -> Model:
71+
def InceptionResNetV2(dimension: int = 128) -> Model:
72+
"""
73+
InceptionResNetV2 model
74+
Args:
75+
dimension (int): number of dimensions in the embedding layer
76+
Returns:
77+
model (Model)
78+
"""
5179

5280
inputs = Input(shape=(160, 160, 3))
5381
x = Conv2D(32, 3, strides=2, padding="valid", use_bias=False, name="Conv2d_1a_3x3")(inputs)
@@ -1618,9 +1646,16 @@ def InceptionResNetV2(dimension=128) -> Model:
16181646
return model
16191647

16201648

1621-
def loadModel(
1649+
def load_facenet128d_model(
16221650
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facenet_weights.h5",
16231651
) -> Model:
1652+
"""
1653+
Construct FaceNet-128d model, download weights and then load weights
1654+
Args:
1655+
dimension (int): construct FaceNet-128d or FaceNet-512d models
1656+
Returns:
1657+
model (Model)
1658+
"""
16241659
model = InceptionResNetV2()
16251660

16261661
# -----------------------------------
@@ -1640,3 +1675,33 @@ def loadModel(
16401675
# -----------------------------------
16411676

16421677
return model
1678+
1679+
1680+
def load_facenet512d_model(
1681+
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facenet512_weights.h5",
1682+
) -> Model:
1683+
"""
1684+
Construct FaceNet-512d model, download its weights and load
1685+
Returns:
1686+
model (Model)
1687+
"""
1688+
1689+
model = InceptionResNetV2(dimension=512)
1690+
1691+
# -------------------------
1692+
1693+
home = functions.get_deepface_home()
1694+
1695+
if os.path.isfile(home + "/.deepface/weights/facenet512_weights.h5") != True:
1696+
logger.info("facenet512_weights.h5 will be downloaded...")
1697+
1698+
output = home + "/.deepface/weights/facenet512_weights.h5"
1699+
gdown.download(url, output, quiet=False)
1700+
1701+
# -------------------------
1702+
1703+
model.load_weights(home + "/.deepface/weights/facenet512_weights.h5")
1704+
1705+
# -------------------------
1706+
1707+
return model

deepface/basemodels/Facenet512.py

-40
This file was deleted.

deepface/basemodels/FbDeepFace.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import tensorflow as tf
55
from deepface.commons import functions
66
from deepface.commons.logger import Logger
7+
from deepface.models.FacialRecognition import FacialRecognition
78

89
logger = Logger(module="basemodels.FbDeepFace")
910

@@ -35,12 +36,23 @@
3536

3637

3738
# -------------------------------------
38-
# pylint: disable=line-too-long
39+
# pylint: disable=line-too-long, too-few-public-methods
40+
class DeepFace(FacialRecognition):
41+
"""
42+
Fb's DeepFace model class
43+
"""
3944

45+
def __init__(self):
46+
self.model = load_model()
47+
self.model_name = "DeepFace"
4048

41-
def loadModel(
49+
50+
def load_model(
4251
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",
4352
) -> Model:
53+
"""
54+
Construct DeepFace model, download its weights and load
55+
"""
4456
base_model = Sequential()
4557
base_model.add(
4658
Convolution2D(32, (11, 11), activation="relu", name="C1", input_shape=(152, 152, 3))

0 commit comments

Comments
 (0)