Skip to content

Commit c954e6c

Browse files
committed
Overlay
1 parent 31afb4b commit c954e6c

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

superannotate/db/images.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,9 @@ def upload_annotations_from_json_to_image(
946946
)
947947

948948

949-
def create_fuse_image(image, classes_json, project_type, in_memory=False):
949+
def create_fuse_image(
950+
image, classes_json, project_type, in_memory=False, output_overlay=False
951+
):
950952
"""Creates fuse for locally located image and annotations
951953
952954
:param image: path to image
@@ -970,11 +972,20 @@ def create_fuse_image(image, classes_json, project_type, in_memory=False):
970972
if "name" not in ann_class:
971973
continue
972974
class_color_dict[ann_class["name"]] = ann_class["color"]
973-
image_size = Image.open(image).size
975+
pil_image = Image.open(image)
976+
image_size = pil_image.size
974977
fi = np.full((image_size[1], image_size[0], 4), [0, 0, 0, 255], np.uint8)
978+
if output_overlay:
979+
fi_ovl = np.full(
980+
(image_size[1], image_size[0], 4), [0, 0, 0, 255], np.uint8
981+
)
982+
fi_ovl[:, :, :3] = np.array(pil_image)
975983
if project_type == "Vector":
976984
fi_pil = Image.fromarray(fi)
977985
draw = ImageDraw.Draw(fi_pil)
986+
if output_overlay:
987+
fi_pil_ovl = Image.fromarray(fi_ovl)
988+
draw_ovl = ImageDraw.Draw(fi_pil_ovl)
978989
for annotation in annotation_json:
979990
if "className" not in annotation:
980991
continue
@@ -988,22 +999,44 @@ def create_fuse_image(image, classes_json, project_type, in_memory=False):
988999
(annotation["points"]["x2"], annotation["points"]["y2"])
9891000
)
9901001
draw.rectangle(pt, fill_color, outline_color)
1002+
if output_overlay:
1003+
draw_ovl.rectangle(pt, None, fill_color)
9911004
elif annotation["type"] == "polygon":
9921005
pts = annotation["points"]
9931006
draw.polygon(pts, fill_color, outline_color)
1007+
if output_overlay:
1008+
draw_ovl.polygon(pts, None, fill_color)
9941009
elif annotation["type"] == "polyline":
9951010
pts = annotation["points"]
9961011
draw.line(pts, fill_color, width=2)
1012+
if output_overlay:
1013+
draw_ovl.line(pts, fill_color, width=2)
9971014
elif annotation["type"] == "point":
9981015
pts = [
9991016
annotation["x"] - 2, annotation["y"] - 2,
10001017
annotation["x"] + 2, annotation["y"] + 2
10011018
]
10021019
draw.ellipse(pts, fill_color, outline_color)
1020+
if output_overlay:
1021+
draw_ovl.ellipse(pts, None, fill_color)
10031022
elif annotation["type"] == "ellipse":
10041023
temp = np.full(
10051024
(image_size[1], image_size[0], 3), [0, 0, 0], np.uint8
10061025
)
1026+
if output_overlay:
1027+
temp_ovl = np.full(
1028+
(image_size[1], image_size[0], 3), [0, 0, 0], np.uint8
1029+
)
1030+
cv2.ellipse(
1031+
temp_ovl, (annotation["cx"], annotation["cy"]),
1032+
(annotation["rx"], annotation["ry"]),
1033+
annotation["angle"], 0, 360, fill_color[:-1], 1
1034+
)
1035+
new_array_ovl = np.array(fi_pil_ovl)
1036+
new_array_ovl[:, :, :-1] += temp_ovl
1037+
new_array_ovl[:, :, 3] += temp_mask
1038+
fi_pil_ovl = Image.fromarray(new_array_ovl)
1039+
draw_ovl = ImageDraw.Draw(fi_pil_ovl)
10071040
cv2.ellipse(
10081041
temp, (annotation["cx"], annotation["cy"]),
10091042
(annotation["rx"], annotation["ry"]), annotation["angle"],
@@ -1030,6 +1063,8 @@ def create_fuse_image(image, classes_json, project_type, in_memory=False):
10301063
for pt in pts:
10311064
pt_e = [pt["x"] - 2, pt["y"] - 2, pt["x"] + 2, pt["y"] + 2]
10321065
draw.ellipse(pt_e, fill_color, fill_color)
1066+
if output_overlay:
1067+
draw_ovl.ellipse(pt_e, fill_color, fill_color)
10331068
pt_dict[pt["id"]] = [pt["x"], pt["y"]]
10341069
connections = annotation["connections"]
10351070
for connection in connections:
@@ -1038,9 +1073,16 @@ def create_fuse_image(image, classes_json, project_type, in_memory=False):
10381073
fill_color,
10391074
width=1
10401075
)
1076+
if output_overlay:
1077+
draw_ovl.line(
1078+
pt_dict[connection["from"]] +
1079+
pt_dict[connection["to"]],
1080+
fill_color,
1081+
width=1
1082+
)
10411083
else:
10421084
annotation_mask = np.array(Image.open(annotation_path[1]))
1043-
print(annotation_mask.shape, annotation_mask.dtype)
1085+
# print(annotation_mask.shape, annotation_mask.dtype)
10441086
for annotation in annotation_json:
10451087
if "className" not in annotation or "parts" not in annotation:
10461088
continue
@@ -1055,7 +1097,15 @@ def create_fuse_image(image, classes_json, project_type, in_memory=False):
10551097
fi_pil = Image.fromarray(fi)
10561098

10571099
if in_memory:
1058-
return fi_pil
1100+
if output_overlay:
1101+
return (fi_pil, fi_pil_ovl)
1102+
else:
1103+
return (fi_pil, )
10591104
fuse_path = str(image) + "___fuse.png"
10601105
fi_pil.save(fuse_path)
1061-
return fuse_path
1106+
if output_overlay:
1107+
overlay_path = str(image) + "___overlay.jpg"
1108+
fi_pil_ovl.save(overlay_path)
1109+
return (fuse_path, overlay_path)
1110+
else:
1111+
return (fuse_path)

0 commit comments

Comments
 (0)