当前位置: 首页 > news >正文

格式转化——Labelme标注好的json文件批量转为png(标签)文件(物体为红色,背景为黑色)和jpg原图

作用如题目,批量将标注好的json文件转成png标签,jpg原图,其中标签时红黑图。

代码如下:

import argparse
import base64
import json
import os
import os.path as osp
import imgviz
import PIL.Image
import yaml
from labelme.logger import logger
from labelme import utils'''single json file'''
def main():logger.warning("This script is aimed to demonstrate how to convert the ""JSON file to a single image dataset.")logger.warning("It won't handle multiple JSON files to generate a ""real-use dataset.")parser = argparse.ArgumentParser()#parser.add_argument("json_file")parser.add_argument('--jsonfiles', nargs='?', const=True, default="datasets//before", help='resume most recent training') #设置输入路径  将default里面的路径转成json文件下的路径parser.add_argument("-o", "--out", default="datasets//png")  #设置输出路径 default里面的路径改为要存放标签png和原图jpg的路径args = parser.parse_args()import globjsonfiles = glob.glob(args.jsonfiles+"/*.json")print(jsonfiles)for json_file in jsonfiles:if 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)data = json.load(open(json_file))imageData = data.get("imageData")if not imageData:imagePath = os.path.join(os.path.dirname(json_file), 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 sorted(data["shapes"], key=lambda x: x["label"]):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_valuelbl, _ = utils.shapes_to_label(img.shape, data["shapes"], label_name_to_value)label_names = [None] * (max(label_name_to_value.values()) + 1)for name, value in label_name_to_value.items():label_names[value] = namelbl_viz = imgviz.label2rgb(label=lbl, image=imgviz.asgray(img), label_names=label_names, loc="rb")label_name  = json_file.split('\\')[-1].replace('.json','.png')image_name  = json_file.split('\\')[-1].replace('.json','.jpg')PIL.Image.fromarray(img).save(osp.join(out_dir, image_name))utils.lblsave(osp.join(out_dir, label_name), 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")# 生成info.yaml文件# logger.warning('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)logger.info("Saved to: {}".format(out_dir))if __name__ == "__main__":main()

http://www.lryc.cn/news/355742.html

相关文章:

  • 力扣刷题--2535. 数组元素和与数字和的绝对差【简单】
  • 2024年【危险化学品经营单位安全管理人员】考试报名及危险化学品经营单位安全管理人员找解析
  • IntelliJ IDEA集成Baidu Comate,商城系统支付交易功能开发实战
  • 20212313 2023-2024-2 《移动平台开发与实践》第5次作业
  • Python图形界面(GUI)Tkinter笔记(十二):用【Entry()】实现单行文本输入(3)
  • 前端渲染页面的原理
  • 【一竞技DOTA2】RAMZES666替补参加裂变联赛
  • 1109 擅长C(测试点0,1,2,3)
  • 北京新高度画室:端午假期免费吃,住,学!
  • 电脑重要文件如何加密保护?教你两种方法
  • 新零售收银解决方案:传统门店超市的数字化-亿发
  • 独家揭秘!Amazon、lazada、Shopee测评自养号,新手也能秒变高手!
  • 企企通入选第一新声《2024年中国CIO数字化产品选型白皮书》供应链数字产品可信名录
  • Linux中 “权限设置修改”
  • 9.1 Go语言入门(环境篇)
  • 简单的网页分享按钮代码
  • ld链接文件
  • React-事件绑定
  • 使用“tcpdump”查看原始数据包
  • Python字符串处理全面教程
  • 基于微信小程序+ JAVA后端实现的【微信小程序跑腿平台】设计与实现 (内附设计LW + PPT+ 源码+ 演示视频 下载)
  • 使用 VALUES 子句构建数据集
  • for循环绑定id,更新html页面的文字内容
  • claude3国内API接口对接
  • Java:IO
  • ubuntu安全加固
  • 【MySQL】数据库的开始
  • 线性稳压电路和开关稳压电路
  • Leetcode:找出峰值
  • 简单微信企业群消息推送接口