Skip to content

Commit

Permalink
Merge pull request #7 from mayrajeo/visualizations
Browse files Browse the repository at this point in the history
Visualizations
  • Loading branch information
mayrajeo authored Jan 23, 2024
2 parents 2bb5932 + d65f91e commit b7e878a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 45 deletions.
8 changes: 4 additions & 4 deletions geo2ml/data/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def shp_to_coco(
json.dump(coco_dict, f)
return

# %% ../../nbs/13_data.cv.ipynb 19
# %% ../../nbs/13_data.cv.ipynb 18
def coco_to_shp(
coco_data: Path | str, outpath: Path, raster_path: Path, downsample_factor: int = 1
):
Expand Down Expand Up @@ -297,7 +297,7 @@ def coco_to_shp(
tfmd_gdf.to_file(outpath / f'{i["file_name"][:-4]}.geojson', driver="GeoJSON")
return

# %% ../../nbs/13_data.cv.ipynb 23
# %% ../../nbs/13_data.cv.ipynb 22
def shp_to_coco_results(
prediction_path: Path,
raster_path: Path,
Expand Down Expand Up @@ -368,7 +368,7 @@ def shp_to_coco_results(
json.dump(results, f)
return

# %% ../../nbs/13_data.cv.ipynb 28
# %% ../../nbs/13_data.cv.ipynb 27
def shp_to_yolo(
raster_path: Path,
shp_path: Path,
Expand Down Expand Up @@ -460,7 +460,7 @@ def shp_to_yolo(
for n in names.keys():
dest.write(f" {names[n]}: {n}\n")

# %% ../../nbs/13_data.cv.ipynb 34
# %% ../../nbs/13_data.cv.ipynb 33
def yolo_to_shp(
prediction_path: Path,
raster_path: Path,
Expand Down
28 changes: 19 additions & 9 deletions geo2ml/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import rasterio as rio
import rasterio.plot as rioplot
import matplotlib.patches as mpatches
from PIL import Image, ImageDraw
from PIL import Image

# %% ../nbs/31_plotting.ipynb 6
def plot_coco_instance(
Expand Down Expand Up @@ -110,8 +110,8 @@ def plot_yolo_instance(
) -> plt.Axes:
"Plot yolo format instance to `ax` and return it. If `classes` are provided show them in legend"
image = Image.open(image_fname)
ax.imshow(image)
w, h = image.size
plotted_image = ImageDraw.Draw(image)
cmap = plt.get_cmap("tab20")
with open(annotation_fname) as f:
anns = f.read().split("\n")[:-1]
Expand Down Expand Up @@ -139,27 +139,37 @@ def plot_yolo_instance(
for ann in transformed_annotations:
obj_cls, x0, y0, x1, y1 = ann
c = tuple((np.array(cmap(obj_cls)) * 255).astype(np.uint8))
plotted_image.rectangle(((x0, y0), (x1, y1)), outline=c)
ax.add_patch(
mpatches.Rectangle(
(x0, y0),
x1 - x0,
y1 - y0,
fill=False,
linestyle="--",
linewidth=2,
edgecolor=np.array(cmap(obj_cls)),
)
)

elif ann_type == "polygon" or ann_type == "rotated box":
w, h = image.size
plotted_image = ImageDraw.Draw(image)

for ann in anns:
obj_cls = ann[0]
xcoords = [ann[i] * w for i in range(1, len(ann), 2)]
ycoords = [ann[i] * h for i in range(2, len(ann) + 1, 2)]
xcoords.append(xcoords[0])
ycoords.append(ycoords[0])
c = tuple((np.array(cmap(obj_cls)) * 255).astype(np.uint8))
plotted_image.polygon(list(zip(xcoords, ycoords)), outline=c)

coords = list(zip(xcoords, ycoords))
ax.add_patch(
mpatches.Polygon(
coords, fill=False, linewidth=1.5, edgecolor=np.array(cmap(obj_cls))
)
)
if classes:
ax.legend(
handles=[
mpatches.Patch(color=cmap(i), label=classes[i])
for i in range_of(classes)
]
)
ax.imshow(np.array(image))
return ax
7 changes: 7 additions & 0 deletions nbs/12_data.tiling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,13 @@
"from nbdev import nbdev_export\n",
"nbdev_export()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
24 changes: 9 additions & 15 deletions nbs/13_data.cv.ipynb

Large diffs are not rendered by default.

28 changes: 13 additions & 15 deletions nbs/31_plotting.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nbs/example_data/tiles/coco_norm.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nbs/example_data/tiles/coco_rot.json

Large diffs are not rendered by default.

0 comments on commit b7e878a

Please sign in to comment.