语义分割制作自己的voc数据集

news/2024/5/20 7:35:04 标签: 语义分割, json, 批量转换, voc, 批处理

目录

1.数据标注

2.单文件转换

3.数据批处理

4.数据集重命名处理

5. 创建imageSets


1.数据标注

见《数据集标注》

2.单文件转换

使用conda安装完后,在conda安装目录下有如下几个可执行程序,使用labelme_json_to_dataset.exe可以转换单个json文件,但不能批量转换

输入以下命令进行单个文件转换:

labelme_json_to_dataset.exe json文件路径

3.数据批处理

在conda安装目录中,进入到如下目录:"你的conda安装路径\Lib\site-packages\labelme\cli\",现将原先的json_to_dataset.py文件备份,将该脚本使用以下代码代替:

import argparse
import base64
import json
import os
import os.path as osp

import PIL.Image
import yaml

from labelme.logger import logger
from labelme import utils

def label(json_file, out_dir, label_name_to_value):
    data = json.load(open(json_file))
    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)
    
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
    


def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file_dir')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()
    label_name_to_value = {'_background_': 0}
    
    path = args.json_file_dir
    dirs = os.listdir(path)
    for json_name in dirs:
        if args.out is None:
            out_dir = osp.basename(json_name).replace('.', '_')
            out_dir = osp.join(osp.dirname(json_name), out_dir)
        else:
            out_dir = args.out+'/'+osp.basename(json_name).replace('.', '_')
        if not osp.exists(out_dir):
            os.mkdir(out_dir)
        
        json_file=path+'/'+json_name
        label(json_file, out_dir, label_name_to_value)
        

if __name__ == '__main__':
    main()

将标注好的所有数据的json文件都放到一个文件夹中,在cmd中定位到该文件夹,进入conda环境,输入以下命令:

activate labelme
labelme_json_to_dataset json<文件夹>

参考:《制作自己的语义分割数据集》

4.数据集重命名处理

对所有label.png文件重命名,将转换后的所有文件夹统一放到一个文件夹下,我命名为dataset,作为批处理的输入,创建一个新的文件夹,我命名为labelpng,作为输出。批处理程序如下:

import os
import shutil

inputdir = 'E:\\temp\\dataset'
outputdir= 'E:\\temp\\labelpng'

for dir in os.listdir(inputdir):
    
    #设置旧文件名(就是路径+文件名)
    oldname = inputdir + os.sep + dir.split('_')[0]+'_'+dir.split('_')[1] + os.sep + 'label.png'   # os.sep添加系统分隔符
    
    #设置新文件名
    newname = outputdir + os.sep + dir.split('_')[0]+'.png'
    
    shutil.copyfile(oldname, newname)   #用os模块中的rename方法对文件改名
    print(oldname, '======>', newname)

执行完成后所有的label.png文件就转换成原始图像对应名字的.png文件了。

将所有mask和原始图像放到voc2012对于的文件夹中就可以了。

5. 创建imageSets

使用如下代码将创建好的mask进行数据分割:

from sklearn.model_selection import train_test_split
import os

imagedir = 'E:/temp/segment_dataset/0918/labelpng/'
outdir = 'E:/temp/segment_dataset/0918/imagesets_seg/'

images = []
for file in os.listdir(imagedir):
        filename = file.split('.')[0]
        images.append(filename)

train, test = train_test_split(images, train_size=0.7, random_state=0)
val, test = train_test_split(test, train_size=0.2/0.3, random_state=0)

with open(outdir+"train.txt", 'w') as f:
	f.write('\n'.join(train))
	
with open(outdir+"val.txt", 'w') as f:
	f.write('\n'.join(val))
	
with open(outdir+"test.txt", 'w') as f:
	f.write('\n'.join(test))

with open(outdir+"trainval.txt", 'w') as f:
	f.write('\n'.join(images))

 


http://www.niftyadmin.cn/n/1784596.html

相关文章

英语日常用语

Dinner is on me. 晚饭我请。You ask for it! 活该&#xff01;You dont say! 真想不到&#xff01;Get out of here! 滚出去&#xff01;How come… 怎么回事&#xff0c;怎么搞的。Dont mention it. 没关系&#xff0c;别客气。It is not a big deal! 没什么了不起&#xff0…

c++中成员函数重载

函数重载简单说是一个函数实现多个功能&#xff0c;函数名称相同但函数的参数不同或参数类型不同。 int add(int a,int b); float add(float a,float b); int add(int a,int b,int c); 以上三个函数函数名称相同&#xff0c;但参数类型或个数不同&#xff0c;在c语义中分别…

用apt为RHEL设置自动更新

你说,生活究竟是为了什么? "等死"!活了大半辈子,就是为了入土的这一天,所以,在死之前,请好好的珍惜每一天.-------------------------------------------------------------------------------终于搞定Linux服务器的自动更新功能了.其实过程很简单,可自己却云山雾里…

SUN OS 5.10(solairs 10) 环境下搭建 vsftp 服务

SUN OS 5.10 VSFTPD 操作系统&#xff1a;sun os 5.10 for x86 1&#xff0e;安装VSFTPD-2.3.0 # gzip –d vsftpd-2.3.0.tar.gz # tar xvf vsftpd-2.3.0.tar # cd vsftpd-2.3.0 # make install # mv vsftpd.conf /etc/ # mkdir /usr/share/empty 2&#xff0e; 建立vs…

组建优秀的团队-实现目标的开始

组建优秀的团队-实现目标的开始<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />Jack Zhai“霸王理论”中不仅树立了团队共同的目标&#xff0c;而且要求组建优秀的团队。看过<三国演义>的人都很羡慕刘备在“无兵无地”的条…

ubuntu系统访问ubuntu系统服务器

本地安装ssh&#xff0c;并开启ssh服务。 service sshd started 访问远程服务器命令&#xff1a; sudo ssh rootip地址 -p 端口号 本地ubuntu与远程服务器通信&#xff0c;数据传输&#xff0c;使用scp命令&#xff0c;速度超快的&#xff0c;本地传输到服务器命令格式如下…

配置Domino CA以支持SSL

图文并茂为您详解如何配置Domino CA以支持SSL部分文字参考网上《如何配置Domino CA以支持SSL》一文&#xff0c;但是原文只有晦涩的文字没有图&#xff0c;且文中描述为翻译后的中文&#xff0c;和实际配置中的英文环境有一定的个人理解差异&#xff0c;加上有部分解释不够完善…

AxWindowsMediaPlayer媒体文件主要方法属性 (转)

AxWindowsMediaPlayer媒体文件主要方法属性 属性/方法名&#xff1a; 说明&#xff1a; [基本属性]   URL:String; 指定媒体位置&#xff0c;本机或网络地址 uiMode:String; 播放器界面模式&#xff0c;可为Full, Mini, None, Invisible playState:integer; 播放状态&am…