44
55# 3rd party dependencies
66import numpy as np
7+ from lightphe import LightPHE
78
89# project dependencies
910from deepface .commons import image_utils
1011from deepface .modules import modeling , detection , preprocessing
1112from deepface .models .FacialRecognition import FacialRecognition
1213from deepface .modules .normalization import normalize_embedding_l2 , normalize_embedding_minmax
14+ from deepface .modules .encryption import encrypt_embeddings
15+ from deepface .commons .logger import Logger
16+
17+ logger = Logger ()
1318
1419
1520def represent (
@@ -24,6 +29,7 @@ def represent(
2429 max_faces : Optional [int ] = None ,
2530 l2_normalize : bool = False ,
2631 minmax_normalize : bool = False ,
32+ cryptosystem : Optional [LightPHE ] = None ,
2733) -> Union [List [Dict [str , Any ]], List [List [Dict [str , Any ]]]]:
2834 """
2935 Represent facial images as multi-dimensional vector embeddings.
@@ -63,6 +69,10 @@ def represent(
6369 minmax_normalize (bool): Flag to enable min-max normalization of the output embeddings
6470 to the range [0, 1].
6571
72+ cryptosystem (LightPHE): An instance of a partially homomorphic encryption system
73+ to encrypt the output embeddings. If provided, the embeddings will be encrypted
74+ using the specified cryptosystem.
75+
6676 Returns:
6777 results (List[Dict[str, Any]] or List[Dict[str, Any]]): A list of dictionaries.
6878 Result type becomes List of List of Dict if batch input passed.
@@ -77,6 +87,8 @@ def represent(
7787 the full image area and is nonsensical.
7888 - face_confidence (float): Confidence score of face detection. If `detector_backend` is set
7989 to 'skip', the confidence will be 0 and is nonsensical.
90+ - encrypted_embedding (List[Any]): Encrypted multidimensional vector representing
91+ facial features. This field is included only if a `cryptosystem` is provided.
8092 """
8193 resp_objs = []
8294
@@ -178,15 +190,20 @@ def represent(
178190 if l2_normalize :
179191 embeddings = normalize_embedding_l2 (embeddings )
180192
193+ encrypted_embeddings = encrypt_embeddings (embeddings , cryptosystem )
194+
181195 resp_objs_dict = defaultdict (list )
182196 for idy , batch_index in enumerate (batch_indexes ):
183- resp_objs_dict [batch_index ].append (
184- {
185- "embedding" : embeddings if len (batch_images ) == 1 else embeddings [idy ],
186- "facial_area" : batch_regions [idy ],
187- "face_confidence" : batch_confidences [idy ],
188- }
189- )
197+ resp_obj = {
198+ "embedding" : embeddings if len (batch_images ) == 1 else embeddings [idy ],
199+ "facial_area" : batch_regions [idy ],
200+ "face_confidence" : batch_confidences [idy ],
201+ }
202+ if cryptosystem is not None and encrypted_embeddings is not None :
203+ resp_obj ["encrypted_embedding" ] = (
204+ encrypted_embeddings if len (batch_images ) == 1 else encrypted_embeddings [idy ]
205+ )
206+ resp_objs_dict [batch_index ].append (resp_obj )
190207
191208 resp_objs = [resp_objs_dict [idx ] for idx in range (len (images ))]
192209
0 commit comments