Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,32 +4,63 @@ 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 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def get_json_annotations(self):
|
35 |
"""Get all annotations as formatted JSON string"""
|
@@ -38,7 +69,7 @@ class AnnotationManager:
|
|
38 |
def clear_annotations(self):
|
39 |
"""Clear all annotations"""
|
40 |
self.annotations = {}
|
41 |
-
return ""
|
42 |
|
43 |
def create_interface():
|
44 |
annotation_mgr = AnnotationManager()
|
@@ -52,7 +83,7 @@ def create_interface():
|
|
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.
|
56 |
|
57 |
**Labels:**
|
58 |
- advertisement: Use for marking advertisement regions
|
@@ -66,59 +97,37 @@ def create_interface():
|
|
66 |
label="Draw Bounding Boxes",
|
67 |
show_download_button=True,
|
68 |
interactive=True,
|
69 |
-
categories=CATEGORIES
|
70 |
)
|
71 |
|
72 |
with gr.Column(scale=1):
|
73 |
json_output = gr.TextArea(
|
74 |
label="Combined Annotations JSON",
|
75 |
-
interactive=
|
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.
|
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=
|
106 |
inputs=[bbox_input],
|
107 |
outputs=[json_output, status_msg]
|
108 |
)
|
109 |
|
110 |
clear_btn.click(
|
111 |
-
fn=
|
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__":
|
|
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
|
|
|
7 |
CATEGORIES = ["advertisement", "text"]
|
8 |
|
9 |
class AnnotationManager:
|
10 |
def __init__(self):
|
11 |
self.annotations = {}
|
12 |
|
13 |
+
def validate_annotations(self, bbox_data):
|
14 |
+
"""Validate the annotation data and return (is_valid, error_message)"""
|
15 |
+
if not bbox_data or not isinstance(bbox_data, tuple):
|
16 |
+
return False, "No image or annotations provided"
|
17 |
+
|
18 |
+
image_path, annotations = bbox_data
|
19 |
+
if not isinstance(image_path, str):
|
20 |
+
return False, "Invalid image format"
|
21 |
+
|
22 |
+
if not annotations:
|
23 |
+
return False, "No annotations drawn"
|
24 |
+
|
25 |
+
# Check each annotation
|
26 |
+
for ann in annotations:
|
27 |
+
if len(ann) != 5:
|
28 |
+
return False, "Invalid annotation format"
|
29 |
+
y1, y2, x1, x2, label = ann
|
30 |
+
|
31 |
+
# Validate coordinates
|
32 |
+
if any(not isinstance(coord, (int, float)) for coord in [y1, y2, x1, x2]):
|
33 |
+
return False, "Invalid coordinate values"
|
34 |
+
|
35 |
+
# Validate label
|
36 |
+
if not label or label not in CATEGORIES:
|
37 |
+
return False, f"Invalid or missing label. Must be one of: {', '.join(CATEGORIES)}"
|
38 |
+
|
39 |
+
return True, ""
|
40 |
+
|
41 |
def add_annotation(self, bbox_data):
|
42 |
"""Add or update annotations for an image"""
|
43 |
+
is_valid, error_msg = self.validate_annotations(bbox_data)
|
44 |
+
if not is_valid:
|
45 |
+
return self.get_json_annotations(), error_msg
|
46 |
+
|
47 |
+
image_path, annotations = bbox_data
|
48 |
+
filename = os.path.basename(image_path)
|
49 |
+
formatted_annotations = []
|
50 |
+
for ann in annotations:
|
51 |
+
y1, y2, x1, x2, label = ann
|
52 |
+
formatted_annotations.append({
|
53 |
+
"annotation": [y1, y2, x1, x2],
|
54 |
+
"label": label
|
55 |
+
})
|
56 |
+
self.annotations[filename] = formatted_annotations
|
57 |
+
|
58 |
+
# Count annotations by type
|
59 |
+
ad_count = sum(1 for ann in annotations if ann[4] == "advertisement")
|
60 |
+
text_count = sum(1 for ann in annotations if ann[4] == "text")
|
61 |
+
success_msg = f"Successfully saved for {filename}: {ad_count} advertisements, {text_count} text regions"
|
62 |
+
|
63 |
+
return self.get_json_annotations(), success_msg
|
64 |
|
65 |
def get_json_annotations(self):
|
66 |
"""Get all annotations as formatted JSON string"""
|
|
|
69 |
def clear_annotations(self):
|
70 |
"""Clear all annotations"""
|
71 |
self.annotations = {}
|
72 |
+
return "", "All annotations cleared"
|
73 |
|
74 |
def create_interface():
|
75 |
annotation_mgr = AnnotationManager()
|
|
|
83 |
2. Draw bounding boxes and select either 'advertisement' or 'text' as label
|
84 |
3. Click 'Save Annotations' to add to the collection
|
85 |
4. Repeat for all images
|
86 |
+
5. Copy the combined JSON when finished
|
87 |
|
88 |
**Labels:**
|
89 |
- advertisement: Use for marking advertisement regions
|
|
|
97 |
label="Draw Bounding Boxes",
|
98 |
show_download_button=True,
|
99 |
interactive=True,
|
100 |
+
categories=CATEGORIES
|
101 |
)
|
102 |
|
103 |
with gr.Column(scale=1):
|
104 |
json_output = gr.TextArea(
|
105 |
label="Combined Annotations JSON",
|
106 |
+
interactive=True,
|
107 |
+
lines=15,
|
108 |
+
show_copy_button=True
|
109 |
)
|
110 |
|
111 |
with gr.Row():
|
112 |
save_btn = gr.Button("Save Current Image Annotations", variant="primary")
|
113 |
clear_btn = gr.Button("Clear All Annotations", variant="secondary")
|
|
|
114 |
|
115 |
+
# Add status message with different styling based on message type
|
116 |
+
status_msg = gr.Markdown(label="Status")
|
117 |
|
118 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
save_btn.click(
|
120 |
+
fn=annotation_mgr.add_annotation,
|
121 |
inputs=[bbox_input],
|
122 |
outputs=[json_output, status_msg]
|
123 |
)
|
124 |
|
125 |
clear_btn.click(
|
126 |
+
fn=annotation_mgr.clear_annotations,
|
127 |
inputs=[],
|
128 |
outputs=[json_output, status_msg]
|
129 |
)
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
return demo
|
132 |
|
133 |
if __name__ == "__main__":
|