Skip to content
Snippets Groups Projects
Commit 006a1e35 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Moved patch box annotation to annotators module

parent 5515df64
No related branches found
No related tags found
No related merge requests found
import numpy as np import numpy as np
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from model_server.process import rescale
def draw_boxes_on_2d_image(img, boxes, **kwargs): def draw_boxes_on_2d_image(img, boxes, **kwargs):
pilimg = Image.fromarray(np.copy(img)) # drawing modifies array in-place pilimg = Image.fromarray(np.copy(img)) # drawing modifies array in-place
draw = ImageDraw.Draw(pilimg) draw = ImageDraw.Draw(pilimg)
...@@ -41,4 +43,12 @@ def draw_boxes_on_3d_image(img, boxes, draw_full_depth=False, **kwargs): ...@@ -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 assert clip >= 0.0 and clip <= 1.0
annotated = rescale(annotated, clip=clip) annotated = rescale(annotated, clip=clip)
return annotated return annotated
\ No newline at end of file
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
...@@ -6,6 +6,7 @@ from PIL import Image, ImageDraw ...@@ -6,6 +6,7 @@ from PIL import Image, ImageDraw
from skimage.io import imsave from skimage.io import imsave
from tifffile import imwrite from tifffile import imwrite
from extensions.chaeo.annotators import draw_box_on_patch
from model_server.accessors import GenericImageDataAccessor from model_server.accessors import GenericImageDataAccessor
from model_server.process import pad, rescale, resample_to_8bit from model_server.process import pad, rescale, resample_to_8bit
...@@ -67,10 +68,14 @@ def export_patches_from_zstack( ...@@ -67,10 +68,14 @@ def export_patches_from_zstack(
y1 = rbb['y1'] y1 = rbb['y1']
for zi in range(0, patch.shape[3]): for zi in range(0, patch.shape[3]):
pilimg = Image.fromarray(patch[:, :, 0, zi]) # drawing modifies array in-place # pilimg = Image.fromarray(patch[:, :, 0, zi]) # drawing modifies array in-place
draw = ImageDraw.Draw(pilimg) # draw = ImageDraw.Draw(pilimg)
draw.rectangle([(x0, y0), (x1, y1)], outline='white', width=kwargs.get('linewidth', 1)) # draw.rectangle([(x0, y0), (x1, y1)], outline='white', width=kwargs.get('linewidth', 1))
patch[:, :, 0, zi] = np.array(pilimg) # patch[:, :, 0, zi] = np.array(pilimg)
patch[:, :, 0, zi] = draw_box_on_patch(
patch[:, :, 0, zi],
((x0, y0), (x1, y1)),
)
if pad_to: if pad_to:
patch = pad(patch, pad_to) patch = pad(patch, pad_to)
...@@ -79,4 +84,4 @@ def export_patches_from_zstack( ...@@ -79,4 +84,4 @@ def export_patches_from_zstack(
fname = f'{prefix}-la{obj.label:04d}-zi{obj.zi:04d}.{ext}' fname = f'{prefix}-la{obj.label:04d}-zi{obj.zi:04d}.{ext}'
success = _write_patch_to_file(where, fname, resample_to_8bit(patch)) success = _write_patch_to_file(where, fname, resample_to_8bit(patch))
exported.append(fname) exported.append(fname)
return success, exported return success, exported
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment