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)