def __init__(self, indexes=[1], scores=[0.9]): assert utils.is_type_list(indexes, int) assert utils.is_type_list(scores, float) assert utils.equal_len(indexes, scores) self.indexes = indexes self.scores = scores
def show_feature(features, names, to_uint8, out_file=None): """Visualize a list of feature maps. Args: features (list(ndarray)): The feature map list. names (list(str)): The visualized title list. to_uint8 (list(1|0)): The list indicating whether to convent feature maps to uint8. out_file (str): The output file name. If set to None, the output image will be shown without saving. """ assert utils.is_ndarray_list(features) assert utils.is_type_list(names, str) assert utils.is_type_list(to_uint8, int) assert utils.is_none_or_type(out_file, str) assert utils.equal_len(features, names, to_uint8) num = len(features) row = col = math.ceil(math.sqrt(num)) for i, (f, n) in enumerate(zip(features, names)): plt.subplot(row, col, i + 1) plt.title(n) if to_uint8[i]: f = f.astype(np.uint8) plt.imshow(f) if out_file is None: plt.show() else: plt.savefig(out_file)
def imshow_text_char_boundary(img, text_quads, boundaries, char_quads, chars, show=False, thickness=1, font_scale=0.5, win_name='', wait_time=-1, out_file=None): """Draw text boxes and char boxes on img. Args: img (str or ndarray): The img to be displayed. text_quads (list[list[int|float]]): The text boxes. boundaries (list[list[int|float]]): The boundary list. char_quads (list[list[list[int|float]]]): A 2d list of char boxes. char_quads[i] is for the ith text, and char_quads[i][j] is the jth char of the ith text. chars (list[list[char]]). The string for each text box. thickness (int): Thickness of lines. font_scale (float): Font scales of texts. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str or None): The filename of the output. """ assert isinstance(img, (np.ndarray, str)) assert utils.is_2dlist(text_quads) assert utils.is_2dlist(boundaries) assert utils.is_3dlist(char_quads) assert utils.is_2dlist(chars) assert utils.equal_len(text_quads, char_quads, boundaries) img = mmcv.imread(img) char_color = [mmcv.color_val('blue'), mmcv.color_val('green')] text_color = mmcv.color_val('red') text_inx = 0 for text_box, boundary, char_box, txt in zip(text_quads, boundaries, char_quads, chars): text_box = np.array(text_box) boundary = np.array(boundary) text_box = text_box.reshape(-1, 2).astype(np.int32) cv2.polylines(img, [text_box.reshape(-1, 1, 2)], True, color=text_color, thickness=thickness) if boundary.shape[0] > 0: cv2.polylines(img, [boundary.reshape(-1, 1, 2)], True, color=text_color, thickness=thickness) for b in char_box: b = np.array(b) c = char_color[text_inx % 2] b = b.astype(np.int32) cv2.polylines(img, [b.reshape(-1, 1, 2)], True, color=c, thickness=thickness) label_text = ''.join(txt) cv2.putText(img, label_text, (text_box[0, 0], text_box[0, 1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) text_inx = text_inx + 1 if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def imshow_pred_boundary(img, boundaries_with_scores, labels, score_thr=0, boundary_color='blue', text_color='blue', thickness=1, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None, show_score=False): """Draw boundaries and class labels (with scores) on an image. Args: img (str or ndarray): The image to be displayed. boundaries_with_scores (list[list[float]]): Boundaries with scores. labels (list[int]): Labels of boundaries. score_thr (float): Minimum score of boundaries to be shown. boundary_color (str or tuple or :obj:`Color`): Color of boundaries. text_color (str or tuple or :obj:`Color`): Color of texts. thickness (int): Thickness of lines. font_scale (float): Font scales of texts. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str or None): The filename of the output. show_score (bool): Whether to show text instance score. """ assert isinstance(img, (str, np.ndarray)) assert utils.is_2dlist(boundaries_with_scores) assert utils.is_type_list(labels, int) assert utils.equal_len(boundaries_with_scores, labels) if len(boundaries_with_scores) == 0: warnings.warn('0 text found in ' + out_file) return utils.valid_boundary(boundaries_with_scores[0]) img = mmcv.imread(img) scores = np.array([b[-1] for b in boundaries_with_scores]) inds = scores > score_thr boundaries = [boundaries_with_scores[i][:-1] for i in np.where(inds)[0]] scores = [scores[i] for i in np.where(inds)[0]] labels = [labels[i] for i in np.where(inds)[0]] boundary_color = mmcv.color_val(boundary_color) text_color = mmcv.color_val(text_color) font_scale = 0.5 for boundary, score, label in zip(boundaries, scores, labels): boundary_int = np.array(boundary).astype(np.int32) cv2.polylines(img, [boundary_int.reshape(-1, 1, 2)], True, color=boundary_color, thickness=thickness) if show_score: label_text = f'{score:.02f}' cv2.putText(img, label_text, (boundary_int[0], boundary_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img