wuhp commited on
Commit
7e2847c
·
verified ·
1 Parent(s): 43b1849

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -33,17 +33,17 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
33
  dataset = version_obj.download("coco-segmentation")
34
  root = dataset.location # e.g. "/home/user/app/YourProject"
35
 
36
- # 2) find the train JSON (anything with "train" in the name)
37
  ann_file = None
38
- for dp, _, files in os.walk(root):
39
- for fname in files:
40
- if fname.lower().endswith(".json") and "train" in fname.lower():
41
- ann_file = os.path.join(dp, fname)
42
  break
43
  if ann_file:
44
  break
45
  if ann_file is None:
46
- raise FileNotFoundError(f"Could not locate any '*train*.json' under {root}")
47
 
48
  # 3) load COCO annotations
49
  with open(ann_file, 'r') as f:
@@ -80,11 +80,11 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
80
  )
81
  annos.setdefault(img_id, []).append(line)
82
 
83
- # 7) locate your train-images folder (first folder under root with any .jpg/.png)
84
  train_img_dir = None
85
- for dp, _, files in os.walk(root):
86
- if any(f.lower().endswith((".jpg", ".png", ".jpeg")) for f in files):
87
- train_img_dir = dp
88
  break
89
  if train_img_dir is None:
90
  raise FileNotFoundError(f"No image files found under {root}")
@@ -107,7 +107,7 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
107
  src = os.path.join(train_img_dir, fname)
108
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
109
 
110
- # draw polys
111
  seg_vis = img.copy()
112
  img_id = name_to_id[fname]
113
  for anno in coco['annotations']:
@@ -116,7 +116,7 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
116
  pts = np.array(anno['segmentation'][0], dtype=np.int32).reshape(-1, 2)
117
  cv2.polylines(seg_vis, [pts], True, (255, 0, 0), 2)
118
 
119
- # draw boxes
120
  box_vis = img.copy()
121
  for line in annos.get(img_id, []):
122
  _, cxn, cyn, wnorm, hnorm = map(float, line.split())
@@ -135,10 +135,9 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str):
135
  # --- Gradio app ---
136
  with gr.Blocks() as app:
137
  gr.Markdown("# Segmentation → YOLOv8 Converter")
138
- api_input = gr.Textbox(label="Roboflow API Key", type="password")
139
- url_input = gr.Textbox(label="Roboflow Dataset URL (Segmentation)")
140
- run_btn = gr.Button("Convert")
141
-
142
  before_gallery = gr.Gallery(label="Before (Segmentation)", columns=5, height="auto")
143
  after_gallery = gr.Gallery(label="After (Bounding Boxes)", columns=5, height="auto")
144
 
@@ -149,5 +148,4 @@ with gr.Blocks() as app:
149
  )
150
 
151
  if __name__ == "__main__":
152
- # set share=True if you need a public link
153
  app.launch()
 
33
  dataset = version_obj.download("coco-segmentation")
34
  root = dataset.location # e.g. "/home/user/app/YourProject"
35
 
36
+ # 2) search for any JSON file with "train" in its name
37
  ann_file = None
38
+ for dirpath, _, filenames in os.walk(root):
39
+ for fname in filenames:
40
+ if 'train' in fname.lower() and fname.lower().endswith('.json'):
41
+ ann_file = os.path.join(dirpath, fname)
42
  break
43
  if ann_file:
44
  break
45
  if ann_file is None:
46
+ raise FileNotFoundError(f"No JSON file containing 'train' found under {root}")
47
 
48
  # 3) load COCO annotations
49
  with open(ann_file, 'r') as f:
 
80
  )
81
  annos.setdefault(img_id, []).append(line)
82
 
83
+ # 7) locate your train-images folder (first folder under root with any image files)
84
  train_img_dir = None
85
+ for dirpath, _, files in os.walk(root):
86
+ if any(f.lower().endswith(('.jpg', '.png', '.jpeg')) for f in files):
87
+ train_img_dir = dirpath
88
  break
89
  if train_img_dir is None:
90
  raise FileNotFoundError(f"No image files found under {root}")
 
107
  src = os.path.join(train_img_dir, fname)
108
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
109
 
110
+ # draw segmentation polygons
111
  seg_vis = img.copy()
112
  img_id = name_to_id[fname]
113
  for anno in coco['annotations']:
 
116
  pts = np.array(anno['segmentation'][0], dtype=np.int32).reshape(-1, 2)
117
  cv2.polylines(seg_vis, [pts], True, (255, 0, 0), 2)
118
 
119
+ # draw bounding boxes
120
  box_vis = img.copy()
121
  for line in annos.get(img_id, []):
122
  _, cxn, cyn, wnorm, hnorm = map(float, line.split())
 
135
  # --- Gradio app ---
136
  with gr.Blocks() as app:
137
  gr.Markdown("# Segmentation → YOLOv8 Converter")
138
+ api_input = gr.Textbox(label="Roboflow API Key", type="password")
139
+ url_input = gr.Textbox(label="Roboflow Dataset URL (Segmentation)")
140
+ run_btn = gr.Button("Convert")
 
141
  before_gallery = gr.Gallery(label="Before (Segmentation)", columns=5, height="auto")
142
  after_gallery = gr.Gallery(label="After (Bounding Boxes)", columns=5, height="auto")
143
 
 
148
  )
149
 
150
  if __name__ == "__main__":
 
151
  app.launch()