简介:labelme是图形图像注释工具,它是用Python编写的,并将Qt用于其图形界面,是麻省理工(MIT)的计算机科学和人工智能实验室(CSAIL)研发的图像标注工具,人们可以使用该工具创建定制化标注任务或执行图像标注,项目源代码已经开源。
功能:
1、对图像进行多边形,矩形,圆形,多段线,线段,点形式的标注(可用于目标检-测,图像分割等任务)。
2、对图像进行进行 flag 形式的标注(可用于图像分类 和 清理 任务)。
3、视频标注 - 生成 VOC 格式的数据集(for semantic / instance segmentation) - 生成 COCO 格式的数据集(for instance segmentation)
安装:安装过程比较慢,耐心等候。
conda create --name=labelme python=3.8.1
conda activate labelme
pip install labelme
启动:
conda activate labelme
labelme
菜单栏功能:
标注:
提供了种类繁多的标注性质,满足各种绘制要求:
保存结果:标注文件一般采用默认原视频名称.json
实现labelme批量json_to_dataset方法:使用下面的代码覆盖保存
修改Anaconda\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()
异常处理:
AttributeError: module ‘labelme.utils‘ has no attribute ‘draw_label‘
安装指定版本labelme:
pip install labelme==3.16.5
任意位置新建文件夹:jpg、json
将标注的文件分别存储到jpg和json文件夹中。
进入labelme_json_to_dataset.exe所在目录
执行命令:labelme_json_to_dataset.exe 标注的json文件夹所在的目录
如:labelme_json_to_dataset.exe C:\test_label\json
运行结果: