语义分割制作自己的数据集
1、修改D:\Anaconda3\Lib\site-packages\labelme\cli下的json_to_dataset.py.
import argparse
import json
import os
import os.path as osp
import warningsimport PIL.Image
import yamlfrom labelme import utils
import base64def main():warnings.warn("This script is aimed to demonstrate how to convert the\n""JSON file to a single image dataset, and not to handle\n""multiple JSON files to generate a real-use dataset.")parser = argparse.ArgumentParser()parser.add_argument('json_file')parser.add_argument('-o', '--out', default=None)args = parser.parse_args()json_file = args.json_fileif args.out is None:out_dir = osp.basename(json_file).replace('.', '_')out_dir = osp.join(osp.dirname(json_file), out_dir)else:out_dir = args.outif not osp.exists(out_dir):os.mkdir(out_dir)count = os.listdir(json_file)for i in range(0, len(count)):path = os.path.join(json_file, count[i])if os.path.isfile(path):data = json.load(open(path))if data['imageData']:imageData = data['imageData']else:imagePath = os.path.join(os.path.dirname(path), 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)label_name_to_value = {'_background_': 0}for shape in data['shapes']: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# label_values must be denselabel_values, label_names = [], []for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):label_values.append(lv)label_names.append(ln)assert label_values == list(range(len(label_values)))lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)captions = ['{}: {}'.format(lv, ln)for ln, lv in label_name_to_value.items()]lbl_viz = utils.draw_label(lbl, img, captions)out_dir = osp.basename(count[i]).replace('.', '_')out_dir = osp.join(osp.dirname(count[i]), out_dir)if not osp.exists(out_dir):os.mkdir(out_dir)PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))# PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.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')warnings.warn('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)print('Saved to: %s' % out_dir)if __name__ == '__main__':main()
2、将所有json文件放在一个文件夹中E:\oralcell\test327\json 进入环境D:\Anaconda3\Scripts 这个目录下有labelme_json_to_dataset.exe 执行
labelme_json_to_dataset.exe E:\oralcell\test327\json
生成的文件夹在D:\Anaconda3\Scripts中,将他们放在E:\oralcell\test327\package中
每个json文件生成一个json文件夹,文件夹中有五项,要用的是label.png。
如果出错可能是labelme版本不对
pip install labelme==3.16.7
3、将每个json文件夹中的label取出来放在一个文件夹中E:\oralcell\test327\png,并重命名为原图的名字。
# -*- coding: utf-8 -*-
import os
import numpy as np
import json
import shutildef find_dir_path(path, keyword_name, dir_list):files = os.listdir(path)for file_name in files:file_path = os.path.join(path, file_name)if os.path.isdir(file_path) and keyword_name not in file_path:find_dir_path(file_path, keyword_name, dir_list)elif os.path.isdir(file_path) and keyword_name in file_path:dir_list.append(file_path)all_result_path = []
src_path = r'E:\oralcell\test327\package'#json文件夹的目录
label_save_path = r'E:\oralcell\test327\png'#png存放的目录
find_dir_path(src_path, '_json', all_result_path) # 找出所有带着关键词(_json)的所有目标文件夹
# print(all_result_path)for dir_path in all_result_path:# print(dir_path)file_name = dir_path.split('\\')[-1]key_word = file_name[:-5]# print(key_word)label_file = dir_path + "\\" + "label.png"new_label_save_path = label_save_path + "\\" + key_word + ".png" # 复制生成的label.png到新的文件夹# print(new_label_save_path)shutil.copyfile(label_file, new_label_save_path)
4、改通道数
#批量图片转换:将RGB模式或P模式转换为L灰度拉伸值(可以自定义拉伸大小)
import numpy as np
from PIL import Image
import os
def Mode_P_to_L(img_file,stretch_value):"""
将当前文件下的所有图片进行灰度值转换:param img_file: 图片文件存放目录:param stretch_value: 拉伸值, 图片原始值*stretch_value的结果理论上应该小于255"""##获取目录下所有文件名file_name_list = os.listdir(img_file)#遍历所有图片文件for file in file_name_list:#获取某个图片的全路径img_path = os.path.join(img_file, file)#打开图片image = Image.open(img_path)print(image.mode) #p模式img_arry = Image.fromarray(np.uint8(image))print(img_arry.mode)#L模式img_L = img_arry.convert("L")print(img_L.mode)#灰度拉伸img_end = Image.fromarray(np.uint8(img_L) * stretch_value)print(img_end.mode)#保存图片,并覆盖原图img_end.save(img_path)#提示print("完成对图片:",file," 的转换!")print("所有图片均已完成转换!")#程序主入口
if __name__ == "__main__":# 需要转换的图片所在文件目录img_file = r"E:\oralcell\label327\png\segmentations"#自己输入上一步生成的png格式的标签图stretch_value = 1#自定义拉伸值,但要注意,图片标签值*stretch_value的结果理论上应该小于255Mode_P_to_L(img_file, stretch_value)#调用自定义的转换方法
语义分割制作自己的数据集相关推荐
- 最详细的语义分割---01如何读取数据集?
数据集组成 网络训练的第一步就是读取数据,关于输入图片如何读取,如何进行预处理,将会在本篇文章中进行演示. 首先需要了解的是,语义分割中图片和标签是分别保存的.以voc数据集为例,它有20个类别,加上 ...
- pytorch 语义分割-医学图像-脑肿瘤数据集的载入模块
由于最近目标是完成基于深度学习的脑肿瘤语义分割实验,所以需要用到自定义的数据载入,本文参考了一下博客:https://blog.csdn.net/tuiqdymy/article/details/84 ...
- 点云语义分割:PointNet训练S3DIS数据集
文章目录 一.数据准备 1.1.数据下载 二.训练 三.测试 四.6折交叉验证 项目地址:pointnet 此次我们是用pointnet网络来做语义分割.代码在pointnet项目中的sem_seg文 ...
- 点云语义分割:pointnet++训练S3DIS数据集
文章目录 一.数据预处理 二.训练 三.测试 四.6折交叉验证 tensorflow版本的pointnet++没有关于S3DIS数据集训练代码,我尝试参考ScanNet的训练代码改写成训练S3DIS数 ...
- 视频教程-DeepLabv3+图像语义分割实战:训练自己的数据集-计算机视觉
DeepLabv3+图像语义分割实战:训练自己的数据集 大学教授,美国归国博士.博士生导师:人工智能公司专家顾问:长期从事人工智能.物联网.大数据研究:已发表学术论文100多篇,授权发明专利10多项 ...
- 语义分割数据集Pascal VOC2012的读取与处理
语义分割数据集Pascal VOC2012的读取与处理 前言 读取文件路径 数据预处理 自定义数据集类 完整代码 前言 Pascal VOC2012是语义分割的一个重要数据集.学习一下使用Pytorc ...
- DeepLabV3+语义分割实战
DeepLabV3+语义分割实战 语义分割是计算机视觉的一项重要任务,本文使用Jittor框架实现了DeepLabV3+语义分割模型. DeepLabV3+论文:https://arxiv.org/p ...
- 使用语义分割架构的文档扫描仪 DeepLabV3
0 介绍 地址:https://learnopencv.com/deep-learning-based-document-segmentation-using-semantic-segmentatio ...
- 目前缺少用于语义分割的 3D LiDAR 数据吗?关于三维点云数据集和方法的调查
目前缺少用于语义分割的 3D LiDAR 数据吗?关于三维点云数据集和方法的调查 原文 Are We Hungry for 3D LiDAR Data for Semantic Segmentatio ...
最新文章
- mysql 一键获取数据库表结构
- 微软发布多项Azure Kubernetes服务更新,增加GPU支持
- 深入浅出:对MySQL主从配置的一些总结
- Java高并发编程(一):并发编程的挑战
- Python实现二叉树的三种深度遍历方法!
- bootstrap Table的使用方法
- C++中如何读取一个数的位数_C语言编写程序求水仙花数
- arduino char*转string_Java 中 String 类的常用方法汇总
- 每天快走一小时,身体会有什么变化?
- (10)JavaScript学习笔记 - 数组
- [导入]Reporting Services 3: 报表模型项目
- 用c语言把蜂鸣器封装成函数,C语言蜂鸣器问题
- 计算机编辑文档教程,列举Word文档中常用编辑操作
- 微信小程序 - 方法
- Canvas Scaler
- High Reward Low Risk Strategies
- 利用端端Clouduolc的双向同步和单向同步,打造多机热备份的文件下载服务器
- python多久可以入门_python自学要多久能学会
- 基于Python3.6和Opencv3的活动轮廓模型--CV和RSF
- JVM-对象什么时候进入老年代(实战篇)
热门文章
- Google Chrome源码剖析
- Quartus II下载器件库
- 连接数据库SSL警告: Establishing SSL connection without server’s identity verification is not recommended.
- 【Docker】自定义dockerfile构建容器镜像并发布
- 自相关(ACF)与偏自相关(PACF)(4)
- 系统集成项目管理工程师笔记_备考常见英文词汇汇总
- tagslam框架:LiDARTag和AprilTags,只使用特定标签的雷达/相机
- SQL Server基础操作(此随笔仅作为本人学习进度记录六 !--程序块和循环)
- 对于越野车来说 带大梁,前后硬桥!
- 关于VMware Desktone中的Slony和数据库