File size: 4,226 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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