Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def load_models(models_dir: str) -> ModelBundle:
raise ImportError("insightface is not installed. Run: pip install insightface") from e

app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
app.prepare(ctx_id=0, det_size=(1024, 1024))
app.prepare(ctx_id=0, det_size=(640, 640))
logger.info("FaceAnalysis ready.")

logger.info("Loading inswapper_128…")
Expand Down
17 changes: 17 additions & 0 deletions pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
print(stats)
"""

import gc
import logging
import os
import shutil
Expand All @@ -47,6 +48,10 @@

SUPPORTED_EXTENSIONS = (".jpg", ".jpeg", ".png", ".tiff", ".webp")

# Maximum pixel dimension (longest edge) before the image is downscaled.
# Override with the REFACER_MAX_DIMENSION environment variable.
MAX_DIMENSION = int(os.environ.get("REFACER_MAX_DIMENSION", "4096"))


# ---------------------------------------------------------------------------
# Result types
Expand Down Expand Up @@ -216,6 +221,17 @@ def _process_image(
logger.error("Could not read %s, skipping.", filename)
return result

# --- Downscale if needed ---
h, w = img.shape[:2]
if max(h, w) > MAX_DIMENSION:
scale = MAX_DIMENSION / max(h, w)
new_w, new_h = int(w * scale), int(h * scale)
logger.warning(
"%s — %dx%d exceeds MAX_DIMENSION=%d, downscaling to %dx%d",
filename, w, h, MAX_DIMENSION, new_w, new_h,
)
img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)

# --- Detect faces ---
try:
faces = models.app.get(img)
Expand Down Expand Up @@ -333,6 +349,7 @@ def run(
for filename in filenames:
logger.info("── Processing: %s", filename)
image_result = _process_image(filename, input_dir, output_dir, models)
gc.collect()
stats.image_results.append(image_result)

stats.total_faces += image_result.faces_detected
Expand Down
Loading