首页 > 编程知识 正文

标注自己的数据集,label标签怎么导入图片

时间:2023-05-03 20:24:36 阅读:173287 作者:4343

首先记录Labelme的使用方法,便于快速使用。

labelme概述labelme可用于实例划分、语义划分、目标发现和分类任务的数据集标记任务。

在线标记版本

python版本

labelme正式文档分类标记: Classification

目标检测标记: Object Detection

语义拆分标注: Semantic Segmentation

实例化标记:实例化分段

视频注释:视频注释

其他格式标记: LabelMe Primitives

所有安装操作都将在安装了Anaconda的环境中执行

安装pyqt5

安装pipinstallpyqt5- I https://pypi.tuna.Tsinghua.edu.cn/simple2. label me

pipinstallabelme-I https://pypi.tuna.Tsinghua.edu.cn/simple3.安装完成后,从命令行启动标签me

标签me

在这里打开图像文件夹进行演示。

1 .单击左侧的Open Dir选择要标记的数据文件夹。

2 .在顶部的edit菜单栏中,可以按照多边形(默认)、矩形、圆、直线、点的顺序选择不同的方案。

3 .创建图像分割数据,选择多边形,单击左侧的create polygons,返回图像,按鼠标左键生成点,标记完成后形成标记区域。 同时将显示labelme框,输入标签名称,然后单击“确定”或“换行符”完成标记。

1 .如果需要更改标注中的数据,可以选择左侧的编辑框,或者将鼠标移动到标签上并单击鼠标右键,然后选择编辑标签或标注的名称。 在编辑模式下,将鼠标移动到边界上,单击鼠标右键可以增加点。

2 .标记完成后单击“Save”保存。 将在图像路径下生成同名的json文件。 在目录中打开终端并输入:

labelme_json_to_dataset文件名. json将生成的json转换为相应的数据文件。

*.png info.YAML label.png label _ names.txtlabel _ viz.png常用命令1.启动labelme的方式

直接打开labelmelabelme#打开#文件夹,然后加载此文件夹下及其子文件夹下的所有图像labelme path/to/imgfile/#指定的图像labelme cat.1.jpg#标记同时gui窗口labelmecat.1.jpg-ocat.1.jpg.jpg.jpg自动关闭labellistlabelmecat.1.jpg--labels cat,eye #或传递

#在当前目录下生成文件夹cat _1_ jsonlabelme _ JSON _ to _ dataset cat.1.JSON #将生成文件夹命名为cat1label me _ JSON _ to _ datataset

#终端输入labelme_draw_json cat.1.json

2.将json文件转换为image和label

1 .在目录下创建新的labels.txt文件。 内容是分割的标签,默认内容设定如下。

2 .创建新的labelme2voc.py文件。

您可以从labelme工程目录下的labelme2voc.py文件中复制内容,也可以使用以下代码:

#! /usr/bin/envpythonfrom _ future _ import print _ functionimportargparseimportglobimportos.pathasospimportsysimportimgort labelmmedefmmet formatter _ class=arg parse.argumentdefaultshelpformatter (arser.argumentdefaultshelpformatter ) paber help='。 parser.add _ argument (' output _ dir ',help='output dataset directory ' ) parser

"--noviz", help="no visualization", action="store_true" ) args = parser.parse_args() if osp.exists(args.output_dir): print("Output directory already exists:", args.output_dir) sys.exit(1) os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, "JPEGImages")) os.makedirs(osp.join(args.output_dir, "SegmentationClass")) os.makedirs(osp.join(args.output_dir, "SegmentationClassPNG")) if not args.noviz: os.makedirs( osp.join(args.output_dir, "SegmentationClassVisualization") ) print("Creating dataset:", args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).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.output_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) for filename in glob.glob(osp.join(args.input_dir, "*.json")): print("Generating dataset from:", filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg") out_lbl_file = osp.join( args.output_dir, "SegmentationClass", base + ".npy" ) out_png_file = osp.join( args.output_dir, "SegmentationClassPNG", base + ".png" ) if not args.noviz: out_viz_file = osp.join( args.output_dir, "SegmentationClassVisualization", base + ".jpg", ) with open(out_img_file, "wb") as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) lbl, _ = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) np.save(out_lbl_file, lbl) if not args.noviz: viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), font_size=15, label_names=class_names, loc="rb", ) imgviz.io.imsave(out_viz_file, viz)if __name__ == "__main__": main()

3.转换为voc数据格式

# 终端输入python labelme2voc.py [图像路径] [voc文件夹名称] --labels [label list]# 比如python labelme2voc.py ./id_labelme/images ./id_labelme/target --labels labels.txt

在目录下会根据设定自动生成目标文件夹。文件夹下内容如下所示:

参考

labelme使用

深度学习图像标注工具-labelme

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。