from matplotlib import cm def get_pre_define_colors(num_classes, cmap_set: list[str] = None, in_rgb: bool = True, is_float: bool = False): if cmap_set is None: cmap_set = ['tab20', 'tab20b', 'tab20c'] colors = [] for cmap in cmap_set: colors.extend(list(cm.get_cmap(cmap).colors)) if in_rgb: new_colors = [] for color in colors: if is_float: new_colors.append(tuple(color)) else: new_colors.append(tuple(int(x * 255) for x in color)) colors = new_colors if num_classes > len(colors): print(f"WARNING: {num_classes} classes are requested, but only {len(colors)} colors are available. Predefined colors will be reused.") colors *= num_classes // len(colors) + 1 else: colors = colors[:num_classes] return colors def xyxy_to_xywh(box): return [box[0], box[1], box[2] - box[0], box[3] - box[1]]