Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to extract Encoded PNG for each label for building TF Record #71

Closed
seahawks8 opened this issue Mar 15, 2018 · 4 comments
Closed

how to extract Encoded PNG for each label for building TF Record #71

seahawks8 opened this issue Mar 15, 2018 · 4 comments
Labels

Comments

@seahawks8
Copy link
Contributor

@seahawks8 seahawks8 commented Mar 15, 2018

I have trained several datasets, but am new to using mask
RCNN. I have already built a script to do the following with the output JSON file from labelme

  • read image, get height width
  • get normalized box coordinates (calculated by reading the points and getting min, max for X and Y)

my question, is how do I get a serialized PNG image for each label?
example
image/object/mask = ["\x89PNG\r\n\x1A\n\x00\x00\x00\rIHDR\...", ...]

@wkentaro

This comment has been minimized.

Copy link
Owner

@wkentaro wkentaro commented Mar 16, 2018

I think you can use lableme.utils.polygon_to_mask function:

labelme/labelme/utils.py

Lines 69 to 75 in 58693e6

def polygons_to_mask(img_shape, polygons):
mask = np.zeros(img_shape[:2], dtype=np.uint8)
mask = PIL.Image.fromarray(mask)
xy = list(map(tuple, polygons))
PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
mask = np.array(mask, dtype=bool)
return mask

Then convert the numpy array to a serialized PNG image.

@wkentaro wkentaro added the question label Mar 16, 2018
@seahawks8

This comment has been minimized.

Copy link
Contributor Author

@seahawks8 seahawks8 commented Mar 16, 2018

Thanks. I tried running that and I just end up with a blank png. A function that exports or shows how to get individual png files for each mask would be ideal. I will keep working on this.

@seahawks8

This comment has been minimized.

Copy link
Contributor Author

@seahawks8 seahawks8 commented Mar 17, 2018

Hey so I ended up being able to plot and see the Mask by modifying the function a little bit.
this will draw the PNG just black (background) and the mask will be white.
I wonder if I can just save that polygon png as-is and do the training with that

mask = PIL.Image.fromarray(mask)
xy = list(map(tuple, polygons))
draw = ImageDraw.Draw(mask)
draw.polygon(xy=xy, outline=3, fill=1)
mask.show()
@seahawks8

This comment has been minimized.

Copy link
Contributor Author

@seahawks8 seahawks8 commented Mar 17, 2018

Ok i ended up with this to get the serialized PNG

    mask = PIL.Image.fromarray(mask)
    xy = list(map(tuple, polygons))
    draw = ImageDraw.Draw(mask)
    draw.polygon(xy=xy, outline=1, fill=1)
    imgByteArr = io.BytesIO()
    mask.save(imgByteArr, format='PNG')
    imgByteArr = imgByteArr.getvalue()

output will be something along the lines of
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x07\x80\x00\x00\x048\x08\x00 .....

@seahawks8 seahawks8 closed this Mar 17, 2018
@wkentaro wkentaro added this to the Closed PRs / Issues for questions milestone May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.