Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
REPO_ID = "harpreetsahota/car-dd-segmentation-yolov11"
|
| 8 |
+
FILENAME = "best.pt"
|
| 9 |
+
|
| 10 |
+
# Télécharge best.pt depuis Hugging Face (cache automatique)
|
| 11 |
+
weights_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 12 |
+
|
| 13 |
+
# Charge le modèle YOLO segmentation
|
| 14 |
+
model = YOLO(weights_path)
|
| 15 |
+
|
| 16 |
+
def predict(img: Image.Image, conf=0.25, imgsz=1280):
|
| 17 |
+
# Inference
|
| 18 |
+
results = model(img, conf=conf, imgsz=imgsz, verbose=False)
|
| 19 |
+
|
| 20 |
+
# Image annotée (masks + boxes + labels) -> numpy BGR
|
| 21 |
+
annotated_bgr = results[0].plot()
|
| 22 |
+
|
| 23 |
+
# BGR -> RGB -> PIL
|
| 24 |
+
annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
|
| 25 |
+
return Image.fromarray(annotated_rgb)
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Image(type="pil", label="Image voiture"),
|
| 31 |
+
gr.Slider(0.05, 0.9, value=0.25, step=0.05, label="Confidence (conf)"),
|
| 32 |
+
gr.Slider(640, 1536, value=1280, step=64, label="Taille image (imgsz)")
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Image(type="pil", label="Résultat (segmentation YOLO)"),
|
| 35 |
+
title="Car Damage Segmentation (YOLO)",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|