Пример #1
0
    def __airbus_ship_parse__(self, label_file, image_file):
        """
        (xmin, ymin, xmax, ymax)
        """
        image_file_name = os.path.splitext(os.path.basename(image_file))[0]
        save_image = wwtool.generate_image(800, 800, color=(0, 0, 0))
        img = cv2.imread(image_file)
        img_height, img_width, _ = img.shape

        image_objects = airbus_ship_parse.airbus_ship_parse(os.path.basename(image_file))
        objects = []
        objects_small_save = []
        total_object_num = len(image_objects)
        small_object_num = 0
        large_object_num = 0
        total_object_num = 0
        for image_object in image_objects:
            object_struct = {}
            object_struct_small = {}
            xmin, ymin, xmax, ymax = image_object['bbox']
            bbox_w = xmax - xmin
            bbox_h = ymax - ymin

            if bbox_w * bbox_h <= self.small_object_area:
                continue

            total_object_num += 1
            if bbox_h * bbox_w <= small_size:
                small_object_num += 1
            if bbox_h * bbox_w >= large_object_size:
                large_object_num += 1

            object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h]
            object_struct['segmentation'] = wwtool.bbox2pointobb([xmin, ymin, xmax, ymax])
            object_struct['label'] = 1

            object_struct_small = object_struct.copy()
            object_struct_small['bbox'] = [xmin, ymin, xmax, ymax]
            object_struct_small['label'] = 'ship'
            
            objects_small_save.append(object_struct_small)
            objects.append(object_struct)
        
        if total_object_num > self.max_object_num_per_image:
            self.max_object_num_per_image = total_object_num

        if just_keep_small or generate_small_dataset:
            if small_object_num >= total_object_num * small_object_rate and large_object_num < 1 and len(objects_small_save) > 0:
                # print(os.path.join(dst_image_path, image_file_name + '.png'))
                save_image[0:img_height, 0:img_width, :] = img
                cv2.imwrite(os.path.join(dst_image_path, image_file_name + '.png'), save_image)
                anno_file = os.path.join(dst_label_path, image_file_name + '.txt')
                wwtool.simpletxt_dump(objects_small_save, anno_file)
                return objects
            else:
                return []
        else:
            return objects
Пример #2
0
    def split_image(self, image_fn):
        if not image_fn.endswith('.jpg'):
            return
        image_file = os.path.join(self.image_path, image_fn)
        shp_file = os.path.join(self.merged_shp_path, image_fn.replace('jpg', 'shp'))
        geo_file = os.path.join(self.geo_path, image_fn.replace('jpg', 'png'))
        
        file_name = os.path.splitext(os.path.basename(image_file))[0]

        if not os.path.exists(shp_file):
            return

        img = cv2.imread(image_file)
        geo_info = rio.open(geo_file)

        objects = self.shp_parser(shp_file, geo_info)

        masks = np.array([obj['segmentation'] for obj in objects])

        subimages = wwtool.split_image(img, subsize=self.subimage_size, gap=self.gap)
        subimage_coordinates = list(subimages.keys())

        if masks.shape[0] == 0:
            return

        mask_centroids = []
        for obj in objects:
            geometry = obj['converted_polygon'].centroid
            geo = geojson.Feature(geometry=geometry, properties={})
            coordinate = geo.geometry["coordinates"]
            coordinate[0], coordinate[1] = abs(coordinate[0]), abs(coordinate[1])
            mask_centroids.append(coordinate)

        mask_centroids = np.array(mask_centroids)
        mask_centroids_ = mask_centroids.copy()
        
        for subimage_coordinate in subimage_coordinates:
            objects = []

            mask_centroids_[:, 0] = mask_centroids[:, 0] - subimage_coordinate[0]
            mask_centroids_[:, 1] = mask_centroids[:, 1] - subimage_coordinate[1]


            cx_bool = np.logical_and(mask_centroids_[:, 0] >= 0, mask_centroids_[:, 0] < subimage_size)
            cy_bool = np.logical_and(mask_centroids_[:, 1] >= 0, mask_centroids_[:, 1] < subimage_size)

            subimage_masks = masks[np.logical_and(cx_bool, cy_bool)]

            subimage_masks_ = []
            for subimage_mask in subimage_masks:
                subimage_mask_np = np.array(subimage_mask)
                subimage_mask_np[0::2] = subimage_mask_np[0::2] - subimage_coordinate[0]
                subimage_mask_np[1::2] = subimage_mask_np[1::2] - subimage_coordinate[1]

                subimage_masks_.append(subimage_mask_np.tolist())
            
            subimage_masks = subimage_masks_
            if len(subimage_masks) == 0:
                continue
            img = subimages[subimage_coordinate]
            if np.mean(img) == 0:
                continue

            label_save_file = os.path.join(self.label_save_path, '{}__{}_{}.txt'.format(file_name, subimage_coordinate[0], subimage_coordinate[1]))
            image_save_file = os.path.join(self.image_save_path, '{}__{}_{}.png'.format(file_name, subimage_coordinate[0], subimage_coordinate[1]))
            cv2.imwrite(image_save_file, img)
            
            for subimage_mask in subimage_masks:
                subimage_objects = dict()
                subimage_objects['mask'] = subimage_mask
                subimage_objects['label'] = 'building'
                objects.append(subimage_objects)
            wwtool.simpletxt_dump(objects, label_save_file, encode='mask')
