基于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
猜你喜欢
- 最近遇到了Python访问SqlServer的问题,这里总结下。一、Windows下配置Python访问Sqlserver环境:Window
- 对于个人网站来说,受到建站条件的制约,Access数据库成了广大个人网站站长的首选。然而,Access数据库本身存在很多安全隐患,攻击者一旦
- ajax开发中, 常遇到下面的几种情况: 1 服务端需要返回一段普通文本给客户端 2 服务端需要返回一段HTML代码给客户端 3 服务端需要
- 在Web 开发中,JavaScript的一个很重要的作用就是对DOM进行操作,可你知道么?对DOM的操作是非常昂贵的,因为这会导致浏览器执行
- 本文实例讲述了Python实现向服务器请求压缩数据及解压缩数据的方法。分享给大家供大家参考,具体如下:向服务器请求压缩数据格式,并解压缩数据
- 一、基本用法1.注释Python中,#+语句 即为一条注释,也可以用 ‘'‘注释块 '''#人生苦短,我用P
- 大名鼎鼎的FCKeditor终于在最近发布新版本了,与增加版本号不同,这次完全把它改名了,更名为CKeditor。这应该是和它的开发公司CK
- 本文实例讲述了python将文本转换成图片输出的方法。分享给大家供大家参考。具体实现方法如下:#-*- coding:utf-8 -*-fr
- javascript 代码实现vbscript中的trim、left、right等函数兼容IE,FireFox。<style>b
- 问题:我们每天都要编写一些Python程序,或者用来处理一些文本,或者是做一些系统管理工作。程序写好后,只需要敲下python命令,便可将程
- MooTools 1.2介绍原文地址:30 Days of Mootools 1.2 Tutorials - Day 1 - Intro t
- 正态分布应用最广泛的连续概率分布,其特征是“钟”形曲线。这种分布的概率密度函数为:其中,μ为均值,σ为标准差。求正态分布曲线下面积有3σ原则
- 分享两个常用的代码生成工具:gormgenhandlergengormgen基于 MySQL 数据表结构进行生成 3 个文件:生成表的 st
- python类class定义及其初始化定义类,功能,属性一般类名首字母大写class Calculator:#名字和价格是属性
- 一、发送消息import smtplibfrom email.mime.text import MIMETextfrom email.hea
- 在各类的前端开发工具里,在功能上虽然Editplus显得有些“单薄”,但是仍然是很多我辈做开发的人们离不开的工具,因为他小巧,语言高亮,支持
- 首先下载最新版本的python。www.python.org,目前版本为3.1。 接下来是安装,在windows下python的安装与其他应
- --sql语句就用下面的存储过程 /*--数据导出Excel导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件,如果文件不
- 最近准备使用Python+Hadoop+Pandas进行一些深度的分析与机器学习相关工作。(当然随着学习过程的进展,现在准备使用Python
- 查看python搜索包的路径的实现方法:python搜索包的路径存储在sys.path下查看方法:import syssys.path临时添