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

CV图像处理小工具——json文件转mask

CV图像处理小工具——json文件转mask

import cv2
import json
import numpy as np
import osdef func(file_path: str) -> np.ndarray:try:with open(file_path, mode='r', encoding="utf-8") as f:configs = json.load(f)# 检查JSON是否包含必要的字段if "shapes" not in configs or "imageHeight" not in configs or "imageWidth" not in configs:raise ValueError("JSON文件缺少必要的字段")shapes = configs["shapes"]image_height = configs["imageHeight"]image_width = configs["imageWidth"]png = np.zeros((image_height, image_width, 3), np.uint8)for shape in shapes:if "points" not in shape:raise ValueError("形状缺少点信息")points = np.array(shape["points"], np.int32).reshape((-1, 1, 2))  # 确保点是正确的形状cv2.fillPoly(png, [points], (0, 0, 255))return pngexcept Exception as e:print(f"处理文件 {file_path} 时出错: {e}")return None# 硬编码的文件路径input_directory = "H:/"  # 可以是单个文件路径或目录路径
output_directory = "H:/"# 确保输出目录存在
if not os.path.exists(output_directory):os.makedirs(output_directory)# 处理单个文件或目录中的文件
if os.path.isfile(input_directory) and input_directory.endswith(".json"):# 处理单个JSON文件input_file = input_directoryoutput_file = os.path.join(output_directory, os.path.splitext(os.path.basename(input_file))[0] + ".png")image = func(input_file)if image is not None:cv2.imwrite(output_file, image)
else:# 处理目录中的JSON文件for file_name in os.listdir(input_directory):if file_name.endswith(".json"):input_file = os.path.join(input_directory, file_name)output_file = os.path.join(output_directory, os.path.splitext(file_name)[0] + ".png")image = func(input_file)if image is not None:cv2.imwrite(output_file, image)

在这里插入图片描述


http://www.mrgr.cn/news/49889.html

相关文章:

  • SRAM 中 Multi-Vt 选择(BASE、LL、ULL)
  • HTTP session
  • bat脚本banenr
  • 基于Zabbix进行服务器运行情况监测
  • BBR ProbeRTT 和 ProbeBW 相互作用
  • 过拟合和欠拟合的区别
  • 某乎接口zse96解析(附带可直接运行js)
  • 解析淘宝商品评论API返回值中的评价热度与关注度
  • Flume面试整理-Flume的基本架构
  • three.js快速入门 ---Threejs第一个3D场景
  • 施耐德CPU模块TSXP57204M TSX P57204M
  • 量化选股:原理与实战指南(一)
  • 多模态大模型
  • 数据结构4——栈
  • 蓝牙定位系统:实现精准定位、提高效率的利器
  • C#中判断的应用说明一(if语句)
  • Web Worker加载外部文件实践
  • Kimi沉寂两个月后推出探索版,告诉你最好的搜索就是不用搜索
  • IRP默认最小流程
  • 【每天学点AI】人工智能大模型评估标准有哪些?