#@title Plot Utilities for Bounding Boxes [RUN ME]
im_width = 75
im_height = 75
use_normalized_coordinates = True
def draw_bounding_boxes_on_image_array(image,
boxes,
color=,
thickness=1,
display_str_list=()):
“”“Draws bounding boxes on image (numpy array).
Args:
image: a numpy array object.
boxes: a 2 dimensional numpy array of [N, 4]: (ymin, xmin, ymax, xmax).
The coordinates are in normalized format between [0, 1].
colour: color to draw bounding box. Default is red.
thickness: line thickness. The default value is 4.
display_str_list_list: a list of strings for each bounding box.
Raises:
ValueError: if boxes is not a [N, 4] array
“””
image_pil = PIL.Image.fromarray(image)
rgbimg = PIL.Image.new(“RGBA”, image_pil.size)
rgbimg.paste(image_pil)
draw_bounding_boxes_on_image(rgbimg, boxes, color, thickness,
display_str_list)
return np.array(rgbimg)
def draw_bounding_boxes_on_image(image,
boxes,
color=,
thickness=1,
display_str_list=()):
“”"Draws bounding boxes on image.
Args:
image: a PIL.Image object.
boxes: a 2 dimensional numpy array of [N, 4]: (ymin, xmin, ymax, xmax).
The coordinates are in normalized format between [0, 1].
color: color to draw bounding box. Default is red.
thickness: line thickness. Default value is 4.
display_str_list: a list of strings for each bounding box.
Raises:
ValueError: if boxes is not a [N, 4] array
“”"
boxes_shape = boxes.shape
if not boxes_shape:
return
if len(boxes_shape) != 2 or boxes_shape[1] != 4:
raise ValueError(‘Input must be of size [N, 4]’)
for i in range(boxes_shape[0]):
draw_bounding_box_on_image(image, boxes[i, 1], boxes[i, 0], boxes[i, 3],
boxes[i, 2], color[i], thickness, display_str_list[i])
def draw_bounding_box_on_image(image,
ymin,
xmin,
ymax,
xmax,
color=‘red’,
thickness=1,
display_str=None,
use_normalized_coordinates=True):
“”“Adds a bounding box to an image.
Bounding box coordinates can be specified in either absolute (pixel) or
normalized coordinates by setting the use_normalized_coordinates argument.
Args:
image: a PIL.Image object.
ymin: ymin of bounding box.
xmin: xmin of bounding box.
ymax: ymax of bounding box.
xmax: xmax of bounding box.
color: color to draw bounding box. Default is red.
thickness: line thickness. Default value is 4.
display_str_list: string to display in box
use_normalized_coordinates: If True (default), treat coordinates
ymin, xmin, ymax, xmax as relative to the image. Otherwise treat
coordinates as absolute.
“””
draw = PIL.ImageDraw.Draw(image)
im_width, im_height = image.size
if use_normalized_coordinates:
(left, right, top, bottom) = (xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
else:
(left, right, top, bottom) = (xmin, xmax, ymin, ymax)
draw.line([(left, top), (left, bottom), (right, bottom),
(right, top), (left, top)], width=thickness, fill=color)
I don’t understand how this function helps to place each digit image at random locations since these lines :
image_pil = PIL.Image.fromarray(image)
rgbimg = PIL.Image.new(“RGBA”, image_pil.size) // create a 75x75 black canvas
rgbimg.paste(image_pil) // paste the 75 x 75 image on the black canvas.
I can’t find a line that randomize the digit location.