def parse(string): """ parse string to the appropriate region format and return region object """ from vot.region.shapes import Rectangle, Polygon, Mask if string[0] == 'm': # input is a mask - decode it m_, offset_ = create_mask_from_string(string[1:].split(',')) return Mask(m_, offset=offset_) else: # input is not a mask - check if special, rectangle or polygon tokens = [float(t) for t in string.split(',')] if len(tokens) == 1: return Special(tokens[0]) if len(tokens) == 4: if any([math.isnan(el) for el in tokens]): return Special(0) else: return Rectangle(tokens[0], tokens[1], tokens[2], tokens[3]) elif len(tokens) % 2 == 0 and len(tokens) > 4: if any([math.isnan(el) for el in tokens]): return Special(0) else: return Polygon([(x_, y_) for x_, y_ in zip(tokens[::2], tokens[1::2])]) print('Unknown region format.') return None
def parse_region(string: str) -> "Region": """ Parse input string to the appropriate region format and return Region object Args: string (str): comma separated list of values Returns: Region: resulting region """ from vot.region import Special from vot.region.shapes import Rectangle, Polygon, Mask if string[0] == 'm': # input is a mask - decode it m_, offset_ = create_mask_from_string(string[1:].split(',')) return Mask(m_, offset=offset_) else: # input is not a mask - check if special, rectangle or polygon tokens = [float(t) for t in string.split(',')] if len(tokens) == 1: return Special(tokens[0]) if len(tokens) == 4: if any([math.isnan(el) for el in tokens]): return Special(0) else: return Rectangle(tokens[0], tokens[1], tokens[2], tokens[3]) elif len(tokens) % 2 == 0 and len(tokens) > 4: if any([math.isnan(el) for el in tokens]): return Special(0) else: return Polygon([(x_, y_) for x_, y_ in zip(tokens[::2], tokens[1::2])]) return None
def execute(self, tracker: Tracker, sequence: Sequence, force: bool = False, callback: Callable = None): results = self.results(tracker, sequence) with self._get_runtime(tracker, sequence) as runtime: for i in range(1, self.repetitions+1): name = "%s_%03d" % (sequence.name, i) if Trajectory.exists(results, name) and not force: continue if self._can_stop(tracker, sequence): return trajectory = Trajectory(sequence.length) frame = 0 while frame < sequence.length: _, properties, elapsed = runtime.initialize(sequence.frame(frame), self._get_initialization(sequence, frame)) properties["time"] = elapsed trajectory.set(frame, Special(Special.INITIALIZATION), properties) frame = frame + 1 while frame < sequence.length: region, properties, elapsed = runtime.update(sequence.frame(frame)) properties["time"] = elapsed if calculate_overlap(region, sequence.groundtruth(frame), sequence.size) <= self.failure_overlap: trajectory.set(frame, Special(Special.FAILURE), properties) frame = frame + self.skip_initialize if self.skip_tags: while frame < sequence.length: if not [t for t in sequence.tags(frame) if t in self.skip_tags]: break frame = frame + 1 break else: trajectory.set(frame, region, properties) frame = frame + 1 if callback: callback(i / self.repetitions) trajectory.write(results, name)
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 execute(self, tracker: Tracker, sequence: Sequence, force: bool = False, callback: Callable = None): results = self.results(tracker, sequence) with self._get_runtime(tracker, sequence) as runtime: for i in range(1, self.repetitions+1): name = "%s_%03d" % (sequence.name, i) if Trajectory.exists(results, name) and not force: continue if self._can_stop(tracker, sequence): return trajectory = Trajectory(sequence.length) _, properties, elapsed = runtime.initialize(sequence.frame(0), self._get_initialization(sequence, 0)) properties["time"] = elapsed trajectory.set(0, Special(Special.INITIALIZATION), properties) for frame in range(1, sequence.length): region, properties, elapsed = runtime.update(sequence.frame(frame)) properties["time"] = elapsed trajectory.set(frame, region, properties) trajectory.write(results, name) if callback: callback(i / self.repetitions)
def _read(self): channels = {} tags = {} values = {} groundtruth = [] root = os.path.dirname(os.path.dirname(self._base)) channels["color"] = load_channel( os.path.join(root, 'frames', self.name)) self._metadata["channel.default"] = "color" self._metadata["width"], self._metadata["height"] = six.next( six.itervalues(channels)).size groundtruth = read_trajectory(self._base) if len(groundtruth) == 1 and channels["color"].length > 1: # We are dealing with testing dataset, only first frame is available, so we pad the # groundtruth with unknowns. Only unsupervised experiment will work, but it is ok groundtruth.extend([Special(Special.UNKNOWN)] * (channels["color"].length - 1)) self._metadata["length"] = len(groundtruth) return channels, groundtruth, tags, values
def execute(self, tracker: Tracker, sequence: Sequence, force: bool = False, callback: Callable = None): results = self.results(tracker, sequence) forward, backward = find_anchors(sequence, self.anchor) if len(forward) == 0 and len(backward) == 0: raise RuntimeError("Sequence does not contain any anchors") total = len(forward) + len(backward) current = 0 with self._get_runtime(tracker, sequence) as runtime: for i, reverse in [(f, False) for f in forward] + [(f, True) for f in backward]: name = "%s_%08d" % (sequence.name, i) if Trajectory.exists(results, name) and not force: continue if reverse: proxy = FrameMapSequence(sequence, list(reversed(range(0, i + 1)))) else: proxy = FrameMapSequence(sequence, list(range(i, sequence.length))) trajectory = Trajectory(proxy.length) _, properties, elapsed = runtime.initialize( proxy.frame(0), self._get_initialization(proxy, 0)) properties["time"] = elapsed trajectory.set(0, Special(Special.INITIALIZATION), properties) for frame in range(1, proxy.length): region, properties, elapsed = runtime.update( proxy.frame(frame)) properties["time"] = elapsed trajectory.set(frame, region, properties) trajectory.write(results, name) current = current + 1 if callback: callback(current / total)
def __init__(self, length: int): self._regions = [Special(Special.UNKNOWN)] * length self._properties = dict()
def _read(self): channels = {} tags = {} values = {} groundtruth = [] channels["color"] = load_channel(os.path.join(self._base, "%08d.jpg")) self._metadata["channel.default"] = "color" self._metadata["width"], self._metadata["height"] = six.next( six.itervalues(channels)).size groundtruth_file = os.path.join( self._base, self.metadata("groundtruth", "groundtruth.txt")) groundtruth = read_trajectory(groundtruth_file) if len(groundtruth) == 1 and channels["color"].length > 1: # We are dealing with testing dataset, only first frame is available, so we pad the # groundtruth with unknowns. Only unsupervised experiment will work, but it is ok groundtruth.extend([Special(Special.UNKNOWN)] * (channels["color"].length - 1)) self._metadata["length"] = len(groundtruth) tagfiles = glob.glob(os.path.join(self._base, '*.label')) for tagfile in tagfiles: with open(tagfile, 'r') as filehandle: tagname = os.path.splitext(os.path.basename(tagfile))[0] tag = [line.strip() == "1" for line in filehandle.readlines()] while not len(tag) >= len(groundtruth): tag.append(False) tags[tagname] = tag valuefiles = glob.glob(os.path.join(self._base, '*.value')) for valuefile in valuefiles: with open(valuefile, 'r') as filehandle: valuename = os.path.splitext(os.path.basename(valuefile))[0] value = [ float(line.strip()) for line in filehandle.readlines() ] while not len(value) >= len(groundtruth): value.append(0.0) values[valuename] = value for name, channel in channels.items(): if not channel.length == len(groundtruth): raise DatasetException("Length mismatch for channel %s" % name) for name, tag in tags.items(): if not len(tag) == len(groundtruth): tag_tmp = len(groundtruth) * [False] tag_tmp[:len(tag)] = tag tag = tag_tmp for name, value in values.items(): if not len(value) == len(groundtruth): raise DatasetException("Length mismatch for value %s" % name) return channels, groundtruth, tags, values