Skip to content

Commit cba9558

Browse files
LutaoChunepeplwu
authored andcommitted
添加精灵标注 (#20)
* Create README2.md * Create jingling2seg.md * Delete README2.md * Add files via upload * Add files via upload * Update jingling2seg.md * Update README.md * Create labelme2seg.md * Update jingling2seg.md * Update jingling2seg.md * Update README.md * Add files via upload * Update README.md * Update README.md * Update README.md * Update README.md * Add files via upload * Add files via upload
1 parent 12966d4 commit cba9558

12 files changed

+306
-91
lines changed

docs/annotation/README.md

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,12 @@
11
# PaddleSeg 数据标注
22

3-
用户需预先采集好用于训练、评估和测试的图片,并使用数据标注工具[LabelMe](https://github.com/wkentaro/labelme)完成数据标注,最后用我们提供的数据转换脚本将LabelMe产出的数据格式转换为模型训练时所需的数据格式
3+
用户需预先采集好用于训练、评估和测试的图片,并使用数据标注工具完成数据标注
44

5-
## 1 LabelMe的安装
5+
PaddleSeg支持2种标注工具:LabelMe、精灵数据标注工具。
66

7-
用户在采集完用于训练、评估和预测的图片之后,需使用数据标注工具[LabelMe](https://github.com/wkentaro/labelme)完成数据标注。LabelMe支持在Windows/macOS/Linux三个系统上使用,且三个系统下的标注格式是一样。具体的安装流程请参见[官方安装指南](https://github.com/wkentaro/labelme)
7+
标注教程如下:
8+
- [LabelMe标注教程](labelme2seg.md)
9+
- [精灵数据标注工具教程](jingling2seg.md)
810

9-
## 2 LabelMe的使用
11+
最后用我们提供的数据转换脚本将上述标注工具产出的数据格式转换为模型训练时所需的数据格式。
1012

11-
打开终端输入`labelme`会出现LableMe的交互界面,可以先预览`LabelMe`给出的已标注好的图片,再开始标注自定义数据集。
12-
13-
<div align="center">
14-
<img src="../imgs/annotation/image-1.png" width="600px"/>
15-
<p>图1 LableMe交互界面的示意图</p>
16-
</div>
17-
18-
* 预览已标注图片
19-
20-
获取`LabelMe`的源码:
21-
```
22-
git clone https://github.com/wkentaro/labelme
23-
```
24-
终端输入`labelme`会出现LableMe的交互界面,点击`OpenDir`打开`<path/to/labelme>/examples/semantic_segmentation/data_annotated`,其中`<path/to/labelme>`为克隆下来的`labelme`的路径,打开后示意的是语义分割的真值标注。
25-
26-
<div align="center">
27-
<img src="../imgs/annotation/image-2.png" width="600px"/>
28-
<p>图2 已标注图片的示意图</p>
29-
</div>
30-
31-
* 开始标注
32-
33-
请按照下述步骤标注数据集:
34-
35-
​ (1) 点击`OpenDir`打开待标注图片所在目录,点击`Create Polygons`,沿着目标的边缘画多边形,完成后输入目标的类别。在标注过程中,如果某个点画错了,可以按撤销快捷键可撤销该点。Mac下的撤销快捷键为`command+Z`
36-
37-
<div align="center">
38-
<img src="../imgs/annotation/image-3.png" width="600px"/>
39-
<p>图3 标注单个目标的示意图</p>
40-
</div>
41-
42-
​ (2) 右击选择`Edit Polygons`可以整体移动多边形的位置,也可以移动某个点的位置;右击选择`Edit Label`可以修改每个目标的类别。请根据自己的需要执行这一步骤,若不需要修改,可跳过。
43-
44-
<div align="center">
45-
<img src="../imgs/annotation/image-4-1.png" width="00px" />
46-
<img src="../imgs/annotation/image-4-2.png" width="600px"/>
47-
<p>图4 修改标注的示意图</p>
48-
</div>
49-
50-
​ (3) 图片中所有目标的标注都完成后,点击`Save`保存json文件,**请将json文件和图片放在同一个文件夹里**,点击`Next Image`标注下一张图片。
51-
52-
LableMe产出的真值文件可参考我们给出的文件夹`data_annotated`
53-
54-
<div align="center">
55-
<img src="../imgs/annotation/image-5.png" width="600px"/>
56-
<p>图5 LableMe产出的真值文件的示意图</p>
57-
</div>
58-
59-
## 3 数据格式转换
60-
61-
* 我们用于完成语义分割的数据集目录结构如下:
62-
63-
```
64-
my_dataset # 根目录
65-
|-- JPEGImages # 数据集图片
66-
|-- SegmentationClassPNG # 数据集真值
67-
| |-- xxx.png # 像素级别的真值信息
68-
| |...
69-
|-- class_names.txt # 数据集的类别名称
70-
71-
```
72-
73-
<div align="center">
74-
<img src="../imgs/annotation/image-6.png" width="600px"/>
75-
<p>图6 训练所需的数据集目录的结构示意图</p>
76-
</div>
77-
78-
* 运行转换脚本需要依赖labelme和pillow,如未安装,请先安装。Labelme的具体安装流程请参见[官方安装指南](https://github.com/wkentaro/labelme)。Pillow的安装:
79-
80-
```shell
81-
pip install pillow
82-
```
83-
84-
* 运行以下代码,将标注后的数据转换成满足以上格式的数据集:
85-
86-
```
87-
python labelme2seg.py <path/to/label_json_file> <path/to/output_dataset>
88-
```
89-
90-
其中,`<path/to/label_json_files>`为图片以及LabelMe产出的json文件所在文件夹的目录,`<path/to/output_dataset>`为转换后的数据集所在文件夹的目录。**需注意的是:`<path/to/output_dataset>`不用预先创建,脚本运行时会自动创建,否则会报错。**
91-
92-
转换得到的数据集可参考我们给出的文件夹`my_dataset`。其中,文件`class_names.txt`是数据集中所有标注类别的名称,包含背景类;文件夹`JPEGImages`保存的是数据集的图片;文件夹`SegmentationClassPNG`保存的是各图片的像素级别的真值信息,背景类`_background_`对应为0,其它目标类别从1开始递增,至多为255。
93-
94-
<div align="center">
95-
<img src="../imgs/annotation/image-7.png" width="600px"/>
96-
<p>图7 训练所需的数据集各目录的内容示意图</p>
97-
</div>
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"path":"/Users/chulutao/dataset/humanseg/aa63d7e6db0d03137883772c246c6761fc201059.jpg","outputs":{"object":[{"name":"person","polygon":{"x1":321.99,"y1":63,"x2":293,"y2":98.00999999999999,"x3":245.01,"y3":141.01,"x4":221,"y4":194,"x5":231.99,"y5":237,"x6":231.99,"y6":348.01,"x7":191,"y7":429,"x8":197,"y8":465.01,"x9":193,"y9":586,"x10":151,"y10":618.01,"x11":124,"y11":622,"x12":100,"y12":703,"x13":121.99,"y13":744,"x14":141.99,"y14":724,"x15":163,"y15":658.01,"x16":238.01,"y16":646,"x17":259,"y17":627,"x18":313,"y18":618.01,"x19":416,"y19":639,"x20":464,"y20":606,"x21":454,"y21":555.01,"x22":404,"y22":508.01,"x23":430,"y23":489,"x24":407,"y24":464,"x25":397,"y25":365.01,"x26":407,"y26":290,"x27":361.99,"y27":252,"x28":376,"y28":215.01,"x29":391.99,"y29":189,"x30":388.01,"y30":135.01,"x31":340,"y31":120,"x32":313,"y32":161.01,"x33":307,"y33":188.01,"x34":311,"y34":207,"x35":277,"y35":186,"x36":293,"y36":137,"x37":308.01,"y37":117,"x38":361,"y38":93}}]},"time_labeled":1568101256852,"labeled":true,"size":{"width":706,"height":1000,"depth":3}}

docs/annotation/jingling2seg.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 精灵标注工具教程
2+
3+
## 1 精灵标注工具的安装
4+
5+
用户在采集完用于训练、评估和预测的图片之后,可使用[精灵数据标注工具](http://www.jinglingbiaozhu.com/)完成数据标注。精灵标注支持在Windows/macOS/Linux三个系统上使用,Mac的话可以到MacStore中搜索colabeler下载即可。
6+
7+
## 2 精灵标注工具的使用
8+
9+
开始标注自定义数据集之前,可以先预览精灵标注官网的[文字教程](http://www.jinglingbiaozhu.com/?type=tutorial&cat_id=4)[视频教程](http://www.jinglingbiaozhu.com/?type=tutorial&cat_id=5)
10+
11+
12+
* 开始标注
13+
14+
打开精灵标注工具。
15+
16+
<div align="center">
17+
<img src="../imgs/annotation/jingling-1.png" width="800px"/>
18+
<p>图1 精灵标注交互界面的示意图</p>
19+
</div>
20+
21+
请按照下述步骤标注数据集:
22+
23+
(1) 点击`新建`,然后选择`位置标注`,选择`图片文件夹`,修改填写所需的`分类值`(注意:以英文逗号隔开),点击`创建`按钮,软件会自动加载文件夹下的图片(png,jpg,gif)并创建一个项目。
24+
25+
位置标注支持三种类型:矩形,多边形和曲线。选择简单好用的`多边形框`,沿着目标的边缘画多边形,完成后在右侧输入目标的类别。
26+
27+
**注意:切记单张图片标注完成后进行保存,点击下方中央的勾按钮或者使用快捷键ctrl+s**
28+
29+
然后可以点击左边的前一个后一个或者直接使用键盘的向左按钮和向右按钮来切换图片。
30+
31+
<div align="center">
32+
<img src="../imgs/annotation/jingling-2.png" width="800px"/>
33+
<p>图2 标注单个目标的示意图</p>
34+
</div>
35+
36+
(2) 单击目标框,鼠标拖动可以整体移动多边形的位置;点击左侧的`删除选框`可以删除画错的目标框;点击右侧的`标注信息`可修改目标类别。请根据自己的需要执行这一步骤,若不需要修改,可跳过。
37+
38+
<div align="center">
39+
<img src="../imgs/annotation/jingling-3.png" width="800px"/>
40+
<p>图3 修改标注的示意图</p>
41+
</div>
42+
43+
(3) 当所有图片的标注都完成后,点击左侧的`导出`,输出方式选择`JSON`,指定`保存位置`,点击`确定导出`保存所有图片的标注文件。
44+
45+
**注意:导出的标注文件位于`保存位置`下的`outputs`目录。**
46+
47+
精灵标注产出的真值文件可参考我们给出的文件夹`data_annotated`
48+
49+
<div align="center">
50+
<img src="../imgs/annotation/jingling-4.png" width="300px"/>
51+
<p>图4 精灵标注产出的真值文件的示意图</p>
52+
</div>
53+
54+
## 3 数据格式转换
55+
56+
* 我们用于完成语义分割的数据集目录结构如下:
57+
58+
```
59+
my_dataset # 根目录
60+
|-- JPEGImages # 数据集图片
61+
|-- SegmentationClassPNG # 数据集真值
62+
| |-- xxx.png # 像素级别的真值信息
63+
| |...
64+
|-- class_names.txt # 数据集的类别名称
65+
66+
```
67+
68+
<div align="center">
69+
<img src="../imgs/annotation/image-6.png" width="600px"/>
70+
<p>图5 训练所需的数据集目录的结构示意图</p>
71+
</div>
72+
73+
* 运行转换脚本需要依赖labelme和pillow,如未安装,请先安装。Labelme的具体安装流程请参见[官方安装指南](https://github.com/wkentaro/labelme)。Pillow的安装:
74+
75+
```shell
76+
pip install pillow
77+
```
78+
79+
* 运行以下代码,将标注后的数据转换成满足以上格式的数据集:
80+
81+
```
82+
python jingling2seg.py <path/to/label_json_file> <path/to/output_dataset>
83+
```
84+
85+
其中,`<path/to/label_json_files>`为精灵标注产出的json文件所在文件夹的目录,一般为精灵工具使用(3)中`保存位置`下的`outputs`目录。`<path/to/output_dataset>`为转换后的数据集所在文件夹的目录。
86+
87+
**注意:`<path/to/output_dataset>`不用预先创建,脚本运行时会自动创建,否则会报错。**
88+
89+
转换得到的数据集可参考我们给出的文件夹`my_dataset`。其中,文件`class_names.txt`是数据集中所有标注类别的名称,包含背景类;文件夹`JPEGImages`保存的是数据集的图片;文件夹`SegmentationClassPNG`保存的是各图片的像素级别的真值信息,背景类`_background_`对应为0,其它目标类别从1开始递增,至多为255。
90+
91+
<div align="center">
92+
<img src="../imgs/annotation/jingling-5.png" width="600px"/>
93+
<p>图6 训练所需的数据集各目录的内容示意图</p>
94+
</div>

docs/annotation/jingling2seg.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import print_function
4+
5+
import argparse
6+
import glob
7+
import json
8+
import os
9+
import os.path as osp
10+
import sys
11+
12+
import numpy as np
13+
import PIL.Image
14+
15+
import labelme
16+
17+
18+
def main():
19+
parser = argparse.ArgumentParser(
20+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
21+
)
22+
parser.add_argument('label_dir', help='input annotated directory')
23+
parser.add_argument('output_dir', help='output dataset directory')
24+
args = parser.parse_args()
25+
26+
if osp.exists(args.output_dir):
27+
print('Output directory already exists:', args.output_dir)
28+
sys.exit(1)
29+
30+
os.makedirs(args.output_dir)
31+
os.makedirs(osp.join(args.output_dir, 'JPEGImages'))
32+
os.makedirs(osp.join(args.output_dir, 'SegmentationClassPNG'))
33+
print('Creating dataset:', args.output_dir)
34+
35+
# get the all class names for the given dataset
36+
class_names = ['_background_']
37+
for label_file in glob.glob(osp.join(args.label_dir, '*.json')):
38+
with open(label_file) as f:
39+
data = json.load(f)
40+
if data['outputs']:
41+
for output in data['outputs']['object']:
42+
name = output['name']
43+
cls_name = name
44+
if not cls_name in class_names:
45+
class_names.append(cls_name)
46+
47+
class_name_to_id = {}
48+
for i, class_name in enumerate(class_names):
49+
class_id = i # starts with 0
50+
class_name_to_id[class_name] = class_id
51+
if class_id == 0:
52+
assert class_name == '_background_'
53+
class_names = tuple(class_names)
54+
print('class_names:', class_names)
55+
56+
out_class_names_file = osp.join(args.output_dir, 'class_names.txt')
57+
with open(out_class_names_file, 'w') as f:
58+
f.writelines('\n'.join(class_names))
59+
print('Saved class_names:', out_class_names_file)
60+
61+
for label_file in glob.glob(osp.join(args.label_dir, '*.json')):
62+
print('Generating dataset from:', label_file)
63+
with open(label_file) as f:
64+
base = osp.splitext(osp.basename(label_file))[0]
65+
out_img_file = osp.join(
66+
args.output_dir, 'JPEGImages', base + '.jpg')
67+
out_png_file = osp.join(
68+
args.output_dir, 'SegmentationClassPNG', base + '.png')
69+
70+
data = json.load(f)
71+
72+
data_shapes = []
73+
if data['outputs']:
74+
for output in data['outputs']['object']:
75+
if 'polygon' in output.keys():
76+
polygon = output['polygon']
77+
name = output['name']
78+
79+
# convert jingling format to labelme format
80+
points = []
81+
for i in range(1, int(len(polygon) / 2) + 1):
82+
points.append([polygon['x' + str(i)], polygon['y' + str(i)]])
83+
shape = {'label': name, 'points': points, 'shape_type': 'polygon'}
84+
data_shapes.append(shape)
85+
86+
img_file = osp.join(osp.dirname(label_file), data['path'])
87+
img = np.asarray(PIL.Image.open(img_file))
88+
PIL.Image.fromarray(img).save(out_img_file)
89+
90+
lbl = labelme.utils.shapes_to_label(
91+
img_shape=img.shape,
92+
shapes=data_shapes,
93+
label_name_to_value=class_name_to_id,
94+
)
95+
96+
if osp.splitext(out_png_file)[1] != '.png':
97+
out_png_file += '.png'
98+
# Assume label ranses [0, 255] for uint8,
99+
if lbl.min() >= 0 and lbl.max() <= 255:
100+
lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='L')
101+
lbl_pil.save(out_png_file)
102+
else:
103+
raise ValueError(
104+
'[%s] Cannot save the pixel-wise class label as PNG. '
105+
'Please consider using the .npy format.' % out_png_file
106+
)
107+
108+
109+
if __name__ == '__main__':
110+
main()

0 commit comments

Comments
 (0)