104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
|
|
import numpy as np
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
from skimage.draw import polygon
|
||
|
|
from tifffile import TiffFile
|
||
|
|
|
||
|
|
from src.database.db_manager import DatabaseManager
|
||
|
|
|
||
|
|
|
||
|
|
def read_image(image_path: Path) -> np.ndarray:
|
||
|
|
metadata = {}
|
||
|
|
with TiffFile(image_path) as tif:
|
||
|
|
image = tif.asarray()
|
||
|
|
metadata = tif.imagej_metadata
|
||
|
|
return image, metadata
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
|
||
|
|
polygon_vertices = np.array([[10, 10], [50, 10], [50, 50], [10, 50]])
|
||
|
|
image = np.zeros((100, 100), dtype=np.uint8)
|
||
|
|
rr, cc = polygon(polygon_vertices[:, 0], polygon_vertices[:, 1])
|
||
|
|
image[rr, cc] = 255
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
|
||
|
|
db = DatabaseManager()
|
||
|
|
model_name = "c17"
|
||
|
|
model_id = db.get_models(filters={"model_name": model_name})[0]["id"]
|
||
|
|
print(f"Model name {model_name}, id {model_id}")
|
||
|
|
detections = db.get_detections(filters={"model_id": model_id})
|
||
|
|
|
||
|
|
file_stems = set()
|
||
|
|
|
||
|
|
for detection in detections:
|
||
|
|
file_stems.add(detection["image_filename"].split("_")[0])
|
||
|
|
|
||
|
|
print("Files:", file_stems)
|
||
|
|
|
||
|
|
for stem in file_stems:
|
||
|
|
print(stem)
|
||
|
|
detections = db.get_detections(filters={"model_id": model_id, "i.filename": f"LIKE %{stem}%"})
|
||
|
|
annotations = []
|
||
|
|
for detection in detections:
|
||
|
|
source_path = Path(detection["metadata"]["source_path"])
|
||
|
|
image, metadata = read_image(source_path)
|
||
|
|
|
||
|
|
offset = np.array(list(map(int, metadata["tile_section"].split(","))))[::-1]
|
||
|
|
scale = np.array(list(map(int, metadata["patch_size"].split(","))))[::-1]
|
||
|
|
# tile_size = np.array(list(map(int, metadata["tile_size"].split(","))))
|
||
|
|
segmentation = np.array(detection["segmentation_mask"]) # * tile_size
|
||
|
|
|
||
|
|
# print(source_path, image, metadata, segmentation.shape)
|
||
|
|
# print(offset)
|
||
|
|
# print(scale)
|
||
|
|
# print(segmentation)
|
||
|
|
|
||
|
|
# segmentation = (segmentation + offset * tile_size) / (tile_size * scale)
|
||
|
|
segmentation = (segmentation + offset) / scale
|
||
|
|
|
||
|
|
yolo_annotation = f"{detection['metadata']['class_id']} " + " ".join(
|
||
|
|
[f"{x:.6f} {y:.6f}" for x, y in segmentation]
|
||
|
|
)
|
||
|
|
annotations.append(yolo_annotation)
|
||
|
|
# print(segmentation)
|
||
|
|
# print(yolo_annotation)
|
||
|
|
|
||
|
|
# aa
|
||
|
|
print(
|
||
|
|
" ",
|
||
|
|
detection["model_name"],
|
||
|
|
detection["image_id"],
|
||
|
|
detection["image_filename"],
|
||
|
|
source_path,
|
||
|
|
metadata["label_path"],
|
||
|
|
)
|
||
|
|
# section_i_section_j = detection["image_filename"].split("_")[1].split(".")[0]
|
||
|
|
# print(" ", section_i_section_j)
|
||
|
|
|
||
|
|
label_path = metadata["label_path"]
|
||
|
|
print(" ", label_path)
|
||
|
|
with open(label_path, "w") as f:
|
||
|
|
f.write("\n".join(annotations))
|
||
|
|
|
||
|
|
exit()
|
||
|
|
|
||
|
|
for detection in detections:
|
||
|
|
print(detection["model_name"], detection["image_id"], detection["image_filename"])
|
||
|
|
|
||
|
|
print(detections[0])
|
||
|
|
# polygon_vertices = np.array([[10, 10], [50, 10], [50, 50], [10, 50]])
|
||
|
|
|
||
|
|
# image = np.zeros((100, 100), dtype=np.uint8)
|
||
|
|
|
||
|
|
# rr, cc = polygon(polygon_vertices[:, 0], polygon_vertices[:, 1])
|
||
|
|
|
||
|
|
# image[rr, cc] = 255
|
||
|
|
|
||
|
|
# import matplotlib.pyplot as plt
|
||
|
|
|
||
|
|
# plt.imshow(image, cmap='gray')
|
||
|
|
# plt.show()
|