Пример #3
0
                bboxes_[:, 0] = bboxes[:, 0] - subimage_coordinate[0]
                bboxes_[:, 1] = bboxes[:, 1] - subimage_coordinate[1]
                cx_bool = np.logical_and(bboxes_[:, 0] >= 0,
                                         bboxes_[:, 0] < 1024)
                cy_bool = np.logical_and(bboxes_[:, 1] >= 0,
                                         bboxes_[:, 1] < 1024)
                subimage_bboxes = bboxes_[np.logical_and(cx_bool, cy_bool)]

                if len(subimage_bboxes) == 0:
                    continue
                img = subimages[subimage_coordinate]
                if np.mean(img) == 0:
                    continue

                label_save_file = os.path.join(
                    label_save_path,
                    '{}__{}_{}.txt'.format(file_name, subimage_coordinate[0],
                                           subimage_coordinate[1]))
                image_save_file = os.path.join(
                    image_save_path,
                    '{}__{}_{}.png'.format(file_name, subimage_coordinate[0],
                                           subimage_coordinate[1]))
                cv2.imwrite(image_save_file, img)

                for subimage_bbox in subimage_bboxes:
                    subimage_objects = dict()
                    subimage_objects['bbox'] = subimage_bbox.tolist()
                    subimage_objects['label'] = 'ship'
                    objects.append(subimage_objects)
                wwtool.simpletxt_dump(objects, label_save_file)
