wuhp commited on
Commit
99c4ece
·
verified ·
1 Parent(s): 228a29d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -49,13 +49,24 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
49
  for fname in filenames:
50
  if 'train' in fname.lower() and fname.lower().endswith('.json'):
51
  ann_file = os.path.join(dirpath, fname)
52
- print(f"Found annotation file: {ann_file}")
53
  break
54
  if ann_file:
55
  break
56
 
 
57
  if ann_file is None:
58
- raise FileNotFoundError(f"No JSON file containing 'train' found under {root}")
 
 
 
 
 
 
 
 
 
 
59
 
60
  # 3) load COCO annotations
61
  with open(ann_file, 'r') as f:
@@ -93,7 +104,7 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
93
  )
94
  annos.setdefault(img_id, []).append(line)
95
 
96
- # 7) locate your train-images folder (first folder under root with any image files)
97
  train_img_dir = None
98
  for dirpath, _, files in os.walk(root):
99
  if any(f.lower().endswith(('.jpg', '.png', '.jpeg')) for f in files):
@@ -122,16 +133,13 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
122
  src = os.path.join(train_img_dir, fname)
123
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
124
 
125
- # draw segmentation polygons
126
  seg_vis = img.copy()
127
  img_id = name_to_id[fname]
128
  for anno in coco['annotations']:
129
- if anno['image_id'] != img_id:
130
- continue
131
  pts = np.array(anno['segmentation'][0], dtype=np.int32).reshape(-1, 2)
132
  cv2.polylines(seg_vis, [pts], True, (255, 0, 0), 2)
133
 
134
- # draw bounding boxes
135
  box_vis = img.copy()
136
  for line in annos.get(img_id, []):
137
  _, cxn, cyn, wnorm, hnorm = map(float, line.split())
 
49
  for fname in filenames:
50
  if 'train' in fname.lower() and fname.lower().endswith('.json'):
51
  ann_file = os.path.join(dirpath, fname)
52
+ print(f"Found TRAIN annotation file: {ann_file}")
53
  break
54
  if ann_file:
55
  break
56
 
57
+ # 2b) fallback: first .json anywhere
58
  if ann_file is None:
59
+ for dirpath, _, filenames in os.walk(root):
60
+ for fname in filenames:
61
+ if fname.lower().endswith('.json'):
62
+ ann_file = os.path.join(dirpath, fname)
63
+ print(f"No TRAIN file—falling back to first JSON: {ann_file}")
64
+ break
65
+ if ann_file:
66
+ break
67
+
68
+ if ann_file is None:
69
+ raise FileNotFoundError(f"No JSON annotations found under {root}")
70
 
71
  # 3) load COCO annotations
72
  with open(ann_file, 'r') as f:
 
104
  )
105
  annos.setdefault(img_id, []).append(line)
106
 
107
+ # 7) locate your image folder (first with any .jpg/.png)
108
  train_img_dir = None
109
  for dirpath, _, files in os.walk(root):
110
  if any(f.lower().endswith(('.jpg', '.png', '.jpeg')) for f in files):
 
133
  src = os.path.join(train_img_dir, fname)
134
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
135
 
 
136
  seg_vis = img.copy()
137
  img_id = name_to_id[fname]
138
  for anno in coco['annotations']:
139
+ if anno['image_id'] != img_id: continue
 
140
  pts = np.array(anno['segmentation'][0], dtype=np.int32).reshape(-1, 2)
141
  cv2.polylines(seg_vis, [pts], True, (255, 0, 0), 2)
142
 
 
143
  box_vis = img.copy()
144
  for line in annos.get(img_id, []):
145
  _, cxn, cyn, wnorm, hnorm = map(float, line.split())