欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python實(shí)現(xiàn)RLE格式分割標(biāo)注文件的格式轉(zhuǎn)換

 更新時(shí)間:2022年08月18日 09:18:57   作者:Livingbody  
本文將以Airbus Ship Detection Challenge為例,為大家詳細(xì)講解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格式存儲,具體如下:

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)將兩個(gè)文件夾合并至另一個(gè)文件夾(制作數(shù)據(jù)集)

    python實(shí)現(xiàn)將兩個(gè)文件夾合并至另一個(gè)文件夾(制作數(shù)據(jù)集)

    這篇文章主要介紹了python實(shí)現(xiàn)將兩個(gè)文件夾合并至另一個(gè)文件夾(制作數(shù)據(jù)集),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python解析json串與正則匹配對比方法

    python解析json串與正則匹配對比方法

    今天小編就為大家分享一篇python解析json串與正則匹配對比方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python讀取文件夾中圖片的圖片名并寫入excel表格

    python讀取文件夾中圖片的圖片名并寫入excel表格

    這篇文章介紹了使用python讀取文件夾中圖片的圖片名并寫入excel表格的方法。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • python復(fù)制文件代碼實(shí)現(xiàn)

    python復(fù)制文件代碼實(shí)現(xiàn)

    最近在備份手機(jī)上的照片的時(shí)候,純手工操作覺得有些麻煩,就想寫個(gè)腳本自動(dòng)進(jìn)行。因?yàn)閭浞莸臅r(shí)候有些照片以前備份過了,所以需要有個(gè)判重操作,看下面的實(shí)現(xiàn)代碼
    2013-12-12
  • 淺析python redis的連接及相關(guān)操作

    淺析python redis的連接及相關(guān)操作

    Redis是一個(gè)開源的使用ANSIC語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。這篇文章主要介紹了python redis的連接及相關(guān)操作,需要的朋友可以參考下
    2019-11-11
  • Python讀取word文本操作詳解

    Python讀取word文本操作詳解

    這篇文章主要介紹了Python讀取word文本操作詳解,介紹了涉及到的模塊,相關(guān)概念,模塊的安裝等內(nèi)容,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • python 圖片去噪的方法示例

    python 圖片去噪的方法示例

    這篇文章主要介紹了python 圖片去噪的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python監(jiān)控鍵盤輸入實(shí)例代碼

    python監(jiān)控鍵盤輸入實(shí)例代碼

    這篇文章主要介紹了python監(jiān)控鍵盤輸入實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • python dataframe如何選擇某一列非空的行

    python dataframe如何選擇某一列非空的行

    這篇文章主要介紹了python dataframe如何選擇某一列非空的行問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 對pytorch中x = x.view(x.size(0), -1) 的理解說明

    對pytorch中x = x.view(x.size(0), -1) 的理解說明

    這篇文章主要介紹了對pytorch中x = x.view(x.size(0), -1) 的理解說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論