Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_bbox_annotator import BBoxAnnotator
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
# Define the allowed categories
|
8 |
+
CATEGORIES = ["advertisement", "text"]
|
9 |
+
|
10 |
+
class AnnotationManager:
|
11 |
+
def __init__(self):
|
12 |
+
self.annotations = {}
|
13 |
+
|
14 |
+
def add_annotation(self, bbox_data):
|
15 |
+
"""Add or update annotations for an image"""
|
16 |
+
if bbox_data and isinstance(bbox_data, tuple):
|
17 |
+
image_path, annotations = bbox_data
|
18 |
+
if isinstance(image_path, str) and annotations:
|
19 |
+
filename = os.path.basename(image_path)
|
20 |
+
formatted_annotations = []
|
21 |
+
for ann in annotations:
|
22 |
+
y1, y2, x1, x2, label = ann
|
23 |
+
# Ensure label is one of the allowed categories
|
24 |
+
if label not in CATEGORIES:
|
25 |
+
continue
|
26 |
+
formatted_annotations.append({
|
27 |
+
"annotation": [y1, y2, x1, x2],
|
28 |
+
"label": label
|
29 |
+
})
|
30 |
+
if formatted_annotations: # Only add if there are valid annotations
|
31 |
+
self.annotations[filename] = formatted_annotations
|
32 |
+
return self.get_json_annotations()
|
33 |
+
|
34 |
+
def get_json_annotations(self):
|
35 |
+
"""Get all annotations as formatted JSON string"""
|
36 |
+
return json.dumps(self.annotations, indent=2)
|
37 |
+
|
38 |
+
def clear_annotations(self):
|
39 |
+
"""Clear all annotations"""
|
40 |
+
self.annotations = {}
|
41 |
+
return ""
|
42 |
+
|
43 |
+
def create_interface():
|
44 |
+
annotation_mgr = AnnotationManager()
|
45 |
+
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("""
|
48 |
+
# Advertisement and Text Annotation Tool
|
49 |
+
|
50 |
+
**Instructions:**
|
51 |
+
1. Upload an image using the upload button in the annotator
|
52 |
+
2. Draw bounding boxes and select either 'advertisement' or 'text' as label
|
53 |
+
3. Click 'Save Annotations' to add to the collection
|
54 |
+
4. Repeat for all images
|
55 |
+
5. Click 'Download JSON' when finished
|
56 |
+
|
57 |
+
**Labels:**
|
58 |
+
- advertisement: Use for marking advertisement regions
|
59 |
+
- text: Use for marking text regions
|
60 |
+
""")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column(scale=2):
|
64 |
+
bbox_input = BBoxAnnotator(
|
65 |
+
show_label=True,
|
66 |
+
label="Draw Bounding Boxes",
|
67 |
+
show_download_button=True,
|
68 |
+
interactive=True,
|
69 |
+
categories=CATEGORIES # Set the predefined categories
|
70 |
+
)
|
71 |
+
|
72 |
+
with gr.Column(scale=1):
|
73 |
+
json_output = gr.TextArea(
|
74 |
+
label="Combined Annotations JSON",
|
75 |
+
interactive=False,
|
76 |
+
lines=15
|
77 |
+
)
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
save_btn = gr.Button("Save Current Image Annotations", variant="primary")
|
81 |
+
clear_btn = gr.Button("Clear All Annotations", variant="secondary")
|
82 |
+
download_btn = gr.Button("Download Combined JSON", variant="primary")
|
83 |
+
|
84 |
+
# Add status message
|
85 |
+
status_msg = gr.Textbox(label="Status", interactive=False)
|
86 |
+
|
87 |
+
# Event handlers
|
88 |
+
def save_and_update(bbox_data):
|
89 |
+
if not bbox_data or not isinstance(bbox_data, tuple):
|
90 |
+
return annotation_mgr.get_json_annotations(), "No image or annotations to save"
|
91 |
+
|
92 |
+
image_path, annotations = bbox_data
|
93 |
+
if not annotations:
|
94 |
+
return annotation_mgr.get_json_annotations(), "No annotations drawn"
|
95 |
+
|
96 |
+
# Count annotations by type
|
97 |
+
ad_count = sum(1 for ann in annotations if ann[4] == "advertisement")
|
98 |
+
text_count = sum(1 for ann in annotations if ann[4] == "text")
|
99 |
+
|
100 |
+
json_str = annotation_mgr.add_annotation(bbox_data)
|
101 |
+
filename = os.path.basename(image_path) if isinstance(image_path, str) else "unknown"
|
102 |
+
return json_str, f"Saved for {filename}: {ad_count} advertisements, {text_count} text regions"
|
103 |
+
|
104 |
+
save_btn.click(
|
105 |
+
fn=save_and_update,
|
106 |
+
inputs=[bbox_input],
|
107 |
+
outputs=[json_output, status_msg]
|
108 |
+
)
|
109 |
+
|
110 |
+
clear_btn.click(
|
111 |
+
fn=lambda: (annotation_mgr.clear_annotations(), "All annotations cleared"),
|
112 |
+
inputs=[],
|
113 |
+
outputs=[json_output, status_msg]
|
114 |
+
)
|
115 |
+
|
116 |
+
download_btn.click(
|
117 |
+
fn=lambda x: {"text": x, "filename": "combined_annotations.json"},
|
118 |
+
inputs=[json_output],
|
119 |
+
outputs=[gr.File()]
|
120 |
+
)
|
121 |
+
|
122 |
+
return demo
|
123 |
+
|
124 |
+
if __name__ == "__main__":
|
125 |
+
demo = create_interface()
|
126 |
+
demo.launch()
|