Spaces:
Running
Running
Download services/image_utils.py from RabiHmedtak/API_RapidAPI_test: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/services/image_utils.py
- Command line
-
hf download hf://spaces/RabiHmedtak/API_RapidAPI_test/services/image_utils.py
-
curl -L -o image_utils.py https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/services/image_utils.py
1.07 kB
| # 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 | |