Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load trained YOLO11 model
|
| 9 |
+
model_path = "model.pt" # Ensure this is uploaded to your HF Space
|
| 10 |
+
model = YOLO(model_path)
|
| 11 |
+
|
| 12 |
+
# Define inference function
|
| 13 |
+
def segment_card(image):
|
| 14 |
+
image = np.array(image) # Convert PIL image to numpy array
|
| 15 |
+
results = model(image) # Run YOLO model
|
| 16 |
+
|
| 17 |
+
# Extract bounding boxes
|
| 18 |
+
for result in results:
|
| 19 |
+
for box in result.boxes:
|
| 20 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coords
|
| 21 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw box
|
| 22 |
+
|
| 23 |
+
return Image.fromarray(image) # Convert back to PIL image
|
| 24 |
+
|
| 25 |
+
# Create Gradio UI
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=segment_card,
|
| 28 |
+
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=gr.Image(type="pil"),
|
| 30 |
+
title="MTG Card Segmentation with YOLO11",
|
| 31 |
+
description="Upload a Magic: The Gathering card image, and the model will segment key visual elements."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the app
|
| 35 |
+
iface.launch()
|