Пример #4
0
    def split_image(self, image_fn):
        if not image_fn.endswith('.jpg'):
            return
        image_file = os.path.join(self.image_path, image_fn)
        shp_file = os.path.join(self.roof_shp_path,
                                image_fn.replace('jpg', 'shp'))
        geo_file = image_file

        file_name = os.path.splitext(os.path.basename(image_file))[0]

        if not os.path.exists(shp_file):
            return

        img = cv2.imread(image_file)
        geo_info = rio.open(geo_file)

        objects = self.shp_parser(shp_file,
                                  geo_info,
                                  coord='pixel',
                                  ignore_file=None,
                                  merge_flag=True,
                                  connection_mode='floor')

        masks = np.array([obj['segmentation'] for obj in objects])

        subimages = wwtool.split_image(img,
                                       subsize=self.subimage_size,
                                       gap=self.gap)
        subimage_coordinates = list(subimages.keys())

        if masks.shape[0] == 0:
            return

        mask_centroids = []
        for obj in objects:
            geometry = obj['converted_polygon'].centroid
            geo = geojson.Feature(geometry=geometry, properties={})
            coordinate = geo.geometry["coordinates"]
            coordinate[0], coordinate[1] = abs(coordinate[0]), abs(
                coordinate[1])
            mask_centroids.append(coordinate)

        mask_centroids = np.array(mask_centroids)
        mask_centroids_ = mask_centroids.copy()

        for subimage_coordinate in subimage_coordinates:
            objects = []

            mask_centroids_[:,
                            0] = mask_centroids[:, 0] - subimage_coordinate[0]
            mask_centroids_[:,
                            1] = mask_centroids[:, 1] - subimage_coordinate[1]

            cx_bool = np.logical_and(mask_centroids_[:, 0] >= 0,
                                     mask_centroids_[:, 0] < subimage_size)
            cy_bool = np.logical_and(mask_centroids_[:, 1] >= 0,
                                     mask_centroids_[:, 1] < subimage_size)

            subimage_masks = masks[np.logical_and(cx_bool, cy_bool)]

            subimage_masks_ = []
            for subimage_mask in subimage_masks:
                if wwtool.mask2polygon(subimage_mask).area < 5:
                    continue
                subimage_mask_np = np.array(subimage_mask)
                subimage_mask_np[
                    0::2] = subimage_mask_np[0::2] - subimage_coordinate[0]
                subimage_mask_np[
                    1::2] = subimage_mask_np[1::2] - subimage_coordinate[1]

                subimage_masks_.append(subimage_mask_np.tolist())

            subimage_masks = subimage_masks_
            # cut the polygons by image boundary
            subimage_masks = wwtool.clip_mask(subimage_masks,
                                              image_size=(1024, 1024))

            # judge whether to drop this subimage
            drop_flag = self.drop_subimage(subimages,
                                           subimage_coordinate,
                                           subimage_masks,
                                           show=self.show)
            if drop_flag:
                continue

            img = subimages[subimage_coordinate]

            label_save_file = os.path.join(
                self.label_save_path,
                '{}_{}__{}_{}.txt'.format(self.sub_imageset_fold, file_name,
                                          subimage_coordinate[0],
                                          subimage_coordinate[1]))
            image_save_file = os.path.join(
                self.image_save_path,
                '{}_{}__{}_{}.png'.format(self.sub_imageset_fold, file_name,
                                          subimage_coordinate[0],
                                          subimage_coordinate[1]))
            cv2.imwrite(image_save_file, img)

            for subimage_mask in subimage_masks:
                subimage_objects = dict()
                subimage_objects['mask'] = subimage_mask
                subimage_objects['label'] = 'building'
                objects.append(subimage_objects)
            wwtool.simpletxt_dump(objects, label_save_file, encode='mask')
Пример #5
0
    origin_label_path = './data/dota-v1.5/v1/trainval/labelTxt-v1.5'

    filtered_image_path = './data/dota-v1.5/v1/trainval_iltered/images'
    filtered_label_path = './data/dota-v1.5/v1/trainval_filtered/labels'

    wwtool.mkdir_or_exist(filtered_image_path)
    wwtool.mkdir_or_exist(filtered_label_path)

    filter_count = 1
    progress_bar = mmcv.ProgressBar(len(os.listdir(origin_label_path)))
    for label_name in os.listdir(origin_label_path):
        image_objects = wwtool.dota_parse(os.path.join(origin_label_path, label_name))
        filtered_objects = []
        for image_object in image_objects:
            if convert_classes[image_object['label']] == None:
                filter_count += 1
                continue
            else:
                image_object['label'] = convert_classes[image_object['label']]
                filtered_objects.append(image_object)

        if len(filtered_objects) > 0:
            # img = cv2.imread(os.path.join(origin_image_path, os.path.splitext(label_name)[0] + image_format))
            # save_image_file = os.path.join(filtered_image_path, os.path.splitext(label_name)[0] + '.png')
            # print("Save image file: ", save_image_file)
            # cv2.imwrite(save_image_file, img)
            wwtool.simpletxt_dump(filtered_objects, os.path.join(filtered_label_path, os.path.splitext(label_name)[0] + '.txt'))
        
        progress_bar.update()

    print("Filter object counter: {}".format(filter_count))
