facial area coordinates must be in image borders

This commit is contained in:
Sefik Ilkin Serengil 2024-08-15 08:15:15 +01:00
parent f3d1809a9f
commit 30591701a7
3 changed files with 33 additions and 5 deletions

View File

@ -85,7 +85,9 @@ def extract_faces(
if img is None:
raise ValueError(f"Exception while loading {img_name}")
base_region = FacialAreaRegion(x=0, y=0, w=img.shape[1], h=img.shape[0], confidence=0)
height, width, _ = img.shape
base_region = FacialAreaRegion(x=0, y=0, w=width, h=height, confidence=0)
if detector_backend == "skip":
face_objs = [DetectedFace(img=img, facial_area=base_region, confidence=0)]
@ -137,10 +139,11 @@ def extract_faces(
if normalize_face:
current_img = current_img / 255 # normalize input in [0, 1]
x = int(current_region.x)
y = int(current_region.y)
w = int(current_region.w)
h = int(current_region.h)
# cast to int for flask, and do final checks for borders
x = max(0, int(current_region.x))
y = max(0, int(current_region.y))
w = min(width - x - 1, int(current_region.w))
h = min(height - y - 1, int(current_region.h))
resp_obj = {
"face": current_img,

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

View File

@ -98,3 +98,28 @@ def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
return "data:image/jpeg," + encoded_string
def test_facial_coordinates_are_in_borders():
img_path = "dataset/selfie-many-people.jpg"
img = cv2.imread(img_path)
height, width, _ = img.shape
results = DeepFace.extract_faces(img_path=img_path)
assert len(results) > 0
for result in results:
facial_area = result["facial_area"]
x = facial_area["x"]
y = facial_area["y"]
w = facial_area["w"]
h = facial_area["h"]
assert x >= 0
assert y >= 0
assert x + w < width
assert y + h < height
logger.info("✅ facial area coordinates are all in image borders")