File size: 2,167 Bytes
51829a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
from PIL import Image

# Скачиваем веса из вашего репо на Hugging Face
REPO_ID = "shaquilledanil/yolo11-skinai"
FILENAME = "best.pt"
WEIGHT_PATH = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)

# Загружаем YOLO-модель
model = YOLO(WEIGHT_PATH)

def yolo_inference(image: Image.Image):
    """

    Принимает PIL.Image, прогоняет через YOLO-модель, 

    возвращает кортеж (annotated_image, detections_info).

    """
    results = model.predict(source=image)

    # Подготовим изображение с нарисованными bboxes
    # Ultralytics в results[0].plot() вернёт PIL.Image с рамками
    annotated_image = results[0].plot() if len(results) else image

    # Соберём текстовую информацию о детекциях
    detections = []
    if len(results):
        boxes = results[0].boxes
        for box in boxes:
            cls_id = int(box.cls[0].item())
            conf = float(box.conf[0].item())
            xyxy = box.xyxy[0].tolist()
            class_name = results[0].names[cls_id]
            detections.append({
                "class_id": cls_id,
                "class_name": class_name,
                "confidence": round(conf, 3),
                "bbox": [round(x, 1) for x in xyxy]
            })

    return annotated_image, detections

# Создаём Gradio-интерфейс:
#  - inputs: одна картинка
#  - outputs: (1) картинка с боксами, (2) JSON (список детекций)
demo = gr.Interface(
    fn=yolo_inference,
    inputs=gr.Image(type="pil"),
    outputs=[
        gr.Image(type="pil", label="Detected Objects"), 
        "json"
    ],
    title="YOLO SkinAI Demo",
    description="Загрузите изображение, чтобы увидеть результат детекции"
)

# Запуск (при локальном тесте):
if __name__ == "__main__":
    demo.launch()