Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
inputs=gr.Image(type="pil"),
|
| 11 |
-
outputs="
|
| 12 |
-
title="Image to Figma Layers",
|
| 13 |
-
description="Upload a PNG or JPEG image
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
|
| 6 |
+
# Load the DETR layout model
|
| 7 |
+
model_name = "cmarkea/detr-layout-detection"
|
| 8 |
+
processor = DetrImageProcessor.from_pretrained(model_name)
|
| 9 |
+
model = DetrForObjectDetection.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def detect_layout(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
outputs = model(**inputs)
|
| 14 |
+
target_sizes = torch.tensor([image.size[::-1]]) # H x W
|
| 15 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
|
| 16 |
+
|
| 17 |
+
draw = ImageDraw.Draw(image)
|
| 18 |
+
labels = []
|
| 19 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 20 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 21 |
+
draw.rectangle(box, outline="red", width=2)
|
| 22 |
+
label_name = model.config.id2label[label.item()]
|
| 23 |
+
draw.text((box[0] + 4, box[1]), f"{label_name} ({round(score.item(), 2)})", fill="red")
|
| 24 |
+
labels.append({"label": label_name, "score": round(score.item(), 2), "box": box})
|
| 25 |
+
|
| 26 |
+
return image, labels
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=detect_layout,
|
| 30 |
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs=[gr.Image(type="pil"), gr.JSON()],
|
| 32 |
+
title="Image to Figma Layers (with DETR)",
|
| 33 |
+
description="Upload a PNG or JPEG UI image to detect editable layers using a layout-aware DETR model."
|
| 34 |
)
|
| 35 |
|
| 36 |
+
iface.launch()
|