wuhp commited on
Commit
84c4ee0
·
verified ·
1 Parent(s): 3911cda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -74,20 +74,19 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str, split_ratios=(0.8, 0.1,
74
  )
75
  annos.setdefault(img_id, []).append(line)
76
 
77
- # --- locate the single folder of raw images
78
- img_src = None
 
79
  for dp, _, files in os.walk(root):
80
- if any(f.lower().endswith(('.jpg','.png','.jpeg')) for f in files):
81
- img_src = dp
82
- break
83
- if not img_src:
84
- raise FileNotFoundError(f"No images under {root}")
85
 
86
- # --- copy images + write flat label files
87
- name_to_id = {img['file_name']: img['id'] for img in coco['images']}
88
  for fname, img_id in name_to_id.items():
89
- src_path = os.path.join(img_src, fname)
90
- if not os.path.isfile(src_path):
 
91
  continue
92
  shutil.copy(src_path, os.path.join(flat_img, fname))
93
  with open(os.path.join(flat_lbl, fname.rsplit('.',1)[0] + ".txt"), 'w') as lf:
@@ -102,7 +101,7 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str, split_ratios=(0.8, 0.1,
102
  n = len(all_files)
103
  n_train = max(1, int(n * split_ratios[0]))
104
  n_valid = max(1, int(n * split_ratios[1]))
105
- # ensure at least 1 for each split
106
  n_valid = min(n_valid, n - n_train - 1)
107
 
108
  splits = {
@@ -118,12 +117,10 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str, split_ratios=(0.8, 0.1,
118
  os.makedirs(out_img_dir, exist_ok=True)
119
  os.makedirs(out_lbl_dir, exist_ok=True)
120
  for fn in files:
121
- # move image
122
  shutil.move(
123
  os.path.join(flat_img, fn),
124
  os.path.join(out_img_dir, fn)
125
  )
126
- # move corresponding .txt label
127
  lbl_fn = fn.rsplit('.',1)[0] + ".txt"
128
  shutil.move(
129
  os.path.join(flat_lbl, lbl_fn),
@@ -138,7 +135,9 @@ def convert_seg_to_bbox(api_key: str, dataset_url: str, split_ratios=(0.8, 0.1,
138
  before, after = [], []
139
  sample = random.sample(list(name_to_id.keys()), min(5, len(name_to_id)))
140
  for fname in sample:
141
- src = os.path.join(img_src, fname)
 
 
142
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
143
 
144
  seg_vis = img.copy()
@@ -174,7 +173,7 @@ def upload_and_train_detection(
174
  rf = Roboflow(api_key=api_key)
175
  ws = rf.workspace()
176
 
177
- # get-or-create detection project
178
  try:
179
  proj = ws.project(project_slug)
180
  except:
@@ -185,7 +184,7 @@ def upload_and_train_detection(
185
  project_license=project_license
186
  )
187
 
188
- # upload the folder that now has train/valid/test
189
  ws.upload_dataset(
190
  dataset_path,
191
  project_slug,
 
74
  )
75
  annos.setdefault(img_id, []).append(line)
76
 
77
+ # --- map each file_name to its actual path on disk
78
+ name_to_id = {img['file_name']: img['id'] for img in coco['images']}
79
+ file_paths = {}
80
  for dp, _, files in os.walk(root):
81
+ for f in files:
82
+ if f in name_to_id:
83
+ file_paths[f] = os.path.join(dp, f)
 
 
84
 
85
+ # --- copy images + write flat_labels
 
86
  for fname, img_id in name_to_id.items():
87
+ src_path = file_paths.get(fname)
88
+ if not src_path:
89
+ # skip if we couldn't find this image under root
90
  continue
91
  shutil.copy(src_path, os.path.join(flat_img, fname))
92
  with open(os.path.join(flat_lbl, fname.rsplit('.',1)[0] + ".txt"), 'w') as lf:
 
101
  n = len(all_files)
102
  n_train = max(1, int(n * split_ratios[0]))
103
  n_valid = max(1, int(n * split_ratios[1]))
104
+ # ensure at least 1 left for test
105
  n_valid = min(n_valid, n - n_train - 1)
106
 
107
  splits = {
 
117
  os.makedirs(out_img_dir, exist_ok=True)
118
  os.makedirs(out_lbl_dir, exist_ok=True)
119
  for fn in files:
 
120
  shutil.move(
121
  os.path.join(flat_img, fn),
122
  os.path.join(out_img_dir, fn)
123
  )
 
124
  lbl_fn = fn.rsplit('.',1)[0] + ".txt"
125
  shutil.move(
126
  os.path.join(flat_lbl, lbl_fn),
 
135
  before, after = [], []
136
  sample = random.sample(list(name_to_id.keys()), min(5, len(name_to_id)))
137
  for fname in sample:
138
+ src = file_paths.get(fname)
139
+ if not src:
140
+ continue
141
  img = cv2.cvtColor(cv2.imread(src), cv2.COLOR_BGR2RGB)
142
 
143
  seg_vis = img.copy()
 
173
  rf = Roboflow(api_key=api_key)
174
  ws = rf.workspace()
175
 
176
+ # getorcreate your detection project
177
  try:
178
  proj = ws.project(project_slug)
179
  except:
 
184
  project_license=project_license
185
  )
186
 
187
+ # upload the folder with proper train/valid/test
188
  ws.upload_dataset(
189
  dataset_path,
190
  project_slug,