# services/inference.py """ Runs YOLO detection and skin-type classification in parallel threads. Returns (detections, skin_type_result). """ import time import threading from typing import Optional import cv2 import numpy as np import torch from PIL import Image from models.loader import yolo_model, skin_type_model, skin_transform, device def run_inference( img: np.ndarray, class_translations: dict, skin_type_classes: dict, yolo_timeout: float = 15.0, ) -> tuple[list, dict]: """ Parameters ---------- img : BGR numpy image (already resized) class_translations : lang-specific label map skin_type_classes : lang-specific skin-type label map Returns ------- detections : list of detection dicts skin_type_result : dict with skin_type + probabilities """ detections: list = [] skin_type_result: dict = {} errors: dict = {} # ── YOLO thread ──────────────────────────────────────────────────────────── def run_yolo(): try: t = time.time() results = yolo_model(img, conf=0.15, iou=0.2)[0] for box in results.boxes: x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int) confidence = float(box.conf[0]) class_id = int(box.cls[0]) class_name = results.names[class_id] translated = class_translations.get(class_name, class_name) detections.append({ 'class_id': class_id, 'class_name': translated, 'confidence': confidence, 'bbox': [int(x1), int(y1), int(x2), int(y2)], }) print(f"[inference] YOLO: {time.time()-t:.2f}s — {len(detections)} detections") except Exception as exc: errors['yolo'] = str(exc) print(f"[inference] YOLO error: {exc}") # ── Skin-type thread ─────────────────────────────────────────────────────── def run_skin_type(): try: t = time.time() rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) tensor = skin_transform(rgb).unsqueeze(0).to(device) with torch.no_grad(): output = skin_type_model(tensor) probs = torch.nn.functional.softmax(output, dim=1)[0] pred = output.argmax(1).item() skin_type_result.update({ 'skin_type': skin_type_classes[pred], 'probabilities': { 'dry': float(probs[0] * 100), 'normal': float(probs[1] * 100), 'oily': float(probs[2] * 100), }, }) print(f"[inference] Skin-type: {time.time()-t:.2f}s") except Exception as exc: errors['skin_type'] = str(exc) print(f"[inference] Skin-type error: {exc}") # ── Run in parallel ──────────────────────────────────────────────────────── yolo_thread = threading.Thread(target=run_yolo, daemon=True) skin_thread = threading.Thread(target=run_skin_type, daemon=True) yolo_thread.start() skin_thread.start() yolo_thread.join(timeout=yolo_timeout) skin_thread.join(timeout=yolo_timeout) # ── Fallbacks on failure ─────────────────────────────────────────────────── if 'yolo' in errors or not detections: detections = [] if 'skin_type' in errors or not skin_type_result: skin_type_result = { 'skin_type': skin_type_classes[1], # default: Normal 'probabilities': {'dry': 0.0, 'normal': 100.0, 'oily': 0.0}, } return detections, skin_type_result