Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Datajuicer Data juicer ImageFaceRatioFilter

From Leeroopedia
Knowledge Sources
Domains Data_Quality, Filtering
Last Updated 2026-02-14 16:00 GMT

Overview

Concrete tool for filtering data samples based on face area ratio in images provided by Data-Juicer.

Description

ImageFaceRatioFilter is a filter operator that keeps samples with face area ratios within a specific range. It computes the ratio of the largest face area to the total image area using an OpenCV Haar cascade classifier (default: haarcascade_frontalface_alt.xml). The key metric face_ratios is cached in the stats field. The operator supports 'any' (keep if any image meets the condition) and 'all' (keep only if all images meet the condition) strategies. It extends the Filter base class and implements the two-phase compute_stats/process pattern.

Usage

Import this operator when you need to filter dataset samples based on how much of the image is occupied by faces. Configure it in your Data-Juicer YAML config or instantiate directly.

Code Reference

Source Location

Signature

@UNFORKABLE.register_module("image_face_ratio_filter")
@OPERATORS.register_module("image_face_ratio_filter")
@LOADED_IMAGES.register_module("image_face_ratio_filter")
class ImageFaceRatioFilter(Filter):
    def __init__(
        self,
        cv_classifier: str = "",
        min_ratio: float = 0.0,
        max_ratio: float = 0.4,
        any_or_all: str = "any",
        *args,
        **kwargs,
    ):
        ...

Import

from data_juicer.ops.filter.image_face_ratio_filter import ImageFaceRatioFilter

I/O Contract

Inputs

Name Type Required Description
cv_classifier str No OpenCV classifier path for face detection. Default: haarcascade_frontalface_alt.xml
min_ratio float No Minimum ratio for the largest face area in an image. Default: 0.0
max_ratio float No Maximum ratio for the largest face area in an image. Default: 0.4
any_or_all str No Keep strategy: 'any' or 'all' across images. Default: "any"

Outputs

Name Type Description
samples Dict Filtered samples with stats field updated (face_ratios)

Usage Examples

YAML Configuration

process:
  - image_face_ratio_filter:
      min_ratio: 0.0
      max_ratio: 0.4
      any_or_all: "any"

Python API

from data_juicer.ops.filter.image_face_ratio_filter import ImageFaceRatioFilter

op = ImageFaceRatioFilter(min_ratio=0.0, max_ratio=0.4)
# Apply to dataset
result = dataset.process(op)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment