Skip to content

Commit

Permalink
Enhance/face area filter (#202)
Browse files Browse the repository at this point in the history
* Constrain the detection coordinates within the image
* Add debug logging
  • Loading branch information
drcege authored Jan 30, 2024
1 parent 543f36c commit 77c7559
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions data_juicer/ops/filter/face_area_filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from jsonargparse.typing import ClosedUnitInterval
from loguru import logger

from data_juicer.utils.availability_utils import AvailabilityChecking
from data_juicer.utils.constant import Fields, StatsKeys
Expand Down Expand Up @@ -81,16 +82,21 @@ def compute_stats(self, sample, context=False):
img = pil_to_opencv(image)
dets = self.detector(img, **self.detector_kwargs)
face_detections[key] = [[
det.left(), det.top(),
det.width(), det.height()
max(det.left(), 0),
max(det.top(), 0),
min(det.right(), image.width),
min(det.bottom(), image.height)
] for det in dets]
logger.debug(f'detections: {face_detections}')

# compute face area ratios for each image considering the largest face
face_area_ratios = {}
for key, dets in face_detections.items():
image_area = images[key].width * images[key].height
face_area_ratios[key] = max(
[w * h / image_area for _, _, w, h in dets], default=0.0)
face_area_ratios[key] = max([(x2 - x1) * (y2 - y1)
for x1, y1, x2, y2 in dets],
default=0.0) / image_area
logger.debug(f'ratios: {face_area_ratios}')

sample[Fields.stats][StatsKeys.face_ratios] = [
face_area_ratios[key] for key in loaded_image_keys
Expand Down

0 comments on commit 77c7559

Please sign in to comment.