How to create a label (mask) for segmentation with labelme (semantic segmentation mask)

image.png

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

image.png

Prerequisite knowledge

I am writing an article on the assumption that I have the following knowledge

labelme is an app for creating labels

It's an app like this

Details on how to use labelme

Download label me

For windows

For mac

Label with label me

When you open labelme, the following window will open. (The image is a screenshot of the google search screen. It's hard to understand ...) image.png

While expanding, draw a line at the point. It's very easy to zoom in and out. image.png

I think it's about 1min with this proper feeling. Pretty easy to use. .. image.png

Save the JSON from SAVE on the left. You can get data like this!

{
  "version": "4.5.6",
  "flags": {},
  "shapes": [
    {
      "label": "dog",
      "points": [
        [
          104.36893203883496,
          66.99029126213593
        ],
        [
          93.44660194174757,
          71.35922330097087
        ],
Omitted here
        [
          112.13592233009709,
          74.27184466019418
        ],
        [
          107.28155339805825,
          73.30097087378641
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "imagePath": "Screenshot 2020-09-14 141400.png ",
  "imageData": "abridgement",
  "imageHeight": 405,
  "imageWidth": 535
}

Caution! Since this JSON point is a polygon point, it cannot be used as a mask as it is. .. So we need to convert this point to a mask.

Convert labelme file from JSON to MASK

At first I was thinking of implementing it myself (it's a hassle), but there was a function for it (that's right). labelme has a module called shape.py in the utils folder, and you can convert from JSON to MASK by using the shape_to_mask function.

However, I have to implement it subtly, so I will post the sample code.

import json
with open(path, "r",encoding="utf-8") as f:
    dj = json.load(f)
# dj['shapes'][0]Is for one label this time.
mask = shape_to_mask((dj['imageHeight'],dj['imageWidth']), dj['shapes'][0]['points'], shape_type=None,line_width=1, point_size=1)
mask_img = mask.astype(np.int)#boolean to 0,Convert to 1

#I'm using anaconda
import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(mask_img)

image.png

I was able to label it well. At this level, it takes about 2-3 minutes from opening the image to saving it, isn't it?

Finally

Please let me know if you have any recommended labeling tools other than label me. I wonder if some people have implemented it on their own.

Recommended Posts

How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
How to create a shortcut command for LINUX
How to create a multi-platform app with kivy
How to create a submenu with the [Blender] plugin
[Go] How to create a custom error for Sentry
How to create a local repository for Linux OS
[Python] How to create a 2D histogram with Matplotlib
How to create a SAS token for Azure IoT Hub
How to create a Conda package
How to create a virtual bridge
How to create a Dockerfile (basic)
How to create a config file
How to create a heatmap with an arbitrary domain in Python
How to write a docstring to create a named tuple document with sphinx
How to create a serverless machine learning API with AWS Lambda
How to create a git clone folder
How to add a package with PyCharm
How to create a repository from media
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
How to create sample CSV data with hypothesis
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
How to create a Python virtual environment (venv)
How to draw a 2-axis graph with pyplot
How to create a function object from a string
Create a LINE BOT with Minette for Python
How to develop a cart app with Django
How to create a JSON file in Python
How to make a dictionary with a hierarchical structure.
Create a dashboard for Network devices with Django!
Steps to create a Twitter bot with python
How to write a ShellScript Bash for statement
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a Rest Api in Django
[Note] How to create a Mac development environment
How to create random numbers with NumPy's random module
What I learned by implementing how to create a Default Box for SSD
I tried to create a button for Slack with Raspberry Pi + Tact Switch
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
[Introduction to Python] How to get the index of data with a for statement
Read the Python-Markdown source: How to create a parser
Create a dataset of images to use for learning
How to convert / restore a string with [] in python
I tried to create a table only with Django
[Python] How to draw a line graph with Matplotlib
Try to dynamically create a Checkbutton with Python's Tkinter
How to get a logged-in user with Django's forms.py
How to convert a class object to a dictionary with SQLAlchemy
How to quickly create a machine learning environment using Jupyter Notebook with UbuntuServer 16.04 LTS
I tried to create a reinforcement learning environment for Othello with Open AI gym
Create a Layer for AWS Lambda Python with Docker
I want to manually create a legend with matplotlib
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to create a simple TCP server / client script
A less likely misunderstanding about how to specify a handler for a zip uploaded with Lambda
How to create a kubernetes pod from python code
[Python] How to draw a scatter plot with Matplotlib
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to make a Cisco Webex Teams BOT with Flask