MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / draw_rotated_box_with_label

Method draw_rotated_box_with_label

utils/visualizer.py:943–996  ·  view source on GitHub ↗

Draw a rotated box with label on its top-left corner. Args: rotated_box (tuple): a tuple containing (cnt_x, cnt_y, w, h, angle), where cnt_x and cnt_y are the center coordinates of the box. w and h are the width and height of the box. ang

(
        self, rotated_box, alpha=0.5, edge_color="g", line_style="-", label=None
    )

Source from the content-addressed store, hash-verified

941 return self.output
942
943 def draw_rotated_box_with_label(
944 self, rotated_box, alpha=0.5, edge_color="g", line_style="-", label=None
945 ):
946 """
947 Draw a rotated box with label on its top-left corner.
948
949 Args:
950 rotated_box (tuple): a tuple containing (cnt_x, cnt_y, w, h, angle),
951 where cnt_x and cnt_y are the center coordinates of the box.
952 w and h are the width and height of the box. angle represents how
953 many degrees the box is rotated CCW with regard to the 0-degree box.
954 alpha (float): blending efficient. Smaller values lead to more transparent masks.
955 edge_color: color of the outline of the box. Refer to `matplotlib.colors`
956 for full list of formats that are accepted.
957 line_style (string): the string to use to create the outline of the boxes.
958 label (string): label for rotated box. It will not be rendered when set to None.
959
960 Returns:
961 output (VisImage): image object with box drawn.
962 """
963 cnt_x, cnt_y, w, h, angle = rotated_box
964 area = w * h
965 # use thinner lines when the box is small
966 linewidth = self._default_font_size / (
967 6 if area < _SMALL_OBJECT_AREA_THRESH * self.output.scale else 3
968 )
969
970 theta = angle * math.pi / 180.0
971 c = math.cos(theta)
972 s = math.sin(theta)
973 rect = [(-w / 2, h / 2), (-w / 2, -h / 2), (w / 2, -h / 2), (w / 2, h / 2)]
974 # x: left->right ; y: top->down
975 rotated_rect = [(s * yy + c * xx + cnt_x, c * yy - s * xx + cnt_y) for (xx, yy) in rect]
976 for k in range(4):
977 j = (k + 1) % 4
978 self.draw_line(
979 [rotated_rect[k][0], rotated_rect[j][0]],
980 [rotated_rect[k][1], rotated_rect[j][1]],
981 color=edge_color,
982 linestyle="--" if k == 1 else line_style,
983 linewidth=linewidth,
984 )
985
986 if label is not None:
987 text_pos = rotated_rect[1] # topleft corner
988
989 height_ratio = h / np.sqrt(self.output.height * self.output.width)
990 label_color = self._change_color_brightness(edge_color, brightness_factor=0.7)
991 font_size = (
992 np.clip((height_ratio - 0.02) / 0.08 + 1, 1.2, 2) * 0.5 * self._default_font_size
993 )
994 self.draw_text(label, text_pos, color=label_color, font_size=font_size, rotation=angle)
995
996 return self.output
997
998 def draw_circle(self, circle_coord, color, radius=3):
999 """

Callers 1

Calls 3

draw_lineMethod · 0.95
draw_textMethod · 0.95

Tested by

no test coverage detected