1. 程式人生 > >Tensorflow Object Detection API之MaskRCNN-資料處理篇

Tensorflow Object Detection API之MaskRCNN-資料處理篇

TensorFlow官網介紹:Run an Instance Segmentation Model

要求將資料處理為PNG Instance Segmentation Masks格式在這裡插入圖片描述
以下部分為處理單張Mask圖片的方式:

from PIL import Image, ImageDraw
import io
import numpy as np
mask = Image.open('1a22ac58f61b4def1c6f7ca582d93ea6.png')
mask = np.array(mask, dtype = np.uint8)

x, y = np.where((mask == 255))
polygon = []
for i in range(len(x)):
        polygon.append((x[i],y[i]))
        # print(len(a))
        # print(a)
polygon = tuple(polygon)
print('polygon:\n',polygon)

mask = Image.fromarray(mask)
draw = ImageDraw.Draw(mask)
# draw.polygon(xy=xy, outline=1, fill=1)
draw.polygon(polygon, outline=1, fill=1)
#With this i don't have to save the cropped image in my hard disc and I'm able to retrieve the byte array from a PIL cropped image.
imgByteArr = io.BytesIO()# 建立一個空的Bytes物件
mask.save(imgByteArr, format='PNG')# PNG就是圖片格式,換成JPG/jpg都不行
imgByteArr = imgByteArr.getvalue() # 這個就是儲存的圖片位元組流
print(imgByteArr)

參考連結