基于Python实现RLE格式分割标注文件的格式转换

 更新时间:2022年08月18日 09:18:57   作者:Livingbody  
本文将以Airbus Ship Detection Challenge为例,为大家详细讲解Python实现RLE格式分割标注文件格式转换的方法,感兴趣的可以了解一下

1.Airbus Ship Detection Challenge

url: https://www.kaggle.com/competitions/airbus-ship-detection

Find ships on satellite images as quickly as possible

Data Description

In this competition, you are required to locate ships in images, and put an aligned bounding box segment around the ships you locate. Many images do not contain ships, and those that do may contain multiple ships. Ships within and across images may differ in size (sometimes significantly) and be located in open sea, at docks, marinas, etc.

For this metric, object segments cannot overlap. There were a small percentage of images in both the Train and Test set that had slight overlap of object segments when ships were directly next to each other. Any segments overlaps were removed by setting them to background (i.e., non-ship) encoding. Therefore, some images have a ground truth may be an aligned bounding box with some pixels removed from an edge of the segment. These small adjustments will have a minimal impact on scoring, since the scoring evaluates over increasing overlap thresholds.

The train_ship_segmentations.csv file provides the ground truth (in run-length encoding format) for the training images. The sample_submission files contains the images in the test images.

Please click on each file / folder in the Data Sources section to get more information about the files.

kaggle competitions download -c airbus-ship-detection

2.数据展示

2.1 标注数据

该数据以csv格式存储,具体如下:

2.2 图象文件

3.格式转换

由于图太多,暂时转换10个

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
from PIL import Image


# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode
# 将图片编码成rle格式
def rle_encode(img, min_max_threshold=1e-3, max_mean_threshold=None):
    '''
    img: numpy array, 1 - mask, 0 - background
    Returns run length as string formated
    '''
    if np.max(img) < min_max_threshold:
        return ''  ## no need to encode if it's all zeros
    if max_mean_threshold and np.mean(img) > max_mean_threshold:
        return ''  ## ignore overfilled mask
    pixels = img.T.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return ' '.join(str(x) for x in runs)


# 将图片从rle解码
def rle_decode(mask_rle, shape=(768, 768)):
    '''
    mask_rle: run-length as string formated (start length)
    shape: (height,width) of array to return
    Returns numpy array, 1 - mask, 0 - background
    '''
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        # img[lo:hi] = 1
        img[lo:hi] = 255 #方便可视化
    return img.reshape(shape).T  # Needed to align to RLE direction


def masks_as_image(in_mask_list):
    # Take the individual ship masks and create a single mask array for all ships
    all_masks = np.zeros((768, 768), dtype=np.uint8)
    for mask in in_mask_list:
        if isinstance(mask, str):
            all_masks |= rle_decode(mask)
    return all_masks


# 将目标路径下的rle文件中所包含的所有rle编码,保存到save_img_dir中去
def rle_2_img(train_rle_dir, save_img_dir):
    masks = pd.read_csv(train_rle_dir)
    not_empty = pd.notna(masks.EncodedPixels)
    print(not_empty.sum(), 'masks in', masks[not_empty].ImageId.nunique(), 'images')
    print((~not_empty).sum(), 'empty images in', masks.ImageId.nunique(), 'total images')
    all_batchs = list(masks.groupby('ImageId'))
    train_images = []
    train_masks = []
    i = 0
    for img_id, mask in all_batchs[:10]:
        c_mask = masks_as_image(mask['EncodedPixels'].values)
        im = Image.fromarray(c_mask)
        im.save(save_img_dir + img_id.split('.')[0] + '.png')
        print(i, img_id.split('.')[0] + '.png')
        i += 1

    return train_images, train_masks


if __name__ == '__main__':
    rle_2_img('train_ship_segmentations_v2.csv',
              'mask/')

其中为了方便查看,原计划0为背景,1为mask,为了方便显示,设置为255为mask。

4.转换结果

到此这篇关于基于Python实现RLE格式分割标注文件的格式转换的文章就介绍到这了,更多相关Python RLE文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • python中matplotlib实现最小二乘法拟合的过程详解

    python中matplotlib实现最小二乘法拟合的过程详解

    这篇文章主要给大家介绍了关于python中matplotlib实现最小二乘法拟合的相关资料,文中通过示例代码详细介绍了关于最小二乘法拟合直线和最小二乘法拟合曲线的实现过程,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-07-07
  • Python PyCharm无法打开终端命令行最终解决方案(实测成功)

    Python PyCharm无法打开终端命令行最终解决方案(实测成功)

    这篇文章主要介绍了在使用PyCharm 2024版本时遇到的无法打开终端的问题,文中提供了两种解决方案,大家可以根据自己的需求选择对应的解决方法,需要的朋友可以参考下
    2024-12-12
  • python正则表达式用法超详细讲解大全

    python正则表达式用法超详细讲解大全

    正则表达式是一种用来匹配字符串的强有力的武器,利用字符来匹配字符的思想,基于显示规则进行模式匹配,可以高效组合成不同样式的字符串,下面这篇文章主要给大家介绍了关于python正则表达式用法超详细讲解的相关资料,需要的朋友可以参考下
    2022-10-10
  • pytorch实践线性模型3d详解

    pytorch实践线性模型3d详解

    这篇文章主要介绍了pytorch实践线性模型3d详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • python+rsync精确同步指定格式文件

    python+rsync精确同步指定格式文件

    这篇文章主要为大家详细介绍了python+rsync精确同步指定格式文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • 使用Python实现摇号系统的详细步骤

    使用Python实现摇号系统的详细步骤

    这篇文章主要介绍了如何使用Python构建一个简单的摇号系统,包括需求分析、技术栈、实现步骤和完整代码示例,该系统能够从用户输入的参与者名单中随机抽取指定数量的中奖者,并将结果展示给用户以及记录到日志文件中,需要的朋友可以参考下
    2024-11-11
  • python Opencv将图片转为字符画

    python Opencv将图片转为字符画

    这篇文章主要为大家详细介绍了python Opencv将图片转为字符画的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python爬虫技术

    Python爬虫技术

    本文将要介绍的是python爬虫基础知识,感兴趣的小伙伴一起来学习吧
    2021-08-08
  • Python类的继承用法示例

    Python类的继承用法示例

    这篇文章主要介绍了Python类的继承用法,结合实例形式分析了Python类的定义、继承等相关操作技巧,需要的朋友可以参考下
    2019-01-01
  • python进阶教程之动态类型详解

    python进阶教程之动态类型详解

    这篇文章主要介绍了python进阶教程之动态类型详解,动态类型是动态语言的特性,本文对多种动态类型应用做了讲解,需要的朋友可以参考下
    2014-08-08

最新评论