def test_restore_result(): from mmtrack.core import restore_result num_classes = 3 num_objects = [2, 0, 2] result = [np.random.randn(num_objects[i], 5) for i in range(num_classes)] bboxes, labels = restore_result(result, return_ids=False) assert bboxes.shape == (4, 5) assert (labels == np.array([0, 0, 2, 2])).all() result = [np.random.randn(num_objects[i], 6) for i in range(num_classes)] bboxes, labels, ids = restore_result(result, return_ids=True) assert bboxes.shape == (4, 5) assert (labels == np.array([0, 0, 2, 2])).all() assert len(ids) == 4
def __call__(self, results): detections = results['detections'] bboxes, labels = restore_result(detections) results['public_bboxes'] = bboxes[:, :4] if bboxes.shape[1] > 4: results['public_scores'] = bboxes[:, -1] results['public_labels'] = labels results['bbox_fields'].append('public_bboxes') return results
def format_track_results(self, results, infos, resfile): """Format tracking results.""" with open(resfile, 'wt') as f: for res, info in zip(results, infos): if 'mot_frame_id' in info: frame = info['mot_frame_id'] else: frame = info['frame_id'] + 1 bboxes, labels, ids = restore_result(res, return_ids=True) for bbox, label, id in zip(bboxes, labels, ids): x1, y1, x2, y2, conf = bbox f.writelines( f'{frame},{id},{x1:.3f},{y1:.3f},{(x2-x1):.3f},' + f'{(y2-y1):.3f},{conf:.3f},-1,-1,-1\n')
def format_bbox_results(self, results, infos, resfile): """Format detection results.""" with open(resfile, 'wt') as f: for res, info in zip(results, infos): if 'mot_frame_id' in info: frame = info['mot_frame_id'] else: frame = info['frame_id'] + 1 bboxes, labels = restore_result(res) for bbox, label in zip(bboxes, labels): x1, y1, x2, y2, conf = bbox f.writelines( f'{frame},-1,{x1:.3f},{y1:.3f},{(x2-x1):.3f},' + f'{(y2-y1):.3f},{conf:.3f}\n') f.close()
def show_result(self, img, result, thickness=1, font_scale=0.5, show=False, out_file=None, wait_time=0, backend='cv2', **kwargs): """Visualize tracking results. Args: img (str | ndarray): Filename of loaded image. result (dict): Tracking result. The value of key 'track_results' is ndarray with shape (n, 6) in [id, tl_x, tl_y, br_x, br_y, score] format. The value of key 'bbox_results' is ndarray with shape (n, 5) in [tl_x, tl_y, br_x, br_y, score] format. thickness (int, optional): Thickness of lines. Defaults to 1. font_scale (float, optional): Font scales of texts. Defaults to 0.5. show (bool, optional): Whether show the visualizations on the fly. Defaults to False. out_file (str | None, optional): Output filename. Defaults to None. backend (str, optional): Backend to draw the bounding boxes, options are `cv2` and `plt`. Defaults to 'cv2'. Returns: ndarray: Visualized image. """ assert isinstance(result, dict) track_result = result.get('track_results', None) bboxes, labels, ids = restore_result(track_result, return_ids=True) img = imshow_tracks( img, bboxes, labels, ids, classes=self.CLASSES, thickness=thickness, font_scale=font_scale, show=show, out_file=out_file, wait_time=wait_time, backend=backend) return img
def show_result(self, img, result, thickness=1, font_scale=0.5, show=False, out_file=None, wait_time=0, backend='cv2'): """Visualize tracking results. Args: img (str | ndarray): Filename of loaded image. result (list[ndarray]): Tracking results. thickness (int, optional): Thickness of lines. Defaults to 1. font_scale (float, optional): Font scales of texts. Defaults to 0.5. show (bool, optional): Whether show the visualizations on the fly. Defaults to False. out_file (str | None, optional): Output filename. Defaults to None. backend (str, optional): Backend to draw the bounding boxes, options are `cv2` and `plt`. Defaults to 'cv2'. Returns: ndarray: Visualized image. """ bboxes, labels, ids = restore_result(result, return_ids=True) img = imshow_tracks(img, bboxes, labels, ids, classes=self.CLASSES, thickness=thickness, font_scale=font_scale, show=show, out_file=out_file, wait_time=wait_time, backend=backend) return img