Пример #6
0
                    subimage_mask_np[
                        0::2] = subimage_mask_np[0::2] - subimage_coordinate[0]
                    subimage_mask_np[
                        1::2] = subimage_mask_np[1::2] - subimage_coordinate[1]

                    subimage_masks_.append(subimage_mask_np.tolist())

                subimage_masks = subimage_masks_
                if len(subimage_masks) == 0:
                    continue
                img = subimages[subimage_coordinate]
                if np.mean(img) == 0:
                    continue

                label_save_file = os.path.join(
                    label_save_path,
                    '{}__{}_{}.txt'.format(file_name, subimage_coordinate[0],
                                           subimage_coordinate[1]))
                image_save_file = os.path.join(
                    image_save_path,
                    '{}__{}_{}.png'.format(file_name, subimage_coordinate[0],
                                           subimage_coordinate[1]))
                cv2.imwrite(image_save_file, img)

                for subimage_mask in subimage_masks:
                    subimage_objects = dict()
                    subimage_objects['mask'] = subimage_mask
                    subimage_objects['label'] = 'building'
                    objects.append(subimage_objects)
                wwtool.simpletxt_dump(objects, label_save_file, encode='mask')
Пример #7
0
    def __sn6_parse__(self, label_file, image_file):
        """
        (xmin, ymin, xmax, ymax)
        """
        objects = []
        if self.groundtruth:
            image_file_name = os.path.splitext(os.path.basename(image_file))[0]

            image_name_postfix = image_file_name + '.tif'
            if imageset == 'val':
                if image_name_postfix in valset_image_names:
                    pass
                else:
                    return []

            if imageset == 'train':
                if image_name_postfix in valset_image_names:
                    return []
                else:
                    pass

            save_image = wwtool.generate_image(800, 800, color=(0, 0, 0))
            img = cv2.imread(image_file)
            img_height, img_width, _ = img.shape

            image_name = os.path.basename(image_file).split(
                'SN6_{}_AOI_11_Rotterdam_{}_'.format(
                    imageset_file_name, data_source))[1].split('.tif')[0]
            masks = sn6_parse.sn6_parse(image_name)
            objects_small_save = []
            total_object_num = len(masks)
            small_object_num = 0
            large_object_num = 0
            total_object_num = 0
            for mask in masks:
                object_struct = {}
                object_struct_small = {}
                xmin, ymin, xmax, ymax = wwtool.pointobb2bbox(
                    mask['segmentation'])
                bbox_w = xmax - xmin
                bbox_h = ymax - ymin

                total_object_num += 1
                if bbox_h * bbox_w <= small_size:
                    small_object_num += 1
                if bbox_h * bbox_w >= large_object_size:
                    large_object_num += 1

                object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h]
                object_struct['segmentation'] = mask['segmentation']
                object_struct['label'] = 1

                object_struct_small = object_struct.copy()
                object_struct_small['bbox'] = [xmin, ymin, xmax, ymax]
                object_struct_small['label'] = 'building'

                objects_small_save.append(object_struct_small)
                objects.append(object_struct)

            if total_object_num > self.max_object_num_per_image:
                self.max_object_num_per_image = total_object_num

            if just_keep_small or generate_small_dataset:
                if small_object_num >= total_object_num * small_object_rate and large_object_num < 1 and len(
                        objects_small_save) > 0:
                    # print(os.path.join(dst_image_path, image_file_name + '.png'))
                    save_image[0:img_height, 0:img_width, :] = img
                    cv2.imwrite(
                        os.path.join(dst_image_path, image_file_name + '.png'),
                        save_image)
                    anno_file = os.path.join(dst_label_path,
                                             image_file_name + '.txt')
                    wwtool.simpletxt_dump(objects_small_save, anno_file)
                    return objects
                else:
                    return []
            else:
                return objects
        else:
            obj_struct = {}
            obj_struct['segmentation'] = [0, 0, 0, 0, 0, 0, 0, 0]
            obj_struct['bbox'] = [0, 0, 0, 0]
            obj_struct['label'] = 0

            objects.append(obj_struct)

            return objects