最近,需要制作自己的类似Camvid语义分割数据集的样式,找了很多文章,自己再总结一下。首先非常感谢https://blog.csdn.net/Winters____/article/details/105659632?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control,通过这个终于转换成功。
1、使用labelMe标注自己的数据集。标注完毕后,会生成json文件。
2、将json文件和原图片放入一个文件夹。
3、下载https://github.com/wkentaro/labelme/tree/master/examples/semantic_segmentation,进入到examples/semantic_segmentation文件夹下执行
python ./labelme2voc.py C:/Users/lenovo/Desktop/val C:/Users/lenovo/Desktop/save --labels labels.txt
其中C:/Users/lenovo/Desktop/val为步骤2中存放的路径, C:/Users/lenovo/Desktop/save为生成的标注类别的彩色图片的路径,labels.txt存放自己数据及中的类别(具体仿照原labels.txt修改)。执行完毕后如下图所示:
注意:出现AttributeError: module 'labelme' has no attribute 'LabelFile'错误,通过pip install -U labelme解决。
4、
将标注类别的彩色图片转换为可用于训练的灰度图
"""Removes the color map from segmentation annotations.
Removes the color map from the ground truth segmentation annotations and save
the results to output_dir.
"""
#源程序链接:https://github.com/tensorflow/models/tree/master/research/deeplab/datasets
#该程序用于将voc格式的索引图片转换为可用于语义分割的灰度图片,因为TensorFlow版本的缘故,在原程序上做了少许改动
#tf.__version__==1.12
import glob
import os.path
import numpy as np
from PIL import Image
import tensorflow as tf
#FLAGS = tf.compat.v1.flags.FLAGS
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_string('original_gt_folder',#读取voc格式的png图片路径
'F:\Dataset_segmentaion\data_dataset_voc\SegmentationClassPNG',#default
'Original ground truth annotations.')#help
tf.flags.DEFINE_string('segmentation_format', 'png', 'Segmentation format.')
tf.flags.DEFINE_string('output_dir',#保存路径
'F:\Dataset_segmentaion\data_dataset_voc\labels',
'folder to save modified ground truth annotations.')
def _remove_colormap(filename):
"""Removes the color map from the annotation.
Args:
filename: Ground truth annotation filename.
Returns:
Annotation without color map.
"""
return np.array(Image.open(filename))
def _save_annotation(annotation, filename):
"""Saves the annotation as png file.
Args:
annotation: Segmentation annotation.
filename: Output filename.
"""
pil_image = Image.fromarray(annotation.astype(dtype=np.uint8))
'''
with tf.io.gfile.GFile(filename, mode='w') as f:
#with open(filename, mode='w') as f:
print(f)
pil_image.save(f, 'PNG')
'''
pil_image.save(filename)
def main(unused_argv):
# Create the output directory if not exists.
if not os.path.exists(FLAGS.output_dir):
os.makedirs(FLAGS.output_dir)
#if not tf.io.gfile.isdir(FLAGS.output_dir):
#tf.io.gfile.makedirs(FLAGS.output_dir)
annotations = glob.glob(os.path.join(FLAGS.original_gt_folder,
'*.' + FLAGS.segmentation_format))
for annotation in annotations:
raw_annotation = _remove_colormap(annotation)
filename = os.path.basename(annotation)[:-4]
_save_annotation(raw_annotation,
os.path.join(
FLAGS.output_dir,
filename + '.' + FLAGS.segmentation_format))
if __name__ == '__main__':
#tf.compat.v1.app.run()
tf.app.run()
修改第18行和第22行为自己的路径。18行为步骤3中SegmentationClassPNG的路径,22行为灰度图存放的路径。
到这里就转换完毕了~
5、通过下述代码,可以验证某一图片中存放的类别有哪些~
import numpy as np
from PIL import Image
img = Image.open('C:\\Users\\lenovo\\Desktop\\save\\labels\\22367.jpg')
img = np.array(img)
print(np.unique(img))