Spaces:
Running
Running
File size: 1,070 Bytes
8cff4cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # services/image_utils.py
"""
Lightweight image helpers — no ML imports here.
"""
import cv2
import numpy as np
from config.settings import MAX_IMAGE_SIZE
def optimize_image(img: np.ndarray, max_size: int = MAX_IMAGE_SIZE) -> np.ndarray:
"""Downscale image so the longest side ≤ max_size."""
h, w = img.shape[:2]
if max(h, w) > max_size:
scale = max_size / max(h, w)
new_w = int(w * scale)
new_h = int(h * scale)
img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
return img
def detect_skin(image: np.ndarray, threshold: float = 0.05) -> bool:
"""Return True when ≥ threshold fraction of pixels look like skin (HSV heuristic)."""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_skin = np.array([0, 20, 70], dtype=np.uint8)
upper_skin = np.array([20, 255, 255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_skin, upper_skin)
skin_ratio = cv2.countNonZero(mask) / (image.shape[0] * image.shape[1])
return skin_ratio > threshold
|