MCPcopy Create free account
hub / github.com/apple/ml-4m / visualize_bboxes

Function visualize_bboxes

fourm/utils/plotting_utils.py:1042–1086  ·  view source on GitHub ↗

Visualizes bounding boxes on the image. Args: img (np.array): Image to draw bounding boxes on. bboxes_str (str): String containing bounding boxes in the format: v0=1 v1=2 v2=3 v3=4 class_name ..., where v0 is xmin, v1 is ymin, v2 is xmax, v3 is ymax

(img, bboxes_str, color=BOX_COLOR, thickness=2)

Source from the content-addressed store, hash-verified

1040 return input_img_overlay
1041
1042def visualize_bboxes(img, bboxes_str, color=BOX_COLOR, thickness=2):
1043 """
1044 Visualizes bounding boxes on the image.
1045
1046 Args:
1047 img (np.array): Image to draw bounding boxes on.
1048 bboxes_str (str): String containing bounding boxes in the format:
1049 v0=1 v1=2 v2=3 v3=4 class_name ..., where
1050 v0 is xmin, v1 is ymin, v2 is xmax, v3 is ymax
1051 color (tuple): Color of the bounding box.
1052 thickness (int): Thickness of the bounding box.
1053 """
1054 if img is None:
1055 img = 255 * np.ones((256,256,3), dtype=np.int32)
1056 img = img.copy()
1057
1058 bboxes_str = bboxes_str.replace('[PAD]', '')
1059
1060 if len(bboxes_str.replace('[EOS]', '')) == 0:
1061 return img
1062
1063 try:
1064 bboxes = convert_string_to_bboxes(bboxes_str.replace(' [EOS]', ''))
1065 except:
1066 return img
1067
1068 for bbox in bboxes:
1069 x_min, y_min, x_max, y_max, class_name = bbox
1070 img_h, img_w = img.shape[0], img.shape[1]
1071 x_min, x_max, y_min, y_max = int(x_min * img_w), int(x_max * img_w), int(y_min * img_h), int(y_max * img_h)
1072
1073 cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness)
1074
1075 ((text_width, text_height), _) = cv2.getTextSize(class_name.rstrip(), cv2.FONT_HERSHEY_SIMPLEX, 0.35, 1)
1076 cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), BOX_COLOR, -1)
1077 cv2.putText(
1078 img,
1079 text=f"{class_name}",
1080 org=(x_min, y_min - int(0.3 * text_height)),
1081 fontFace=cv2.FONT_HERSHEY_SIMPLEX,
1082 fontScale=0.35,
1083 color=TEXT_COLOR,
1084 lineType=cv2.LINE_AA,
1085 )
1086 return img
1087
1088
1089def plot_text_in_square(ax, text, padding=0.5, fontsize=14, wrap_width=50):

Callers 4

plot_modalitiesMethod · 0.90
modalities_to_pilMethod · 0.90
plot_modalityFunction · 0.85
save_conds_and_targetsFunction · 0.85

Calls 1

convert_string_to_bboxesFunction · 0.85

Tested by

no test coverage detected