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