Spaces:
Sleeping
Sleeping
get raw output (#1)
Browse files- get raw output (0d5e20680c409953a7f3c807bbe5f524d5e1ce22)
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import cv2
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from typing import Tuple, List
|
|
|
|
| 11 |
from rfdetr.detr import RFDETRMedium
|
| 12 |
|
| 13 |
# UI Element classes
|
|
@@ -76,26 +77,47 @@ def draw_detections(
|
|
| 76 |
|
| 77 |
return img_with_boxes
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
@torch.inference_mode()
|
| 80 |
def detect_ui_elements(
|
| 81 |
image: Image.Image,
|
| 82 |
confidence_threshold: float,
|
| 83 |
line_thickness: int
|
| 84 |
-
) -> Tuple[Image.Image, str]:
|
| 85 |
"""
|
| 86 |
Detect UI elements in the uploaded image
|
| 87 |
-
|
| 88 |
Args:
|
| 89 |
image: Input PIL Image
|
| 90 |
confidence_threshold: Minimum confidence score for detections
|
| 91 |
line_thickness: Thickness of bounding box lines
|
| 92 |
-
|
| 93 |
Returns:
|
| 94 |
Annotated image and detection summary text
|
| 95 |
"""
|
| 96 |
try:
|
| 97 |
if image is None:
|
| 98 |
-
return None, "Please upload an image first."
|
| 99 |
|
| 100 |
# Load model
|
| 101 |
model = load_model()
|
|
@@ -130,20 +152,22 @@ def detect_ui_elements(
|
|
| 130 |
# Create summary text
|
| 131 |
summary_text = f"**Total detections:** {len(filtered_boxes)}"
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
import traceback
|
| 137 |
error_msg = f"**Error during detection:**\n\n```\n{str(e)}\n\n{traceback.format_exc()}\n```"
|
| 138 |
print(error_msg) # Also print to logs
|
| 139 |
-
return None, error_msg
|
|
|
|
| 140 |
|
| 141 |
# Gradio interface
|
| 142 |
with gr.Blocks(title="UI-DETR-1 UI Element Detector", theme=gr.themes.Soft()) as demo:
|
| 143 |
|
| 144 |
gr.Markdown("""
|
| 145 |
# UI-DETR-1 UI Element Detector
|
| 146 |
-
|
| 147 |
Upload a screenshot or UI mockup to automatically detect elements.
|
| 148 |
""")
|
| 149 |
|
|
@@ -185,14 +209,18 @@ with gr.Blocks(title="UI-DETR-1 UI Element Detector", theme=gr.themes.Soft()) as
|
|
| 185 |
|
| 186 |
summary_output = gr.Markdown(label="Detection Summary")
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# Connect button
|
| 190 |
detect_button.click(
|
| 191 |
fn=detect_ui_elements,
|
| 192 |
inputs=[input_image, confidence_slider, thickness_slider],
|
| 193 |
-
outputs=[output_image, summary_output]
|
| 194 |
)
|
| 195 |
|
| 196 |
# Launch
|
| 197 |
if __name__ == "__main__":
|
| 198 |
-
demo.queue().launch(share=False)
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from typing import Tuple, List
|
| 11 |
+
import json
|
| 12 |
from rfdetr.detr import RFDETRMedium
|
| 13 |
|
| 14 |
# UI Element classes
|
|
|
|
| 77 |
|
| 78 |
return img_with_boxes
|
| 79 |
|
| 80 |
+
|
| 81 |
+
def detections_to_raw_json(detections) -> str:
|
| 82 |
+
out = []
|
| 83 |
+
for box, score, cls_id in zip(
|
| 84 |
+
detections.xyxy,
|
| 85 |
+
detections.confidence,
|
| 86 |
+
detections.class_id
|
| 87 |
+
):
|
| 88 |
+
cid = int(cls_id)
|
| 89 |
+
out.append({
|
| 90 |
+
"class_id": cid,
|
| 91 |
+
"class_name": CLASSES[cid] if 0 <= cid < len(CLASSES) else str(cid),
|
| 92 |
+
"score": float(score),
|
| 93 |
+
"box_xyxy": [
|
| 94 |
+
float(box[0]),
|
| 95 |
+
float(box[1]),
|
| 96 |
+
float(box[2]),
|
| 97 |
+
float(box[3]),
|
| 98 |
+
],
|
| 99 |
+
})
|
| 100 |
+
return json.dumps(out, indent=2)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
@torch.inference_mode()
|
| 104 |
def detect_ui_elements(
|
| 105 |
image: Image.Image,
|
| 106 |
confidence_threshold: float,
|
| 107 |
line_thickness: int
|
| 108 |
+
) -> Tuple[Image.Image, str, str]:
|
| 109 |
"""
|
| 110 |
Detect UI elements in the uploaded image
|
|
|
|
| 111 |
Args:
|
| 112 |
image: Input PIL Image
|
| 113 |
confidence_threshold: Minimum confidence score for detections
|
| 114 |
line_thickness: Thickness of bounding box lines
|
|
|
|
| 115 |
Returns:
|
| 116 |
Annotated image and detection summary text
|
| 117 |
"""
|
| 118 |
try:
|
| 119 |
if image is None:
|
| 120 |
+
return None, "Please upload an image first.", "[]"
|
| 121 |
|
| 122 |
# Load model
|
| 123 |
model = load_model()
|
|
|
|
| 152 |
# Create summary text
|
| 153 |
summary_text = f"**Total detections:** {len(filtered_boxes)}"
|
| 154 |
|
| 155 |
+
raw_json = detections_to_raw_json(detections)
|
| 156 |
+
|
| 157 |
+
return annotated_pil, summary_text, raw_json
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
import traceback
|
| 161 |
error_msg = f"**Error during detection:**\n\n```\n{str(e)}\n\n{traceback.format_exc()}\n```"
|
| 162 |
print(error_msg) # Also print to logs
|
| 163 |
+
return None, error_msg, "[]"
|
| 164 |
+
|
| 165 |
|
| 166 |
# Gradio interface
|
| 167 |
with gr.Blocks(title="UI-DETR-1 UI Element Detector", theme=gr.themes.Soft()) as demo:
|
| 168 |
|
| 169 |
gr.Markdown("""
|
| 170 |
# UI-DETR-1 UI Element Detector
|
|
|
|
| 171 |
Upload a screenshot or UI mockup to automatically detect elements.
|
| 172 |
""")
|
| 173 |
|
|
|
|
| 209 |
|
| 210 |
summary_output = gr.Markdown(label="Detection Summary")
|
| 211 |
|
| 212 |
+
raw_output = gr.Code(
|
| 213 |
+
label="Raw Detection",
|
| 214 |
+
language="json"
|
| 215 |
+
)
|
| 216 |
|
| 217 |
# Connect button
|
| 218 |
detect_button.click(
|
| 219 |
fn=detect_ui_elements,
|
| 220 |
inputs=[input_image, confidence_slider, thickness_slider],
|
| 221 |
+
outputs=[output_image, summary_output, raw_output]
|
| 222 |
)
|
| 223 |
|
| 224 |
# Launch
|
| 225 |
if __name__ == "__main__":
|
| 226 |
+
demo.queue().launch(share=False)
|