话不多少,直接上代码,更换源目录和目标目录即可

1、导入库

import cv2
import numpy as np
import os
import shutil
import matplotlib.pyplot as plt

2、设置源目录/输出目录

json_dir = 'G:/json_filedir/'    #json文件所在文件夹(注:文件夹中只能包含json文件)
label_dir = 'G:/label_filedir/'   #目标输出文件夹

3、定义转换函数

def json2png(json_folder, png_save_folder):
    if os.path.isdir(png_save_folder):
        shutil.rmtree(png_save_folder)#清除输出文件夹
    os.makedirs(png_save_folder)#重新创建输出文件夹
    json_files = os.listdir(json_folder)
    for json_file in json_files:
        json_path = os.path.join(json_folder, json_file)#生成单个源json文件的路径
        os.system("labelme_json_to_dataset {}".format(json_path))#生成label文件夹及label.png文件
        label_path = os.path.join(json_folder, json_file.split(".")[0] + "_json/label.png")#生成label.png的系统路径
        temporary_path = os.path.join(json_folder, json_file.split(".")[0] + "_json")
        png_save_path = os.path.join(png_save_folder, json_file.split(".")[0] + ".png")#生成目标目录
        label_png = cv2.imread(label_path, 0)#读取label.png
        cv2.imwrite(png_save_path, label_png)#将label.png文件重新保存在目标目录下
        shutil.rmtree(temporary_path)#每次循环执行完后删除生成的临时文件,不删的话,再次执行时需要手动删除

4、执行函数,转换格式

#执行函数(批量转换)
json2png(json_folder=json_dir, png_save_folder=label_dir)

5、查看效果

test_img = cv2.imread('G:/test.jpg')
test_label = cv2.imread('G:/label_filedir/test.png')

plt.figure(figsize=(16,8))
plt.subplot(121),plt.imshow(test_img,'gray')
plt.subplot(122),plt.imshow(test_label,'gray')
plt.show()

 

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 3 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