diff --git a/deepface/modules/recognition.py b/deepface/modules/recognition.py index 90e8c29..4002b47 100644 --- a/deepface/modules/recognition.py +++ b/deepface/modules/recognition.py @@ -398,6 +398,7 @@ def __find_bulk_embeddings( enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, + color_face='bgr' # `represent` expects images in bgr format. ) except ValueError as err: diff --git a/deepface/modules/representation.py b/deepface/modules/representation.py index dc894df..2be4fec 100644 --- a/deepface/modules/representation.py +++ b/deepface/modules/representation.py @@ -87,6 +87,7 @@ def represent( # we have run pre-process in verification. so, skip if it is coming from verify. target_size = model.input_shape if detector_backend != "skip": + # Images are returned in RGB format. img_objs = detection.extract_faces( img_path=single_img_path, detector_backend=detector_backend, @@ -104,6 +105,9 @@ def represent( if len(img.shape) != 3: raise ValueError(f"Input img must be 3 dimensional but it is {img.shape}") + # Convert to RGB format to keep compatability with `extract_faces`. + img = img[:, :, ::-1] + # make dummy region and confidence to keep compatibility with `extract_faces` img_objs = [ { @@ -130,7 +134,7 @@ def represent( img = img_obj["face"] - # bgr to rgb + # rgb to bgr img = img[:, :, ::-1] region = img_obj["facial_area"] diff --git a/tests/test_represent.py b/tests/test_represent.py index 4e22a03..e5a7eab 100644 --- a/tests/test_represent.py +++ b/tests/test_represent.py @@ -111,6 +111,27 @@ def test_max_faces(): assert len(results) == max_faces +def test_represent_detector_backend(): + # Results using a detection backend. + results_1 = DeepFace.represent(img_path="dataset/img1.jpg") + assert len(results_1) == 1 + + # Results performing face extraction first. + faces = DeepFace.extract_faces(img_path="dataset/img1.jpg", color_face='bgr') + assert len(faces) == 1 + + # Images sent into represent need to be in BGR format. + img = faces[0]['face'] + results_2 = DeepFace.represent(img_path=img, detector_backend="skip") + assert len(results_2) == 1 + + # The embeddings should be the exact same for both cases. + embedding_1 = results_1[0]['embedding'] + embedding_2 = results_2[0]['embedding'] + assert embedding_1 == embedding_2 + logger.info("✅ test represent function for consistent output.") + + @pytest.mark.parametrize( "model_name", [