Adding metdata to tiled images
This commit is contained in:
@@ -114,11 +114,12 @@ class Label:
|
||||
return truth_val
|
||||
|
||||
def to_string(self, bbox: list = None, polygon: list = None):
|
||||
coords = ""
|
||||
if bbox is None:
|
||||
bbox = self.bbox
|
||||
# coords += " ".join([f"{x:.6f}" for x in self.bbox])
|
||||
if polygon is None:
|
||||
polygon = self.polygon
|
||||
coords = " ".join([f"{x:.6f}" for x in self.bbox])
|
||||
if self.polygon is not None:
|
||||
coords += " " + " ".join([f"{x:.6f} {y:.6f}" for x, y in self.polygon])
|
||||
return f"{self.class_id} {coords}"
|
||||
@@ -179,6 +180,13 @@ class ImageSplitter:
|
||||
|
||||
for i in range(patch_size[0]):
|
||||
for j in range(patch_size[1]):
|
||||
metadata = {
|
||||
"image_path": str(self.image_path),
|
||||
"label_path": str(self.label_path),
|
||||
"tile_section": f"{i}, {j}",
|
||||
"tile_size": f"{hstep}, {wstep}",
|
||||
"patch_size": f"{patch_size[0]}, {patch_size[1]}",
|
||||
}
|
||||
tile_reference = f"i{i}j{j}"
|
||||
hrange = (i * hstep / h, (i + 1) * hstep / h)
|
||||
wrange = (j * wstep / w, (j + 1) * wstep / w)
|
||||
@@ -199,7 +207,7 @@ class ImageSplitter:
|
||||
print(l.bbox)
|
||||
|
||||
# print(labels)
|
||||
yield tile_reference, tile, labels
|
||||
yield tile_reference, tile, labels, metadata
|
||||
|
||||
def split_respective_to_label(self, padding: int = 67):
|
||||
if self.labels is None:
|
||||
@@ -208,6 +216,7 @@ class ImageSplitter:
|
||||
for i, label in enumerate(self.labels):
|
||||
tile_reference = f"_lbl-{i+1:02d}"
|
||||
# print(label.bbox)
|
||||
metadata = {"image_path": str(self.image_path), "label_path": str(self.label_path), "label_index": str(i)}
|
||||
|
||||
xc_norm, yc_norm, h_norm, w_norm = label.bbox # normalized coords
|
||||
xc, yc, h, w = [
|
||||
@@ -246,17 +255,17 @@ class ImageSplitter:
|
||||
|
||||
# print("tile shape:", tile.shape)
|
||||
|
||||
yolo_annotation = f"{label.class_id} {x_offset/nx} {y_offset/ny} {h_norm} {w_norm} "
|
||||
print(yolo_annotation)
|
||||
yolo_annotation = f"{label.class_id} " # {x_offset/nx} {y_offset/ny} {h_norm} {w_norm} "
|
||||
yolo_annotation += " ".join(
|
||||
[
|
||||
f"{(x*self.image.shape[1]-(xc - x_offset))/nx:.6f} {(y*self.image.shape[0]-(yc-y_offset))/ny:.6f}"
|
||||
for x, y in label.polygon
|
||||
]
|
||||
)
|
||||
print(yolo_annotation)
|
||||
new_label = Label(yolo_annotation=yolo_annotation)
|
||||
|
||||
yield tile_reference, tile, [new_label]
|
||||
yield tile_reference, tile, [new_label], metadata
|
||||
|
||||
|
||||
def main(args):
|
||||
@@ -278,9 +287,9 @@ def main(args):
|
||||
else:
|
||||
data = data.split_into_tiles(patch_size=args.patch_size)
|
||||
|
||||
for tile_reference, tile, labels in data:
|
||||
for tile_reference, tile, labels, metadata in data:
|
||||
print()
|
||||
print(tile_reference, tile.shape, labels) # len(labels) if labels else None)
|
||||
print(tile_reference, tile.shape, labels, metadata) # len(labels) if labels else None)
|
||||
|
||||
# { debug
|
||||
debug = False
|
||||
@@ -310,15 +319,21 @@ def main(args):
|
||||
# } debug
|
||||
|
||||
if args.output:
|
||||
imwrite(args.output / "images" / f"{image_path.stem}_{tile_reference}.tif", tile)
|
||||
# imwrite(args.output / "images" / f"{image_path.stem}_{tile_reference}.tif", tile, metadata=metadata)
|
||||
scale = 5
|
||||
tile_zoomed = zoom(tile, zoom=scale)
|
||||
imwrite(args.output / "images-zoomed" / f"{image_path.stem}_{tile_reference}.tif", tile_zoomed)
|
||||
metadata["scale"] = scale
|
||||
imwrite(
|
||||
args.output / "images" / f"{image_path.stem}_{tile_reference}.tif",
|
||||
tile_zoomed,
|
||||
metadata=metadata,
|
||||
imagej=True,
|
||||
)
|
||||
|
||||
if labels is not None:
|
||||
with open(args.output / "labels" / f"{image_path.stem}_{tile_reference}.txt", "w") as f:
|
||||
for label in labels:
|
||||
label.offset_label(tile.shape[1], tile.shape[0])
|
||||
# label.offset_label(tile.shape[1], tile.shape[0])
|
||||
f.write(label.to_string() + "\n")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user