本文主要是介绍使用labelme标注ground truth,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
step1:标注
直接在Anconda prompt 中输入 labelme 即可打开标注软件
Step2: json 转png
原文链接:https://blog.csdn.net/weixin_45437022/article/details/114751314?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscore&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscorehttps://blog.csdn.net/weixin_45437022/article/details/114751314?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscore&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscore
import argparse
import json
import os
import os.path as osp
import warnings
import numpy as np
import PIL.Image
import yaml
from labelme import utilsdef main():json_file='F:/Anaconda/data_json/' #这里填入你的json文件的文件夹路径list = os.listdir(json_file)for i in range(0, len(list)):path = os.path.join(json_file, list[i])if os.path.isfile(path):data = json.load(open(path))img = utils.img_b64_to_arr(data['imageData'])lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]lbl_viz = utils.draw_label(lbl, img, captions)out_dir = osp.basename(list[i]).replace('.', '_')out_dir = osp.join(osp.dirname(list[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'))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 lbl_names:f.write(lbl_name + '\n')warnings.warn('info.yaml is being replaced by label_names.txt')info = dict(label_names=lbl_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()
Step3:将mask.png 转化成二值图
import cv2#cv2.imread 的FLAG设置为2
#否则图像是全0的
img = cv2.imread('./5.png',2) img_255 = 255 * img#保存成 .jpg 格式
#若保存成 .png 格式,还是全黑的,不知道什么原因。
cv2.imwrite('./5.jpg', img_255 )
这篇关于使用labelme标注ground truth的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!