|
import gradio as gr
|
|
from huggingface_hub import hf_hub_download
|
|
from ultralytics import YOLO
|
|
from PIL import Image
|
|
|
|
|
|
REPO_ID = "shaquilledanil/yolo11-skinai"
|
|
FILENAME = "best.pt"
|
|
WEIGHT_PATH = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
|
|
|
|
|
model = YOLO(WEIGHT_PATH)
|
|
|
|
def yolo_inference(image: Image.Image):
|
|
"""
|
|
Принимает PIL.Image, прогоняет через YOLO-модель,
|
|
возвращает кортеж (annotated_image, detections_info).
|
|
"""
|
|
results = model.predict(source=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
|
|
|
|
|
|
|
|
|
|
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()
|
|
|