首先感谢大佬:
https://blog.csdn.net/tzwsg/article/details/114653071
一、上代码,json2gray.py:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# !H:\Anaconda3\envs\new_labelme\python.exe
import argparse
import json
import os
import os.path as osp
import base64
import warnings
import PIL.Image
import yaml
from labelme import utils
import cv2
import numpy as np
from skimage import img_as_ubyte
# from sys import argv
def 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_file
#json_file = "./make1" ###json所在文件夹
# freedom
list_path = os.listdir(json_file)
print('freedom =', json_file)
for i in range(0, len(list_path)):
path = os.path.join(json_file, list_path[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(path).replace('.', '_')
out_dir = osp.basename(path).split('.json')[0]
save_file_name = out_dir
# out_dir = osp.join(osp.dirname(path), out_dir)
if not osp.exists(json_file + 'mask'):
os.mkdir(json_file + 'mask')
maskdir = json_file + 'mask'
if not osp.exists(json_file + 'mask_viz'):
os.mkdir(json_file + 'mask_viz')
maskvizdir = json_file + 'mask_viz'
out_dir1 = maskdir
# if not osp.exists(out_dir1):
# os.mkdir(out_dir1)
# PIL.Image.fromarray(img).save(out_dir1 + '\\' + save_file_name + '_img.png')
PIL.Image.fromarray(lbl).save(out_dir1 + '/' + save_file_name + '.png')
PIL.Image.fromarray(lbl_viz).save(maskvizdir + '/' + save_file_name +
'_label_viz.png')
# if not osp.exists(json_file + '\\' + 'mask_png'):
# os.mkdir(json_file + '\\' + 'mask_png')
# mask_save2png_path = json_file + '\\' + 'mask_png'
################################
# mask_pic = cv2.imread(out_dir1+'\\'+save_file_name+'_label.png',)
# print('pic1_deep:',mask_pic.dtype)
# mask_dst = img_as_ubyte(lbl) # mask_pic
# print('pic2_deep:', mask_dst.dtype)
# cv2.imwrite(mask_save2png_path + '\\' + save_file_name + '_label.png', mask_dst)
##################################
with open(osp.join(out_dir1, '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_dir1, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
print('Saved to: %s' % out_dir1)
if __name__ == '__main__':
# base64path = argv[1]
main()
可能要求你安装cv2,我安装了py-opencv 4.6.0
二、输出灰度图
这个路径下,有两个.json文件,其实是上一篇文章生成的,我拷贝到下图的路径中了。注意,如果这个文件夹下还有别的东西,最好都拷走!
我执行
(labelme) C:\ProgramData\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli>python json2gray.py D:\BaiduNetdiskDownload\gao
如下图所示:
结果你看,新增了并列的两个文件夹:
打开gaomask
肉眼看1.png和2.png漆黑一片。
其实啊,我可以用机器视觉神器:halcon,看一下。
说明第0类,是背景类,灰度值为0
第1类是左边那个区域,灰度值为1
第2类是右边那个区域,灰度值为2.
看看下面的图,你就更清楚了: