2727import PIL .ImageDraw as ImageDraw
2828import PIL .ImageFont as ImageFont
2929import six
30- from six .moves import range
31- from six .moves import zip
3230import tensorflow .compat .v1 as tf
3331
3432from visualize import shape_utils
@@ -105,7 +103,7 @@ def save_image_array_as_png(image, output_path):
105103 output_path: path to which image should be written.
106104 """
107105 image_pil = Image .fromarray (np .uint8 (image )).convert ('RGB' )
108- with tf .gfile .Open (output_path , 'w' ) as fid :
106+ with tf .io . gfile .open (output_path , 'w' ) as fid :
109107 image_pil .save (fid , 'PNG' )
110108
111109
@@ -214,7 +212,7 @@ def draw_bounding_box_on_image(image,
214212 # If the total height of the display strings added to the top of the bounding
215213 # box exceeds the top of the image, stack the strings below the bounding box
216214 # instead of above.
217- display_str_heights = [font .getsize (ds )[1 ] for ds in display_str_list ]
215+ display_str_heights = [font .getbbox ( ds )[ 3 ] - font . getbbox (ds )[1 ] for ds in display_str_list ]
218216 # Each display_str has a top and bottom margin of 0.05x.
219217 total_display_str_height = (1 + 2 * 0.05 ) * sum (display_str_heights )
220218
@@ -224,7 +222,8 @@ def draw_bounding_box_on_image(image,
224222 text_bottom = bottom + total_display_str_height
225223 # Reverse list and print from bottom to top.
226224 for display_str in display_str_list [::- 1 ]:
227- text_width , text_height = font .getsize (display_str )
225+ left , top , right , bottom = font .getbbox (display_str )
226+ text_width , text_height = (right - left , bottom - top )
228227 margin = np .ceil (0.05 * text_height )
229228 draw .rectangle ([(left , text_bottom - text_height - 2 * margin ),
230229 (left + text_width , text_bottom )],
@@ -300,9 +299,9 @@ def create_visualization_fn(category_index,
300299 include_keypoints = False ,
301300 include_track_ids = False ,
302301 ** kwargs ):
303- """Constructs a visualization function that can be wrapped in a py_func .
302+ """Constructs a visualization function that can be wrapped in a numpy_function .
304303
305- py_funcs only accept positional arguments. This function returns a suitable
304+ numpy_functions only accept positional arguments. This function returns a suitable
306305 function with the correct positional argument mapping. The positional
307306 arguments in order are:
308307 0: image
@@ -317,15 +316,15 @@ def create_visualization_fn(category_index,
317316 vis_only_masks_fn = create_visualization_fn(category_index,
318317 include_masks=True, include_keypoints=False, include_track_ids=False,
319318 **kwargs)
320- image = tf.py_func (vis_only_masks_fn,
319+ image = tf.numpy_function (vis_only_masks_fn,
321320 inp=[image, boxes, classes, scores, masks],
322321 Tout=tf.uint8)
323322
324323 -- Example 2 --
325324 vis_masks_and_track_ids_fn = create_visualization_fn(category_index,
326325 include_masks=True, include_keypoints=False, include_track_ids=True,
327326 **kwargs)
328- image = tf.py_func (vis_masks_and_track_ids_fn,
327+ image = tf.numpy_function (vis_masks_and_track_ids_fn,
329328 inp=[image, boxes, classes, scores, masks, track_ids],
330329 Tout=tf.uint8)
331330
@@ -346,7 +345,7 @@ def create_visualization_fn(category_index,
346345 """
347346
348347 def visualization_py_func_fn (* args ):
349- """Visualization function that can be wrapped in a tf.py_func .
348+ """Visualization function that can be wrapped in a tf.numpy_function .
350349
351350 Args:
352351 *args: First 4 positional arguments must be: image - uint8 numpy array
@@ -496,11 +495,11 @@ def draw_boxes(image_and_detections):
496495 if original_image_spatial_shape is not None :
497496 image_and_detections [2 ] = _resize_original_image (image , original_shape )
498497
499- image_with_boxes = tf .py_func (visualize_boxes_fn , image_and_detections [2 :],
498+ image_with_boxes = tf .numpy_function (visualize_boxes_fn , image_and_detections [2 :],
500499 tf .uint8 )
501500 return image_with_boxes
502501
503- images = tf .map_fn (draw_boxes , elems , dtype = tf .uint8 , back_prop = False )
502+ images = tf .map_fn (draw_boxes , elems , fn_output_signature = tf .uint8 , back_prop = False )
504503 return images
505504
506505
@@ -1106,8 +1105,8 @@ def image_summary_or_default_string(summary_name, image):
11061105 update_op = self .add_images ([[images [0 ]]]) # pylint: disable=assignment-from-none
11071106 image_tensors = get_images ()
11081107 else :
1109- update_op = tf .py_func (self .add_images , [[images [0 ]]], [])
1110- image_tensors = tf .py_func (get_images , [],
1108+ update_op = tf .numpy_function (self .add_images , [[images [0 ]]], [])
1109+ image_tensors = tf .numpy_function (get_images , [],
11111110 [tf .uint8 ] * self ._max_examples_to_draw )
11121111 eval_metric_ops = {}
11131112 for i , image in enumerate (image_tensors ):
0 commit comments