未验证 提交 7bcd8b1d 编写于 作者: L LutaoChu 提交者: GitHub

fix label tool bugs (#266)

* modify label tool
上级 867d4a5b
...@@ -76,12 +76,6 @@ ...@@ -76,12 +76,6 @@
<p>图5 格式转换后的数据集目录的结构示意图</p> <p>图5 格式转换后的数据集目录的结构示意图</p>
</div> </div>
* 运行转换脚本需要依赖labelme和pillow,如未安装,请先安装。Labelme的具体安装流程请参见[官方安装指南](https://github.com/wkentaro/labelme)。Pillow的安装:
```shell
pip install pillow
```
* 运行以下代码,将标注后的数据转换成满足以上格式的数据集: * 运行以下代码,将标注后的数据转换成满足以上格式的数据集:
``` ```
......
...@@ -86,12 +86,6 @@ LableMe产出的真值文件可参考我们给出的文件夹[docs/annotation/la ...@@ -86,12 +86,6 @@ LableMe产出的真值文件可参考我们给出的文件夹[docs/annotation/la
<p>图7 格式转换后的数据集目录的结构示意图</p> <p>图7 格式转换后的数据集目录的结构示意图</p>
</div> </div>
* 运行转换脚本需要依赖labelme和pillow,如未安装,请先安装。Labelme的具体安装流程请参见[官方安装指南](https://github.com/wkentaro/labelme)。Pillow的安装:
```shell
pip install pillow
```
* 运行以下代码,将标注后的数据转换成满足以上格式的数据集: * 运行以下代码,将标注后的数据转换成满足以上格式的数据集:
``` ```
......
...@@ -20,12 +20,11 @@ import glob ...@@ -20,12 +20,11 @@ import glob
import json import json
import os import os
import os.path as osp import os.path as osp
import numpy as np import numpy as np
import PIL.Image import PIL.Image
import labelme
from gray2pseudo_color import get_color_map_list from gray2pseudo_color import get_color_map_list
from labelme2seg import shape2label
def parse_args(): def parse_args():
...@@ -102,10 +101,10 @@ def main(args): ...@@ -102,10 +101,10 @@ def main(args):
img_shape = (data_size['height'], data_size['width'], img_shape = (data_size['height'], data_size['width'],
data_size['depth']) data_size['depth'])
lbl = labelme.utils.shapes_to_label( lbl = shape2label(
img_shape=img_shape, img_size=img_shape,
shapes=data_shapes, shapes=data_shapes,
label_name_to_value=class_name_to_id, class_name_mapping=class_name_to_id,
) )
if osp.splitext(out_png_file)[1] != '.png': if osp.splitext(out_png_file)[1] != '.png':
......
...@@ -17,13 +17,14 @@ from __future__ import print_function ...@@ -17,13 +17,14 @@ from __future__ import print_function
import argparse import argparse
import glob import glob
import math
import json import json
import os import os
import os.path as osp import os.path as osp
import numpy as np import numpy as np
import PIL.Image import PIL.Image
import labelme import PIL.ImageDraw
import cv2
from gray2pseudo_color import get_color_map_list from gray2pseudo_color import get_color_map_list
...@@ -77,12 +78,12 @@ def main(args): ...@@ -77,12 +78,12 @@ def main(args):
data = json.load(f) data = json.load(f)
img_file = osp.join(osp.dirname(label_file), data['imagePath']) img_file = osp.join(osp.dirname(label_file), data['imagePath'])
img = np.asarray(PIL.Image.open(img_file)) img = np.asarray(cv2.imread(img_file))
lbl = labelme.utils.shapes_to_label( lbl = shape2label(
img_shape=img.shape, img_size=img.shape,
shapes=data['shapes'], shapes=data['shapes'],
label_name_to_value=class_name_to_id, class_name_mapping=class_name_to_id,
) )
if osp.splitext(out_png_file)[1] != '.png': if osp.splitext(out_png_file)[1] != '.png':
...@@ -98,6 +99,27 @@ def main(args): ...@@ -98,6 +99,27 @@ def main(args):
'Please consider using the .npy format.' % out_png_file) 'Please consider using the .npy format.' % out_png_file)
def shape2mask(img_size, points):
label_mask = PIL.Image.fromarray(np.zeros(img_size[:2], dtype=np.uint8))
image_draw = PIL.ImageDraw.Draw(label_mask)
points_list = [tuple(point) for point in points]
assert len(points_list) > 2, 'Polygon must have points more than 2'
image_draw.polygon(xy=points_list, outline=1, fill=1)
return np.array(label_mask, dtype=bool)
def shape2label(img_size, shapes, class_name_mapping):
label = np.zeros(img_size[:2], dtype=np.int32)
for shape in shapes:
points = shape['points']
class_name = shape['label']
shape_type = shape.get('shape_type', None)
class_id = class_name_mapping[class_name]
label_mask = shape2mask(img_size[:2], points)
label[label_mask] = class_id
return label
if __name__ == '__main__': if __name__ == '__main__':
args = parse_args() args = parse_args()
main(args) main(args)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册