mrprimenotes commited on
Commit
fa27543
·
verified ·
1 Parent(s): 8535ce3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -4,7 +4,12 @@ import json
4
  import os
5
  from pathlib import Path
6
 
7
- CATEGORIES = ["advertisement", "text"]
 
 
 
 
 
8
 
9
  class AnnotationManager:
10
  def __init__(self):
@@ -22,7 +27,8 @@ class AnnotationManager:
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"
@@ -36,13 +42,22 @@ class AnnotationManager:
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)
@@ -56,9 +71,9 @@ class AnnotationManager:
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
 
@@ -69,7 +84,7 @@ class AnnotationManager:
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()
@@ -80,14 +95,14 @@ def create_interface():
80
 
81
  **Instructions:**
82
  1. Upload an image using the upload button in the annotator
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
90
- - text: Use for marking text regions
91
  """)
92
 
93
  with gr.Row():
 
4
  import os
5
  from pathlib import Path
6
 
7
+ # Define categories and their limits
8
+ CATEGORY_LIMITS = {
9
+ "advertisement": 1, # Maximum 1 advertisement annotation per image
10
+ "text": 2 # Maximum 2 text annotations per image
11
+ }
12
+ CATEGORIES = list(CATEGORY_LIMITS.keys())
13
 
14
  class AnnotationManager:
15
  def __init__(self):
 
27
  if not annotations:
28
  return False, "No annotations drawn"
29
 
30
+ # Count annotations per category
31
+ category_counts = {cat: 0 for cat in CATEGORIES}
32
  for ann in annotations:
33
  if len(ann) != 5:
34
  return False, "Invalid annotation format"
 
42
  if not label or label not in CATEGORIES:
43
  return False, f"Invalid or missing label. Must be one of: {', '.join(CATEGORIES)}"
44
 
45
+ # Count this annotation
46
+ category_counts[label] += 1
47
+
48
+ # Check category limits
49
+ for category, count in category_counts.items():
50
+ limit = CATEGORY_LIMITS[category]
51
+ if count > limit:
52
+ return False, f"Too many {category} annotations. Maximum allowed: {limit}"
53
+
54
  return True, ""
55
 
56
  def add_annotation(self, bbox_data):
57
  """Add or update annotations for an image"""
58
  is_valid, error_msg = self.validate_annotations(bbox_data)
59
  if not is_valid:
60
+ return self.get_json_annotations(), f"❌ Error: {error_msg}"
61
 
62
  image_path, annotations = bbox_data
63
  filename = os.path.basename(image_path)
 
71
  self.annotations[filename] = formatted_annotations
72
 
73
  # Count annotations by type
74
+ counts = {cat: sum(1 for ann in annotations if ann[4] == cat) for cat in CATEGORIES}
75
+ counts_str = ", ".join(f"{count} {cat}" for cat, count in counts.items())
76
+ success_msg = f"Successfully saved for {filename}: {counts_str}"
77
 
78
  return self.get_json_annotations(), success_msg
79
 
 
84
  def clear_annotations(self):
85
  """Clear all annotations"""
86
  self.annotations = {}
87
+ return "", "🗑️ All annotations cleared"
88
 
89
  def create_interface():
90
  annotation_mgr = AnnotationManager()
 
95
 
96
  **Instructions:**
97
  1. Upload an image using the upload button in the annotator
98
+ 2. Draw bounding boxes and select the appropriate label
99
  3. Click 'Save Annotations' to add to the collection
100
  4. Repeat for all images
101
  5. Copy the combined JSON when finished
102
 
103
+ **Annotation Limits per Image:**
104
+ - advertisement: Maximum 1 annotation
105
+ - text: Maximum 2 annotations
106
  """)
107
 
108
  with gr.Row():