Merge pull request #1427 from dakotah-jones/patch-1

Update representation.py
This commit is contained in:
Sefik Ilkin Serengil 2025-02-19 11:43:16 +00:00 committed by GitHub
commit 8ed6b7c7c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 1 deletions

View File

@ -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:

View File

@ -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"]

View File

@ -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",
[