def load(self, add_gt=True, add_mask=False):
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'image_id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        if add_mask:
            assert add_gt
        with timed_operation('Load Groundtruth Boxes for {}'.format(self.name)):
            img_ids = self.coco.getImgIds()
            img_ids.sort()
            # list of dict, each has keys: height,width,id,file_name
            imgs = self.coco.loadImgs(img_ids)

            for img in tqdm.tqdm(imgs):
                img['image_id'] = img.pop('id')
                self._use_absolute_file_name(img)
                if add_gt:
                    self._add_detection_gt(img, add_mask)
            return imgs
Esempio n. 2
0
    def get_training_bbox(bbox_dir, imglist):
        import xml.etree.ElementTree as ET
        ret = []

        def parse_bbox(fname):
            root = ET.parse(fname).getroot()
            size = root.find('size').getchildren()
            size = map(int, [size[0].text, size[1].text])

            box = root.find('object').find('bndbox').getchildren()
            box = map(lambda x: float(x.text), box)
            return np.asarray(box, dtype='float32')

        with timed_operation('Loading Bounding Boxes ...'):
            cnt = 0
            for k in tqdm.trange(len(imglist)):
                fname = imglist[k][0]
                fname = fname[:-4] + 'xml'
                fname = os.path.join(bbox_dir, fname)
                try:
                    ret.append(parse_bbox(fname))
                    cnt += 1
                except Exception:
                    ret.append(None)
            logger.info("{}/{} images have bounding box.".format(cnt, len(imglist)))
        return ret
Esempio n. 3
0
  def load(self, add_gt=True, add_mask=False):
    """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the
              dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'image_id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and
                optionally
                'segmentation'.
        """
    with timed_operation("Load annotations for {}".format(
        os.path.basename(self.annotation_file))):
      img_ids = self.coco.getImgIds()
      img_ids.sort()
      # list of dict, each has keys: height,width,id,file_name
      imgs = self.coco.loadImgs(img_ids)

      for idx, img in enumerate(tqdm.tqdm(imgs)):
        img["image_id"] = img.pop("id")
        img["file_name"] = os.path.join(self._imgdir, img["file_name"])
        if idx == 0:
          # make sure the directories are correctly set
          assert os.path.isfile(img["file_name"]), img["file_name"]
        if add_gt:
          self._add_detection_gt(img, add_mask)
      return imgs
Esempio n. 4
0
    def load(self,
             add_gt=True,
             add_mask=False):  # 로딩 함수 (mask는 알겠는데 ground truth가 뭐지?)@@@@@
        """
        Args: 
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'height', 'width', 'id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        if add_mask:  # 만약 마스크가 있다면
            assert add_gt  # ground truth가 없으면 예외처리
        with timed_operation('Load Groundtruth Boxes for {}'.format(
                self.name)):  # timed_operation은 어디서 온거지?@@@@@
            img_ids = self.coco.getImgIds()  # 코코를 이용한 이미지 아이디 초기화
            img_ids.sort()  # 이미지 아이디 정렬
            # list of dict, each has keys: height,width,id,file_name
            imgs = self.coco.loadImgs(img_ids)  # 이미지 아이디를 인덱스로 한 이미지 로딩

            for img in tqdm.tqdm(imgs):  # tqdm으로 진행상황을 보여준다.
                self._use_absolute_file_name(
                    img)  # _use_absuloute_file_name 은 어디서 온거지? 바로 아래에서 온다.
                if add_gt:  # groung truth가 있다면
                    self._add_detection_gt(
                        img, add_mask
                    )  # _add_detection_gt함수를 수행하라. 근데 이 함수 뭐지? 아래에서 온다.
            return imgs  # 로딩함수의 리턴값은 이미지 객체이다.
Esempio n. 5
0
    def load(self, add_gt=True, add_mask=False):
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'height', 'width', 'id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        if add_mask:
            assert add_gt
        with timed_operation('Load Groundtruth Boxes for {}'.format(self.name)):
            img_ids = self.coco.getImgIds()
            img_ids.sort()
            # list of dict, each has keys: height,width,id,file_name
            imgs = self.coco.loadImgs(img_ids)

            for img in tqdm.tqdm(imgs):
                self._use_absolute_file_name(img)
                if add_gt:
                    self._add_detection_gt(img, add_mask)
            return imgs
Esempio n. 6
0
 def load(self, add_gt=True):
     with timed_operation("Load groundtruth boxes from {}".format(
             self.tfrecords_pattern)):
         examples = self.get_examples(self.tfrecords_pattern)
         result = []
         skipped = 0
         for i, example in enumerate(examples):
             image_path = example_to_str(example, 'image_path')
             old_path = example_to_str(example, 'path')
             w = example_to_int(example, 'width')
             h = example_to_int(example, 'height')
             bboxes = example_to_numpy(example, 'bboxes', np.float32,
                                       (-1, 4))
             if bboxes.shape[0] == 0:
                 skipped += 1
             assert len(bboxes.shape) == 2
             assert bboxes.shape[1] == 4
             lbl = example_to_int(example, 'label') + 1  # BACKGROUND is 0
             labels = np.asarray(list(repeat(lbl, len(bboxes))))
             is_crowd = np.zeros(len(bboxes))
             result.append({
                 'height': h,
                 'width': w,
                 'id': old_path,
                 'file_name': image_path,
                 'boxes': bboxes,
                 'class': labels,
                 'is_crowd': is_crowd,
                 'label': lbl - 1
             })
         logger.info('{} images have zero boxes.'.format(skipped))
         return result
Esempio n. 7
0
    def load(self, add_gt=True, add_mask=False):
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask

        Returns:
            a list of dict, each has keys including:
                'image_id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        # self.reload_coco()
        with timed_operation('Load annotations for {}'.format(
                os.path.basename(self.annotation_file_path))):
            img_ids_r = self.coco.getImgIds()

            if add_gt:
                img_ids = list(
                    filter(lambda x: x in self.coco.imgToAnns, img_ids_r))
            else:
                img_ids = img_ids_r

            img_ids.sort()
            # list of dict, each has keys: height,width,id,file_name
            imgs = copy.deepcopy(self.coco.loadImgs(img_ids))

            # print("drop images without annotations")
            # imgs = list(filter(lambda x: x in self.coco.imgToAnns, imgs_r))

            for idx, img in enumerate(tqdm.tqdm(imgs)):
                # img['image_id'] = img.pop('id')
                #  FIXME: why pop here, if use func to load COCOFormatDetectionSubset, then we can use pop
                #       when use func, it can prevent data
                img['image_id'] = img['id']
                img['file_name'] = img['path']
                if idx == 0:
                    # make sure the directories are correctly set
                    assert os.path.isfile(img["file_name"]), img["file_name"]
                if add_gt:
                    self._add_detection_gt(img, add_mask)
            return imgs
Esempio n. 8
0
    def load(self, add_gt=True, add_mask=False):
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts
            add_mask: whether to also add ground truth mask
        Returns:
            a list of dict, each has keys including:
                height, width, id, file_name,
                and (if add_gt is True) boxes, class, is_crowd
        """
        if add_mask:
            assert add_gt
        with timed_operation('Load Groundtruth Boxes for {}'.format(self.name)):
            img_ids = self.coco.getImgIds()
            img_ids.sort()
            # list of dict, each has keys: height,width,id,file_name
            imgs = self.coco.loadImgs(img_ids)

            for img in imgs:
                self._use_absolute_file_name(img)
                if add_gt:
                    self._add_detection_gt(img, add_mask)
            return imgs
Esempio n. 9
0
    def load(self, add_gt=True):
        """
        Args:
            add_gt: whether to add ground truth bounding box annotations to the dicts

        Returns:
            a list of dict, each has keys including:
                'height', 'width', 'id', 'file_name',
                and (if add_gt is True) 'boxes', 'class', 'is_crowd', and optionally
                'segmentation'.
        """
        assert add_gt == True, 'Temporal, add_gt must be true for now'

        with timed_operation('Load images and labels for {}'.format(
                self.name)):
            # first try to load from cache
            try:
                imgs = load_from_cache(self.name,
                                       ctime=os.path.getmtime(__file__))
                logger.info('Loaded from cache {}'.format(self.name + '.pkl'))
            except IOError:
                imgs = [{
                    'fn_img': f[0],
                    'fn_label': f[1]
                } for f in self._imageset]
                valid_imgs = []
                for img in imgs:
                    try:
                        self._use_absolute_file_name(img)
                    except IOError:
                        logger.info('skipping {}'.format(img['file_name']))
                        continue
                    valid_imgs.append(img)
                imgs, valid_imgs = valid_imgs, imgs
                save_to_cache(imgs, self.name)
            return imgs
Esempio n. 10
0
    def load(self, split_set='train'):
        """
        Args:
            split_set: ['train', 'val']

        Returns:
            a list of dict, each has keys including:
                'height', 'width', 'id', 'file_name',
                and (if split_set is 'train') 'boxes', 'class', 'is_crowd'.
        """
        with timed_operation('Load Groundtruth Boxes...'):
            frame_list_mat = scipy.io.loadmat(
                pjoin(self._basedir, 'frame_' + split_set + '.mat'))
            frame_list = frame_list_mat['img_index_' + split_set]

            imgs = []
            imgs_without_fg = 0

            # each iteration only reads one file so it's faster
            for idx, frame in enumerate(frame_list):
                img = {}

                self._use_absolute_file_name(img, frame[0][0])

                if split_set == 'train':
                    if frame[0][0][1] == '6':
                        img['height'] = 576
                        img['width'] = 720
                    else:
                        img['height'] = 1080
                        img['width'] = 1920

                    anno_data = scipy.io.loadmat(
                        pjoin(self._annodir, frame[0][0] + '.jpg.mat'))
                    if 'box_new' in anno_data:
                        gt_bb_array = anno_data['box_new']
                    elif 'anno_file' in anno_data:
                        gt_bb_array = anno_data['anno_file']
                    elif 'anno_previous' in anno_data:
                        gt_bb_array = anno_data['anno_previous']
                    else:
                        raise Exception(frame[0][0] +
                                        ' bounding boxes info missing!')

                    # if true, include gts that are bg as well
                    # todo: since this option will rarely be used, re-id class not added yet
                    include_all = cfg.DATA.INCLUDE_ALL
                    if include_all:
                        img['boxes'] = []
                        for bb in gt_bb_array:
                            box = FloatBox(bb[1], bb[2], bb[1] + bb[3],
                                           bb[2] + bb[4])
                            box.clip_by_shape([img['height'], img['width']])
                            img['boxes'].append(
                                [box.x1, box.y1, box.x2, box.y2])
                        img['boxes'] = np.asarray(img['boxes'],
                                                  dtype='float32')

                        img['class'] = np.ones(len(gt_bb_array))

                        img['re_id_class'] = np.asarray(gt_bb_array[:, 0] + 1,
                                                        dtype='int32')
                        img['re_id_class'][img['re_id_class'] == -1] = 1
                    else:
                        img['boxes'] = []
                        # the 2-class detection class, pedestrian/bg
                        img['class'] = []
                        img['re_id_class'] = []
                        for bb in gt_bb_array:
                            if bb[0] != -2:
                                box = FloatBox(bb[1], bb[2], bb[1] + bb[3],
                                               bb[2] + bb[4])
                                box.clip_by_shape(
                                    [img['height'], img['width']])
                                img['boxes'].append(
                                    [box.x1, box.y1, box.x2, box.y2])
                                img['class'].append(1)
                                img['re_id_class'].append(bb[0])
                            else:
                                continue

                        if len(img['boxes']) == 0:
                            imgs_without_fg += 1
                            continue
                        img['boxes'] = np.asarray(img['boxes'],
                                                  dtype='float32')
                        img['class'] = np.asarray(img['class'], dtype='int32')
                        img['re_id_class'] = np.asarray(img['re_id_class'],
                                                        dtype='int32')

                    img['is_crowd'] = np.zeros(len(img['re_id_class']),
                                               dtype='int8')

                imgs.append(img)

            print(
                'Number of images without identified pedestrians: {}.'.format(
                    imgs_without_fg))
            return imgs