提交 eedc2ad3 编写于 作者: K Kentaro Wada

Add examples/instance_segmentation

上级 bc0c5b59
# Instance Segmentation Example
## Annotation
```bash
labelme data_annotated --labels labels.txt --nodata
```
{
"imagePath": "2011_000003.jpg",
"shapes": [
{
"line_color": null,
"points": [
[
251.8142292490119,
107.33596837944665
],
[
230.8142292490119,
119.33596837944665
],
[
222.8142292490119,
135.33596837944665
],
[
224.8142292490119,
148.33596837944665
],
[
218.8142292490119,
161.33596837944665
],
[
203.8142292490119,
168.33596837944665
],
[
193.8142292490119,
200.33596837944665
],
[
195.8142292490119,
222.33596837944665
],
[
200.8142292490119,
227.33596837944665
],
[
192.8142292490119,
234.33596837944665
],
[
198.8142292490119,
264.3359683794467
],
[
214.8142292490119,
295.3359683794467
],
[
215.8142292490119,
320.3359683794467
],
[
222.8142292490119,
327.3359683794467
],
[
236.8142292490119,
326.3359683794467
],
[
241.8142292490119,
323.3359683794467
],
[
236.8142292490119,
298.3359683794467
],
[
239.8142292490119,
287.3359683794467
],
[
235.8142292490119,
268.3359683794467
],
[
258.81422924901193,
258.3359683794467
],
[
265.81422924901193,
264.3359683794467
],
[
257.81422924901193,
273.3359683794467
],
[
260.81422924901193,
282.3359683794467
],
[
285.81422924901193,
288.3359683794467
],
[
298.81422924901193,
278.3359683794467
],
[
289.81422924901193,
270.3359683794467
],
[
282.81422924901193,
270.3359683794467
],
[
284.81422924901193,
264.3359683794467
],
[
293.81422924901193,
261.3359683794467
],
[
309.81422924901193,
236.33596837944665
],
[
314.81422924901193,
217.33596837944665
],
[
310.81422924901193,
208.33596837944665
],
[
313.81422924901193,
202.33596837944665
],
[
309.81422924901193,
185.33596837944665
],
[
292.81422924901193,
173.33596837944665
],
[
270.81422924901193,
159.33596837944665
],
[
262.81422924901193,
154.33596837944665
],
[
265.81422924901193,
142.33596837944665
],
[
274.81422924901193,
137.33596837944665
],
[
279.81422924901193,
130.33596837944665
],
[
271.81422924901193,
121.33596837944665
]
],
"fill_color": null,
"label": "person-1"
},
{
"line_color": null,
"points": [
[
482.81422924901193,
85.33596837944665
],
[
468.81422924901193,
90.33596837944665
],
[
460.81422924901193,
110.33596837944665
],
[
460.81422924901193,
127.33596837944665
],
[
444.81422924901193,
137.33596837944665
],
[
419.81422924901193,
153.33596837944665
],
[
410.81422924901193,
163.33596837944665
],
[
403.81422924901193,
168.33596837944665
],
[
394.81422924901193,
170.33596837944665
],
[
386.81422924901193,
168.33596837944665
],
[
386.81422924901193,
184.33596837944665
],
[
392.81422924901193,
182.33596837944665
],
[
410.81422924901193,
187.33596837944665
],
[
414.81422924901193,
192.33596837944665
],
[
437.81422924901193,
189.33596837944665
],
[
434.81422924901193,
204.33596837944665
],
[
390.81422924901193,
195.33596837944665
],
[
386.81422924901193,
195.33596837944665
],
[
387.81422924901193,
208.33596837944665
],
[
381.81422924901193,
212.33596837944665
],
[
372.81422924901193,
212.33596837944665
],
[
372.81422924901193,
216.33596837944665
],
[
400.81422924901193,
270.3359683794467
],
[
389.81422924901193,
272.3359683794467
],
[
389.81422924901193,
274.3359683794467
],
[
403.81422924901193,
282.3359683794467
],
[
444.81422924901193,
283.3359683794467
],
[
443.81422924901193,
259.3359683794467
],
[
426.81422924901193,
244.33596837944665
],
[
462.81422924901193,
256.3359683794467
],
[
474.81422924901193,
270.3359683794467
],
[
477.81422924901193,
280.3359683794467
],
[
473.81422924901193,
289.3359683794467
],
[
471.81422924901193,
296.3359683794467
],
[
472.81422924901193,
317.3359683794467
],
[
480.81422924901193,
332.3359683794467
],
[
494.81422924901193,
335.3359683794467
],
[
498.81422924901193,
329.3359683794467
],
[
494.81422924901193,
308.3359683794467
],
[
499.81422924901193,
297.3359683794467
],
[
499.81422924901193,
90.33596837944665
]
],
"fill_color": null,
"label": "person-2"
},
{
"line_color": null,
"points": [
[
374.81422924901193,
159.33596837944665
],
[
369.81422924901193,
170.33596837944665
],
[
369.81422924901193,
210.33596837944665
],
[
375.81422924901193,
212.33596837944665
],
[
387.81422924901193,
209.33596837944665
],
[
385.81422924901193,
185.33596837944665
],
[
385.81422924901193,
168.33596837944665
],
[
385.81422924901193,
165.33596837944665
],
[
382.81422924901193,
159.33596837944665
]
],
"fill_color": null,
"label": "bottle"
},
{
"line_color": null,
"points": [
[
370.81422924901193,
170.33596837944665
],
[
366.81422924901193,
173.33596837944665
],
[
365.81422924901193,
182.33596837944665
],
[
368.81422924901193,
185.33596837944665
]
],
"fill_color": null,
"label": "person-2"
},
{
"line_color": null,
"points": [
[
338.81422924901193,
266.3359683794467
],
[
313.81422924901193,
269.3359683794467
],
[
297.81422924901193,
277.3359683794467
],
[
282.81422924901193,
288.3359683794467
],
[
273.81422924901193,
302.3359683794467
],
[
272.81422924901193,
320.3359683794467
],
[
279.81422924901193,
337.3359683794467
],
[
428.81422924901193,
337.3359683794467
],
[
432.81422924901193,
316.3359683794467
],
[
423.81422924901193,
296.3359683794467
],
[
403.81422924901193,
283.3359683794467
],
[
370.81422924901193,
270.3359683794467
]
],
"fill_color": null,
"label": "__ignore__"
}
],
"imageData": null,
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
]
}
\ No newline at end of file
{
"imagePath": "2011_000006.jpg",
"shapes": [
{
"line_color": null,
"points": [
[
204.936170212766,
108.56382978723406
],
[
183.936170212766,
141.56382978723406
],
[
166.936170212766,
150.56382978723406
],
[
108.93617021276599,
203.56382978723406
],
[
92.93617021276599,
228.56382978723406
],
[
95.93617021276599,
244.56382978723406
],
[
105.93617021276599,
244.56382978723406
],
[
116.93617021276599,
223.56382978723406
],
[
163.936170212766,
187.56382978723406
],
[
147.936170212766,
212.56382978723406
],
[
117.93617021276599,
222.56382978723406
],
[
108.93617021276599,
243.56382978723406
],
[
100.93617021276599,
325.56382978723406
],
[
135.936170212766,
329.56382978723406
],
[
148.936170212766,
319.56382978723406
],
[
150.936170212766,
295.56382978723406
],
[
169.936170212766,
272.56382978723406
],
[
171.936170212766,
249.56382978723406
],
[
178.936170212766,
246.56382978723406
],
[
186.936170212766,
225.56382978723406
],
[
214.936170212766,
219.56382978723406
],
[
242.936170212766,
157.56382978723406
],
[
228.936170212766,
146.56382978723406
],
[
228.936170212766,
125.56382978723406
],
[
216.936170212766,
112.56382978723406
]
],
"fill_color": null,
"label": "person-1"
},
{
"line_color": null,
"points": [
[
271.936170212766,
109.56382978723406
],
[
249.936170212766,
110.56382978723406
],
[
244.936170212766,
150.56382978723406
],
[
215.936170212766,
219.56382978723406
],
[
208.936170212766,
245.56382978723406
],
[
214.936170212766,
220.56382978723406
],
[
188.936170212766,
227.56382978723406
],
[
170.936170212766,
246.56382978723406
],
[
170.936170212766,
275.56382978723406
],
[
221.936170212766,
278.56382978723406
],
[
233.936170212766,
259.56382978723406
],
[
246.936170212766,
253.56382978723406
],
[
245.936170212766,
256.56382978723406
],
[
242.936170212766,
251.56382978723406
],
[
262.936170212766,
256.56382978723406
],
[
304.936170212766,
226.56382978723406
],
[
297.936170212766,
199.56382978723406
],
[
308.936170212766,
164.56382978723406
],
[
296.936170212766,
148.56382978723406
]
],
"fill_color": null,
"label": "person-2"
},
{
"line_color": null,
"points": [
[
308.936170212766,
115.56382978723406
],
[
298.936170212766,
145.56382978723406
],
[
309.936170212766,
166.56382978723406
],
[
297.936170212766,
200.56382978723406
],
[
305.936170212766,
228.56382978723406
],
[
262.936170212766,
258.56382978723406
],
[
252.936170212766,
284.56382978723406
],
[
272.936170212766,
291.56382978723406
],
[
281.936170212766,
250.56382978723406
],
[
326.936170212766,
235.56382978723406
],
[
351.936170212766,
239.56382978723406
],
[
365.936170212766,
223.56382978723406
],
[
371.936170212766,
187.56382978723406
],
[
353.936170212766,
168.56382978723406
],
[
344.936170212766,
143.56382978723406
],
[
336.936170212766,
115.56382978723406
]
],
"fill_color": null,
"label": "person-3"
},
{
"line_color": null,
"points": [
[
308.936170212766,
242.56382978723406
],
[
281.936170212766,
251.56382978723406
],
[
270.936170212766,
287.56382978723406
],
[
174.936170212766,
275.56382978723406
],
[
148.936170212766,
296.56382978723406
],
[
150.936170212766,
319.56382978723406
],
[
159.936170212766,
328.56382978723406
],
[
164.77327127659578,
375.0
],
[
485.936170212766,
373.56382978723406
],
[
497.936170212766,
336.56382978723406
],
[
497.936170212766,
202.56382978723406
],
[
453.936170212766,
193.56382978723406
],
[
434.936170212766,
212.56382978723406
],
[
367.936170212766,
224.56382978723406
],
[
350.936170212766,
241.56382978723406
]
],
"fill_color": null,
"label": "chair"
},
{
"line_color": null,
"points": [
[
425.936170212766,
82.56382978723406
],
[
404.936170212766,
109.56382978723406
],
[
400.936170212766,
114.56382978723406
],
[
437.936170212766,
114.56382978723406
],
[
448.936170212766,
102.56382978723406
],
[
446.936170212766,
91.56382978723406
]
],
"fill_color": null,
"label": "person-4"
},
{
"line_color": null,
"points": [
[
457.936170212766,
85.56382978723406
],
[
439.936170212766,
117.56382978723406
],
[
477.936170212766,
117.56382978723406
],
[
474.936170212766,
87.56382978723406
]
],
"fill_color": null,
"label": "__ignore__"
},
{
"line_color": null,
"points": [
[
183.936170212766,
140.56382978723406
],
[
125.93617021276599,
140.56382978723406
],
[
110.93617021276599,
187.56382978723406
],
[
22.936170212765987,
199.56382978723406
],
[
18.936170212765987,
218.56382978723406
],
[
22.936170212765987,
234.56382978723406
],
[
93.93617021276599,
239.56382978723406
],
[
91.93617021276599,
229.56382978723406
],
[
110.93617021276599,
203.56382978723406
]
],
"fill_color": null,
"label": "sofa"
},
{
"line_color": null,
"points": [
[
103.93617021276599,
290.56382978723406
],
[
58.93617021276599,
303.56382978723406
],
[
97.93617021276599,
311.56382978723406
]
],
"fill_color": null,
"label": "sofa"
},
{
"line_color": null,
"points": [
[
348.936170212766,
146.56382978723406
],
[
472.936170212766,
149.56382978723406
],
[
477.936170212766,
162.56382978723406
],
[
471.936170212766,
196.56382978723406
],
[
453.936170212766,
192.56382978723406
],
[
434.936170212766,
213.56382978723406
],
[
368.936170212766,
226.56382978723406
],
[
375.936170212766,
187.56382978723406
],
[
353.936170212766,
164.56382978723406
]
],
"fill_color": null,
"label": "sofa"
},
{
"line_color": null,
"points": [
[
246.936170212766,
252.56382978723406
],
[
219.936170212766,
277.56382978723406
],
[
254.936170212766,
287.56382978723406
],
[
261.936170212766,
256.56382978723406
]
],
"fill_color": null,
"label": "sofa"
}
],
"imageData": null,
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
]
}
\ No newline at end of file
{
"imageData": null,
"shapes": [
{
"fill_color": null,
"line_color": null,
"label": "bus-1",
"points": [
[
260.936170212766,
22.563829787234056
],
[
193.936170212766,
19.563829787234056
],
[
124.93617021276599,
39.563829787234056
],
[
89.93617021276599,
101.56382978723406
],
[
81.93617021276599,
150.56382978723406
],
[
108.93617021276599,
145.56382978723406
],
[
88.93617021276599,
244.56382978723406
],
[
89.93617021276599,
322.56382978723406
],
[
116.93617021276599,
367.56382978723406
],
[
158.936170212766,
368.56382978723406
],
[
165.936170212766,
337.56382978723406
],
[
347.936170212766,
335.56382978723406
],
[
349.936170212766,
369.56382978723406
],
[
391.936170212766,
373.56382978723406
],
[
403.936170212766,
335.56382978723406
],
[
425.936170212766,
332.56382978723406
],
[
421.936170212766,
281.56382978723406
],
[
428.936170212766,
252.56382978723406
],
[
428.936170212766,
236.56382978723406
],
[
409.936170212766,
220.56382978723406
],
[
409.936170212766,
150.56382978723406
],
[
430.936170212766,
143.56382978723406
],
[
433.936170212766,
112.56382978723406
],
[
431.936170212766,
96.56382978723406
],
[
408.936170212766,
90.56382978723406
],
[
395.936170212766,
50.563829787234056
],
[
338.936170212766,
25.563829787234056
]
]
},
{
"fill_color": null,
"line_color": null,
"label": "bus-2",
"points": [
[
88.93617021276599,
115.56382978723406
],
[
0.9361702127659877,
96.56382978723406
],
[
0.0,
251.968085106388
],
[
0.9361702127659877,
265.56382978723406
],
[
27.936170212765987,
265.56382978723406
],
[
29.936170212765987,
283.56382978723406
],
[
63.93617021276599,
281.56382978723406
],
[
89.93617021276599,
252.56382978723406
],
[
100.93617021276599,
183.56382978723406
],
[
108.93617021276599,
145.56382978723406
],
[
81.93617021276599,
151.56382978723406
]
]
},
{
"fill_color": null,
"line_color": null,
"label": "car",
"points": [
[
413.936170212766,
168.56382978723406
],
[
497.936170212766,
168.56382978723406
],
[
497.936170212766,
256.56382978723406
],
[
431.936170212766,
258.56382978723406
],
[
430.936170212766,
236.56382978723406
],
[
408.936170212766,
218.56382978723406
]
]
}
],
"fillColor": [
255,
0,
0,
128
],
"lineColor": [
0,
255,
0,
128
],
"imagePath": "2011_000025.jpg"
}
\ No newline at end of file
_background_
aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
potted plant
sheep
sofa
train
tv/monitor
\ No newline at end of file
#!/usr/bin/env python
from __future__ import print_function
import argparse
import glob
import io
import json
import os
import os.path as osp
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
import PIL.ImagePalette
import labelme
from labelme.utils import label2rgb
from labelme.utils import label_colormap
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('labels_file')
parser.add_argument('in_dir')
parser.add_argument('out_dir')
args = parser.parse_args()
if osp.exists(args.out_dir):
print('Output directory already exists:', args.out_dir)
quit(1)
os.makedirs(args.out_dir)
os.makedirs(osp.join(args.out_dir, 'JPEGImages'))
os.makedirs(osp.join(args.out_dir, 'SegmentationClass'))
os.makedirs(osp.join(args.out_dir, 'SegmentationClassVisualization'))
os.makedirs(osp.join(args.out_dir, 'SegmentationObject'))
os.makedirs(osp.join(args.out_dir, 'SegmentationObjectVisualization'))
print('Creating dataset:', args.out_dir)
class_names = []
class_name_to_id = {}
for i, line in enumerate(open(args.labels_file).readlines()):
class_id = i - 1 # starts with -1
class_name = line.strip()
class_name_to_id[class_name] = class_id
if class_id == -1:
assert class_name == '__ignore__'
continue
elif class_id == 0:
assert class_name == '_background_'
class_names.append(class_name)
class_names = tuple(class_names)
print('class_names:', class_names)
out_class_names_file = osp.join(args.out_dir, 'class_names.txt')
with open(out_class_names_file, 'w') as f:
f.writelines('\n'.join(class_names))
print('Saved class_names:', out_class_names_file)
colormap = labelme.utils.label_colormap(255)
for label_file in glob.glob(osp.join(args.in_dir, '*.json')):
print('Generating dataset from:', label_file)
with open(label_file) as f:
base = osp.splitext(osp.basename(label_file))[0]
out_img_file = osp.join(
args.out_dir, 'JPEGImages', base + '.jpg')
out_cls_file = osp.join(
args.out_dir, 'SegmentationClass', base + '.png')
out_clsv_file = osp.join(
args.out_dir, 'SegmentationClassVisualization', base + '.jpg')
out_ins_file = osp.join(
args.out_dir, 'SegmentationObject', base + '.png')
out_insv_file = osp.join(
args.out_dir, 'SegmentationObjectVisualization', base + '.jpg')
data = json.load(f)
img_file = osp.join(osp.dirname(label_file), data['imagePath'])
img = np.asarray(PIL.Image.open(img_file))
PIL.Image.fromarray(img).save(out_img_file)
cls, ins = labelme.utils.shapes_to_label(
img_shape=img.shape,
shapes=data['shapes'],
label_name_to_value=class_name_to_id,
type='instance',
)
PIL.Image.fromarray(cls).save(out_cls_file)
label_names = ['%d: %s' % (cls_id, cls_name)
for cls_id, cls_name in enumerate(class_names)]
clsv = labelme.utils.draw_label(
cls, img, label_names, colormap=colormap)
PIL.Image.fromarray(clsv).save(out_clsv_file)
PIL.Image.fromarray(ins).save(out_ins_file)
instance_ids = np.unique(ins)
instance_names = [str(i) for i in range(max(instance_ids) + 1)]
insv = labelme.utils.draw_label(
ins, img, instance_names)
PIL.Image.fromarray(insv).save(out_insv_file)
if __name__ == '__main__':
main()
__ignore__
_background_
aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
potted plant
sheep
sofa
train
tv/monitor
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册