Python图片重命名并复制并将label数据集改成单通道——deeplabv3+制作自己的训练数据集

主要是在准备deeplabv3+训练数据集,用自己的数据然后用labelme工具进行数据分割标注。得到<文件名>.json,利用labelme_json_to_dataset <文件名>.json 得到每张图片对应的<文件名>_json的文件夹,里面有该图片对应的文件,如下:
原始数据为:(为数据保密不放出来了)
0.jpg 1.jpg 2.jpg等等<文件名>.jpg类型图片
利用labelme工具得到文件<文件名>.json,利用labelme_json_to_dataset <文件名>.json 得到每张图片对应的<文件名>_json的文件夹:
Python图片重命名并复制并将label数据集改成单通道——deeplabv3+制作自己的训练数据集_第1张图片
每个图片对应的<文件名>_json的文件夹里的内容:
Python图片重命名并复制并将label数据集改成单通道——deeplabv3+制作自己的训练数据集_第2张图片

每个<文件名>_json的文件夹里的label.png表示为该图片<文件名>对应的标签,所以要将label.png重命名为<文件名>.png并将重命名过后的图片复制到相对应的路径下

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  9 10:47:36 2018

@author: yxh
"""
import os
import sys
import shutil
path = '/home/yxh/linux_v1.4.3/beifen/'

def reName(path):
    fileList = []
    
    files = os.listdir(path)
    i=0
    for f in files:
        file_name, file_extend = os.path.splitext(f)
        new_name = file_name.split('_')[0]
        dirpath = path + '/' +f
        if(os.path.isdir(dirpath)):
            dirfiles = os.listdir(dirpath)
            for d in dirfiles:
                if(d == 'label.png'):
                    #file_path = os.path.join(dirfiles, d)
                    src = os.path.join(os.path.abspath(dirpath), d)
                    dst = os.path.join(os.path.abspath(dirpath), new_name + '.png')
                    os.rename(src, dst)
                    file_path  = dst
                    new_path = '/home/yxh/models-deeplabv3/research/deeplab/datasets/Lane/Semantic_segmentation/' + new_name + '.png'
                    shutil.copy(file_path, new_path)


reName(path)

以上已经可以将完成重命名以及复制工作,但是这样的情况下train.py是可以正常运行的,但是在eval.py vis.py的时候会出现

InvalidArgumentError (see above for traceback): assertion failed: [`predictions` out of bound] [Condition x < y did not hold element-wise:] [x (mean_iou/confusion_matrix/control_dependency_1:0) = ] [0 0 0...] [y (mean_iou/ToInt64_2:0) = ] [2]
	 [[Node: mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_STRING, DT_INT64, DT_STRING, DT_INT64], summarize=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/Switch/_4737, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/data_0, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/data_1, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/data_2, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/Switch_1/_4739, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/data_4, mean_iou/confusion_matrix/assert_less_1/Assert/AssertGuard/Assert/Switch_2/_4741)]]

等情况,表明了并没有将label变成单通道的图片,以下则在上面代码的基础上修改,一气呵成完成重命名复制并生成单通道的label图片

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  9 10:47:36 2018

@author: yxh
"""
import os
import sys
import shutil
from PIL import Image
path = '/home/yxh/linux_v1.4.3/beifen/'

def reName(path):
    fileList = []
    
    files = os.listdir(path)
    i=0
    for f in files:
        file_name, file_extend = os.path.splitext(f)
        new_name = file_name.split('_')[0]
        dirpath = path + '/' +f
        if(os.path.isdir(dirpath)):
            dirfiles = os.listdir(dirpath)
            for d in dirfiles:
                if(d == 'label.png'):
                    #file_path = os.path.join(dirfiles, d)
                    
                    src = os.path.join(os.path.abspath(dirpath), d)
                    src = Image.open(src).convert('LA')
                    
                    dst = os.path.join(os.path.abspath(dirpath), new_name + '.png')
                    src.save(dst)
                    #os.rename(src, dst)
                    #img = Image.open('dst').convert('I')  //“I”为单通道,“L”为灰度图,但是灰度图其实实际上也是三通道
                    #img.save(dst)
                    file_path  = dst
                    new_path = '/home/yxh/models-deeplabv3/research/deeplab/datasets/Lane/Label/' + new_name + '.png'
                    shutil.copy(file_path, new_path)


reName(path)

主要用到的代码是

from PIL import Image
img = Image.open('image.png').convert('I')
img.save('greyscale.png')

参考文档(很不错!):在Python中,如何将RGB图像转换为灰度图像?

你可能感兴趣的:(Python图片重命名并复制并将label数据集改成单通道——deeplabv3+制作自己的训练数据集)