Ejemplo n.º 1
0
    def bbox_flip(self, bboxes, img_shape):
        """Flip bboxes horizontally.

        Args:
            bboxes(ndarray): shape (..., 5*k)
            img_shape(tuple): (height, width)
        """
        assert bboxes.shape[-1] % 5 == 0
        w = img_shape[1]
        # x_ctr and angle
        bboxes[..., 0::5] = w - bboxes[..., 0::5] - 1
        bboxes[..., 4::5] = norm_angle(np.pi - bboxes[..., 4::5])

        return bboxes
Ejemplo n.º 2
0
    def get_ann_info(self, idx):
        """Get annotation from XML file by index.

        Args:
            idx (int): Index of data.

        Returns:
            dict: Annotation info of specified index.
        """

        img_id = self.img_infos[idx]['id']
        xml_path = osp.join(self.img_prefix, 'Annotations', f'{img_id}.xml')
        tree = ET.parse(xml_path)
        root = tree.getroot()
        bboxes = []
        labels = []
        bboxes_ignore = []
        labels_ignore = []
        for obj in root.findall('HRSC_Objects')[0].findall('HRSC_Object'):
            label = self.cat2label['ship']
            difficult = int(obj.find('difficult').text)
            bbox = []
            for key in ['mbox_cx', 'mbox_cy', 'mbox_w', 'mbox_h', 'mbox_ang']:
                bbox.append(obj.find(key).text)
            # TODO: check whether it is necessary to use int
            # Coordinates may be float type
            cx, cy, w, h, a = list(map(float, bbox))
            # set w the long side and h the short side
            new_w, new_h = max(w, h), min(w, h)
            # adjust angle
            a = a if w > h else a + np.pi / 2
            # normalize angle to [-np.pi/4, pi/4*3]
            a = norm_angle(a)
            bbox = [cx, cy, new_w, new_h, a]

            ignore = False
            if self.min_size:
                assert not self.test_mode
                if bbox[2] < self.min_size or bbox[3] < self.min_size:
                    ignore = True
            if difficult or ignore:
                bboxes_ignore.append(bbox)
                labels_ignore.append(label)
            else:
                bboxes.append(bbox)
                labels.append(label)
        if not bboxes:
            bboxes = np.zeros((0, 5))
            labels = np.zeros((0,))
        else:
            bboxes = np.array(bboxes, ndmin=2)
            labels = np.array(labels)
        if not bboxes_ignore:
            bboxes_ignore = np.zeros((0, 5))
            labels_ignore = np.zeros((0,))
        else:
            bboxes_ignore = np.array(bboxes_ignore, ndmin=2)
            labels_ignore = np.array(labels_ignore)
        ann = dict(
            bboxes=bboxes.astype(np.float32),
            labels=labels.astype(np.int64),
            bboxes_ignore=bboxes_ignore.astype(np.float32),
            labels_ignore=labels_ignore.astype(np.int64))
        return ann
Ejemplo n.º 3
0
    def get_ann_info(self, idx):
        img_id = self.img_infos[idx]['id']
        xml_path = osp.join(self.img_prefix, 'Annotations',
                            '{}.xml'.format(img_id))
        tree = ET.parse(xml_path)
        root = tree.getroot()
        bboxes = []
        labels = []
        bboxes_ignore = []
        labels_ignore = []
        for obj in root.findall('object'):
            name = obj.find('name').text
            label = self.cat2label[name]
            difficult = int(obj.find('difficult').text)
            bnd_box = obj.find('robndbox')
            bbox = [
                float(bnd_box.find('cx').text),
                float(bnd_box.find('cy').text),
                float(bnd_box.find('w').text),
                float(bnd_box.find('h').text),
                float(bnd_box.find('angle').text) + 1  # important!
            ]

            # TODO: check whether it is necessary to use int
            # Coordinates may be float type
            cx, cy, w, h, a = list(map(float, bbox))
            # set w the long side and h the short side
            new_w, new_h = max(w, h), min(w, h)
            # adjust angle
            a = a if w > h else a + np.pi / 2
            # normalize angle to [-np.pi/4, pi/4*3]
            a = norm_angle(a)
            bbox = [cx, cy, new_w, new_h, a]

            ignore = False
            if self.min_size:
                assert not self.test_mode
                w = bbox[2] - bbox[0]
                h = bbox[3] - bbox[1]
                if w < self.min_size or h < self.min_size:
                    ignore = True
            if difficult or ignore:
                bboxes_ignore.append(bbox)
                labels_ignore.append(label)
            else:
                bboxes.append(bbox)
                labels.append(label)
        if not bboxes:
            bboxes = np.zeros((0, 5))
            labels = np.zeros((0, ))
        else:
            bboxes = np.array(bboxes, ndmin=2) - 1
            labels = np.array(labels)
        if not bboxes_ignore:
            bboxes_ignore = np.zeros((0, 5))
            labels_ignore = np.zeros((0, ))
        else:
            bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1
            labels_ignore = np.array(labels_ignore)
        ann = dict(bboxes=bboxes.astype(np.float32),
                   labels=labels.astype(np.int64),
                   bboxes_ignore=bboxes_ignore.astype(np.float32),
                   labels_ignore=labels_ignore.astype(np.int64))
        return ann