Skip to content

Commit 3c90295

Browse files
committed
create new folders in dlc labeled-data when they do not exist
1 parent 63debca commit 3c90295

1 file changed

Lines changed: 55 additions & 54 deletions

File tree

  • packages/cheese3d/cheese3d/backends

packages/cheese3d/cheese3d/backends/dlc.py

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -143,60 +143,61 @@ def fix_video_symlinks(self):
143143
def import_c3d_labels(self, videos: Dict[str, Path]):
144144
for name, path in videos.items():
145145
label_folder = self.project_path / "labeled-data" / name
146-
if label_folder.exists():
147-
images = reglob(r".*\.png", str(path))
148-
for image in images:
149-
src_image = Path(image)
150-
dst_image = label_folder / src_image.name
151-
if dst_image.exists():
152-
os.remove(dst_image)
153-
relpath = os.path.relpath(src_image, label_folder)
154-
os.symlink(relpath, dst_image)
155-
annotations_yaml = path / "annotations.yaml"
156-
hdf = label_folder / f"CollectedData_{self.experimenter}.h5"
157-
csv = label_folder / f"CollectedData_{self.experimenter}.csv"
158-
if annotations_yaml.exists():
159-
with open(annotations_yaml, "r") as f:
160-
annotations = yaml.safe_load(f)
161-
data_dict = {}
162-
for kp, files in annotations.items():
163-
for file, coords in files.items():
164-
# create index for this row
165-
idx = ('labeled-data', str(label_folder.name), file)
166-
# get x and y coordinates (could be null/None)
167-
x_coord = coords[0][0]
168-
y_coord = coords[0][1]
169-
# create column keys for x and y
170-
x_col = (self.experimenter, kp, 'x')
171-
y_col = (self.experimenter, kp, 'y')
172-
# store in data dictionary
173-
if idx not in data_dict:
174-
data_dict[idx] = {}
175-
data_dict[idx][x_col] = x_coord
176-
data_dict[idx][y_col] = y_coord
177-
# convert to dataframe
178-
index = pd.MultiIndex.from_tuples(list(data_dict.keys()))
179-
columns = pd.MultiIndex.from_tuples(
180-
[(self.experimenter, bp, coord)
181-
for bp in annotations.keys()
182-
for coord in ['x', 'y']],
183-
names=["scorer", "bodyparts", "coords"]
184-
)
185-
# create empty df with the right structure
186-
df = pd.DataFrame(index=index, columns=columns)
187-
# fill in the values
188-
for idx, values in data_dict.items():
189-
for col, val in values.items():
190-
df.loc[idx, col] = val
191-
# convert to float (this will convert None/null to NaN)
192-
df = df.astype(float)
193-
# write dataframe to disk
194-
if hdf.exists():
195-
os.remove(hdf)
196-
df.to_hdf(hdf, key="df", mode="w")
197-
if csv.exists():
198-
os.remove(csv)
199-
df.to_csv(csv)
146+
if not label_folder.exists():
147+
label_folder.mkdir(exist_ok=True, parents=True)
148+
images = reglob(r".*\.png", str(path))
149+
for image in images:
150+
src_image = Path(image)
151+
dst_image = label_folder / src_image.name
152+
if dst_image.exists():
153+
os.remove(dst_image)
154+
relpath = os.path.relpath(src_image, label_folder)
155+
os.symlink(relpath, dst_image)
156+
annotations_yaml = path / "annotations.yaml"
157+
hdf = label_folder / f"CollectedData_{self.experimenter}.h5"
158+
csv = label_folder / f"CollectedData_{self.experimenter}.csv"
159+
if annotations_yaml.exists():
160+
with open(annotations_yaml, "r") as f:
161+
annotations = yaml.safe_load(f)
162+
data_dict = {}
163+
for kp, files in annotations.items():
164+
for file, coords in files.items():
165+
# create index for this row
166+
idx = ('labeled-data', str(label_folder.name), file)
167+
# get x and y coordinates (could be null/None)
168+
x_coord = coords[0][0]
169+
y_coord = coords[0][1]
170+
# create column keys for x and y
171+
x_col = (self.experimenter, kp, 'x')
172+
y_col = (self.experimenter, kp, 'y')
173+
# store in data dictionary
174+
if idx not in data_dict:
175+
data_dict[idx] = {}
176+
data_dict[idx][x_col] = x_coord
177+
data_dict[idx][y_col] = y_coord
178+
# convert to dataframe
179+
index = pd.MultiIndex.from_tuples(list(data_dict.keys()))
180+
columns = pd.MultiIndex.from_tuples(
181+
[(self.experimenter, bp, coord)
182+
for bp in annotations.keys()
183+
for coord in ['x', 'y']],
184+
names=["scorer", "bodyparts", "coords"]
185+
)
186+
# create empty df with the right structure
187+
df = pd.DataFrame(index=index, columns=columns)
188+
# fill in the values
189+
for idx, values in data_dict.items():
190+
for col, val in values.items():
191+
df.loc[idx, col] = val
192+
# convert to float (this will convert None/null to NaN)
193+
df = df.astype(float)
194+
# write dataframe to disk
195+
if hdf.exists():
196+
os.remove(hdf)
197+
df.to_hdf(hdf, key="df", mode="w")
198+
if csv.exists():
199+
os.remove(csv)
200+
df.to_csv(csv)
200201

201202
def export_c3d_labels(self, videos: Dict[str, Path]):
202203
for name, path in videos.items():

0 commit comments

Comments
 (0)