diff --git a/extensions/chaeo/annotators.py b/extensions/chaeo/annotators.py index f951551a24b560ab068e8b4b41ea6eec82271791..b0cc1453243c82e1aef984423a5fe555f57d4b35 100644 --- a/extensions/chaeo/annotators.py +++ b/extensions/chaeo/annotators.py @@ -1,6 +1,8 @@ import numpy as np from PIL import Image, ImageDraw, ImageFont +from model_server.process import rescale + def draw_boxes_on_2d_image(img, boxes, **kwargs): pilimg = Image.fromarray(np.copy(img)) # drawing modifies array in-place draw = ImageDraw.Draw(pilimg) @@ -41,4 +43,12 @@ def draw_boxes_on_3d_image(img, boxes, draw_full_depth=False, **kwargs): assert clip >= 0.0 and clip <= 1.0 annotated = rescale(annotated, clip=clip) - return annotated \ No newline at end of file + return annotated + +def draw_box_on_patch(patch, bbox, linewidth=1): + assert len(patch.shape) == 2 + ((x0, y0), (x1, y1)) = bbox + pilimg = Image.fromarray(patch) # drawing modifies array in-place + draw = ImageDraw.Draw(pilimg) + draw.rectangle([(x0, y0), (x1, y1)], outline='white', width=linewidth) + return np.array(pilimg) \ No newline at end of file diff --git a/extensions/chaeo/products.py b/extensions/chaeo/products.py index fa47a57d82cd4e94c699cd86d0ae9659a30c654b..1e10205c0d93231a904f199bb4977c32bdb51188 100644 --- a/extensions/chaeo/products.py +++ b/extensions/chaeo/products.py @@ -6,6 +6,7 @@ from PIL import Image, ImageDraw from skimage.io import imsave from tifffile import imwrite +from extensions.chaeo.annotators import draw_box_on_patch from model_server.accessors import GenericImageDataAccessor from model_server.process import pad, rescale, resample_to_8bit @@ -67,10 +68,14 @@ def export_patches_from_zstack( y1 = rbb['y1'] for zi in range(0, patch.shape[3]): - pilimg = Image.fromarray(patch[:, :, 0, zi]) # drawing modifies array in-place - draw = ImageDraw.Draw(pilimg) - draw.rectangle([(x0, y0), (x1, y1)], outline='white', width=kwargs.get('linewidth', 1)) - patch[:, :, 0, zi] = np.array(pilimg) + # pilimg = Image.fromarray(patch[:, :, 0, zi]) # drawing modifies array in-place + # draw = ImageDraw.Draw(pilimg) + # draw.rectangle([(x0, y0), (x1, y1)], outline='white', width=kwargs.get('linewidth', 1)) + # patch[:, :, 0, zi] = np.array(pilimg) + patch[:, :, 0, zi] = draw_box_on_patch( + patch[:, :, 0, zi], + ((x0, y0), (x1, y1)), + ) if pad_to: patch = pad(patch, pad_to) @@ -79,4 +84,4 @@ def export_patches_from_zstack( fname = f'{prefix}-la{obj.label:04d}-zi{obj.zi:04d}.{ext}' success = _write_patch_to_file(where, fname, resample_to_8bit(patch)) exported.append(fname) - return success, exported \ No newline at end of file + return success, exported