def bbox2region(bbox): """Convert bbox to Rectangle or Polygon Class object. Args: bbox (ndarray): the format of rectangle bbox is (x1, y1, w, h); the format of polygon is (x1, y1, x2, y2, ...). Returns: Rectangle or Polygon Class object. """ if vot is None: raise ImportError( 'Please run' 'pip install git+https://github.com/votchallenge/toolkit.git' 'to manually install vot-toolkit') if len(bbox) == 1: return Special(bbox[0]) elif len(bbox) == 4: return Rectangle(bbox[0], bbox[1], bbox[2], bbox[3]) elif len(bbox) % 2 == 0 and len(bbox) > 4: return Polygon([(x_, y_) for x_, y_ in zip(bbox[::2], bbox[1::2])]) else: raise NotImplementedError( f'The length of bbox is {len(bbox)}, which is not supported')
def convert_traxregion(region: TraxRegion) -> Region: if region.type == TraxRegion.RECTANGLE: x, y, width, height = region.bounds() return Rectangle(x, y, width, height) elif region.type == TraxRegion.POLYGON: return Polygon(list(region)) elif region.type == TraxRegion.MASK: return Mask(region.array(), region.offset(), optimize=True) return None
def _generate(base, length, size): background_color = Image.fromarray(np.random.normal(15, 5, (size[1], size[0], 3)).astype(np.uint8)) background_depth = Image.fromarray(np.ones((size[1], size[0]), dtype=np.uint8) * 200) background_ir = Image.fromarray(np.zeros((size[1], size[0]), dtype=np.uint8)) template = Image.open(os.path.join(os.path.dirname(__file__), "cow.png")) dir_color = os.path.join(base, "color") dir_depth = os.path.join(base, "depth") dir_ir = os.path.join(base, "ir") os.makedirs(dir_color, exist_ok=True) os.makedirs(dir_depth, exist_ok=True) os.makedirs(dir_ir, exist_ok=True) path_color = os.path.join(dir_color, "%08d.jpg") path_depth = os.path.join(dir_depth, "%08d.png") path_ir = os.path.join(dir_ir, "%08d.png") groundtruth = [] center_x = size[0] / 2 center_y = size[1] / 2 radius = min(center_x - template.size[0], center_y - template.size[1]) speed = (math.pi * 2) / length for i in range(length): frame_color = background_color.copy() frame_depth = background_depth.copy() frame_ir = background_ir.copy() x = int(center_x + math.cos(i * speed) * radius - template.size[0] / 2) y = int(center_y + math.sin(i * speed) * radius - template.size[1] / 2) frame_color.paste(template, (x, y), template) frame_depth.paste(10, (x, y), template) frame_ir.paste(240, (x, y), template) frame_color.save(path_color % (i + 1)) frame_depth.save(path_depth % (i + 1)) frame_ir.save(path_ir % (i + 1)) groundtruth.append(Rectangle(x, y, template.size[0], template.size[1])) write_trajectory(os.path.join(base, "groundtruth.txt"), groundtruth) metadata = {"name": "dummy", "fps" : 30, "format" : "dummy", "channel.default": "color"} write_properties(os.path.join(base, "sequence"), metadata)
def test_convert_region(self): convert_region(Rectangle(0, 0, 10, 10)) convert_region(Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])) convert_region(Mask(np.ones((100, 100), dtype=np.uint8)))
def test_calculate_overlap(self): from vot.region import Rectangle r1 = Rectangle(0, 0, 100, 100) self.assertEqual(calculate_overlap(r1, r1), 1)