基于Python實(shí)現(xiàn)RLE格式分割標(biāo)注文件的格式轉(zhuǎn)換
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.數(shù)據(jù)展示
2.1 標(biāo)注數(shù)據(jù)
該數(shù)據(jù)以csv格式存儲(chǔ),具體如下:

2.2 圖象文件



3.格式轉(zhuǎn)換
由于圖太多,暫時(shí)轉(zhuǎn)換10個(gè)
#!/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
# 將目標(biāo)路徑下的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/')其中為了方便查看,原計(jì)劃0為背景,1為mask,為了方便顯示,設(shè)置為255為mask。
4.轉(zhuǎn)換結(jié)果










到此這篇關(guān)于基于Python實(shí)現(xiàn)RLE格式分割標(biāo)注文件的格式轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python RLE文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中matplotlib實(shí)現(xiàn)最小二乘法擬合的過程詳解
這篇文章主要給大家介紹了關(guān)于python中matplotlib實(shí)現(xiàn)最小二乘法擬合的相關(guān)資料,文中通過示例代碼詳細(xì)介紹了關(guān)于最小二乘法擬合直線和最小二乘法擬合曲線的實(shí)現(xiàn)過程,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
Python?PyCharm無法打開終端命令行最終解決方案(實(shí)測(cè)成功)
這篇文章主要介紹了在使用PyCharm?2024版本時(shí)遇到的無法打開終端的問題,文中提供了兩種解決方案,大家可以根據(jù)自己的需求選擇對(duì)應(yīng)的解決方法,需要的朋友可以參考下2024-12-12
使用Python實(shí)現(xiàn)搖號(hào)系統(tǒng)的詳細(xì)步驟
這篇文章主要介紹了如何使用Python構(gòu)建一個(gè)簡(jiǎn)單的搖號(hào)系統(tǒng),包括需求分析、技術(shù)棧、實(shí)現(xiàn)步驟和完整代碼示例,該系統(tǒng)能夠從用戶輸入的參與者名單中隨機(jī)抽取指定數(shù)量的中獎(jiǎng)?wù)?并將結(jié)果展示給用戶以及記錄到日志文件中,需要的朋友可以參考下2024-11-11
python進(jìn)階教程之動(dòng)態(tài)類型詳解
這篇文章主要介紹了python進(jìn)階教程之動(dòng)態(tài)類型詳解,動(dòng)態(tài)類型是動(dòng)態(tài)語(yǔ)言的特性,本文對(duì)多種動(dòng)態(tài)類型應(yīng)用做了講解,需要的朋友可以參考下2014-08-08

