基于Python实现RLE格式分割标注文件的格式转换
作者:Livingbody 发布时间:2022-10-22 08:41:12
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.转换结果
来源:https://juejin.cn/post/7132880900198498312


猜你喜欢
- onchange在用于文本框输入框时,有一个明显的不足. 事件不会随着文字的输入而触发,而是等到文本框失去焦点(onblur)时才会触发.
- 常见面试题Vue 如何监控数组defineProperty 真的不能监测数组变化吗?Vue 是如何追踪数据发生变化在 Vue 中当我们把一个
- 函数声明为:func Replace(s, old, new string, n int) string官方描述为:返回将s中前n个不重叠o
- 介绍Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户
- Win10系统安装MySQL8.0遇到的问题及解决方法,具体内容如下所示:对着第一个桌面应用击右键,选择“以管理员身份运行”选项,就可以以管
- Python则是通过缩进来识别代码块的。缩进Python最具特色的是用缩进来标明成块的代码。我下面以if选择结构来举例。if后面跟随条件,如
- 有不少朋友在开发爬虫的过程中喜欢使用Selenium + Chromedriver,以为这样就能做到不被网站的反爬虫机制发现。先不说淘宝这种
- 这是我为了学习tkinter用python 写的一个下载m3u8视频的小程序,程序使用了多线程下载,下载后自动合并成一个视频文件,方便播放。
- 问题背景 基于PyQt5开发了一个可以用于目标跟踪的软件,在开发过程中遇到一个问题,就是如何在PyQt5的组件QLable中自主选定目标框
- os.path模块是os模块根据系统类型从另一个模块导入的,并非由os模块实现1、os.path.abspath(相对路径)-----返回对
- 摘要:利用xlrd读取excel利用xlwt写excel利用xlutils修改excel利用xlrd读取excel先需要在命令行中pip i
- 安装环境:centos 5.4mysql版本:mysql 5.1.xx 采用rpm直接安装所需软件: xtrabackup 1.2.22 采
- 本文实例为大家分享了Go实现文件上传和下载的具体代码,供大家参考,具体内容如下一.文件上传文件上传:客户端把上传文件转换为二进制流后发送给服
- 用python3.x实现base64加密和解密,供大家参考,具体内容如下加密base64_encrypt.py#!/usr/bin/pyth
- 背景最近在用Electron开发一款应用,其中有涉及到检测因特网是否断开的需求。Electron基于Chromium和Node.js,让你可
- 代码如下:create table A_TEST ( PAYOUT_ITEM_CODE VARCHAR2(30) not null, FOR
- Anaconda是一个开源的Python发行版本,包括Conda、Python以及一大堆安装好的工具包,比如:numpy、pandas等。其
- 本文实例讲述了Python列表推导式与生成器表达式用法。分享给大家供大家参考,具体如下:和列表一样,列表推导式也采用方括号[]表示,并且用到
- 在数据库开发过程中,当你检索的数据只是一条记录时,你所编写的事务语句代码往往使用SELECT INSERT 语句。但是我们常常会遇到这样情况
- 界面文件 Ui_ControlBoard.py# -*- coding: utf-8 -*-# Form implementation ge