slau8405 commited on
Commit
8d41fcd
·
1 Parent(s): 8c997c7

Added few more features

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -33,26 +33,44 @@ class PolygonAugmentation:
33
  data = json.load(f)
34
  else:
35
  data = json.load(json_file)
36
- if 'shapes' not in data or not isinstance(data['shapes'], list):
37
- raise ValueError("Invalid JSON: 'shapes' key missing or not a list")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  polygons = []
40
  labels = []
41
  original_areas = []
42
 
43
- for shape in data['shapes']:
44
  if shape.get('shape_type') != 'polygon' or not shape.get('points') or len(shape['points']) < 3:
45
  if self.debug:
46
- print(f"[DEBUG] Skipping invalid shape")
47
  continue
48
  try:
49
  points = [[float(x), float(y)] for x, y in shape['points']]
50
  polygons.append(points)
51
  labels.append(shape['label'])
52
  original_areas.append(self.calculate_polygon_area(points))
53
- except (ValueError, TypeError):
54
  if self.debug:
55
- print(f"[DEBUG] Error processing points: {shape['points']}")
56
  continue
57
 
58
  if not polygons and self.debug:
@@ -137,7 +155,8 @@ class PolygonAugmentation:
137
  "points": poly,
138
  "group_id": None,
139
  "shape_type": "polygon",
140
- "flags": {}
 
141
  })
142
 
143
  aug_data = {
@@ -369,7 +388,7 @@ def augment_image(image: Image.Image, json_file: Any, aug_type: str, aug_param:
369
  )
370
 
371
  # Create a color map for unique labels
372
- unique_labels = list(set(aug_data['shapes'][i]['label'] for i in range(len(aug_data['shapes']))))
373
  colors = [
374
  (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255),
375
  (128, 0, 0), (0, 128, 0), (0, 0, 128), (128, 128, 0) # Add more if needed
@@ -382,10 +401,10 @@ def augment_image(image: Image.Image, json_file: Any, aug_type: str, aug_param:
382
 
383
  # Create masks for visualization
384
  height, width = aug_image.shape[:2]
385
- for poly, shape in zip(aug_data['shapes'], aug_data['shapes']):
386
  label = shape['label']
387
  color = label_color_map[label]
388
- points = np.array(poly['points'], dtype=np.int32)
389
 
390
  # Draw filled mask with transparency
391
  mask = np.zeros((height, width), dtype=np.uint8)
 
33
  data = json.load(f)
34
  else:
35
  data = json.load(json_file)
36
+
37
+ # Check for 'shapes' (LabelMe) or 'segments' (custom format)
38
+ shapes = []
39
+ if 'shapes' in data and isinstance(data['shapes'], list):
40
+ shapes = data['shapes']
41
+ elif 'segments' in data and isinstance(data['segments'], list):
42
+ # Convert custom 'segments' to LabelMe 'shapes' format
43
+ shapes = [
44
+ {
45
+ "label": seg.get("class", "unknown"),
46
+ "points": seg.get("polygon", []),
47
+ "shape_type": "polygon",
48
+ "group_id": None,
49
+ "flags": {},
50
+ "confidence": seg.get("confidence", 1.0)
51
+ }
52
+ for seg in data['segments']
53
+ ]
54
+ else:
55
+ raise ValueError("Invalid JSON: Neither 'shapes' nor ' segments' key found or not a list")
56
 
57
  polygons = []
58
  labels = []
59
  original_areas = []
60
 
61
+ for shape in shapes:
62
  if shape.get('shape_type') != 'polygon' or not shape.get('points') or len(shape['points']) < 3:
63
  if self.debug:
64
+ print(f"[DEBUG] Skipping invalid shape: {shape}")
65
  continue
66
  try:
67
  points = [[float(x), float(y)] for x, y in shape['points']]
68
  polygons.append(points)
69
  labels.append(shape['label'])
70
  original_areas.append(self.calculate_polygon_area(points))
71
+ except (ValueError, TypeError) as e:
72
  if self.debug:
73
+ print(f"[DEBUG] Error processing points: {shape['points']}, error: {str(e)}")
74
  continue
75
 
76
  if not polygons and self.debug:
 
155
  "points": poly,
156
  "group_id": None,
157
  "shape_type": "polygon",
158
+ "flags": {},
159
+ "confidence": 1.0 # Default confidence for augmented shapes
160
  })
161
 
162
  aug_data = {
 
388
  )
389
 
390
  # Create a color map for unique labels
391
+ unique_labels = list(set(shape['label'] for shape in aug_data['shapes']))
392
  colors = [
393
  (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255),
394
  (128, 0, 0), (0, 128, 0), (0, 0, 128), (128, 128, 0) # Add more if needed
 
401
 
402
  # Create masks for visualization
403
  height, width = aug_image.shape[:2]
404
+ for shape in aug_data['shapes']:
405
  label = shape['label']
406
  color = label_color_map[label]
407
+ points = np.array(shape['points'], dtype=np.int32)
408
 
409
  # Draw filled mask with transparency
410
  mask = np.zeros((height, width), dtype=np.uint8)