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

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

CV图像处理小工具——json文件转P格式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.lryc.cn/news/457537.html

相关文章:

  • Typora 快捷键操作大全
  • <Project-8.1.1 pdf2tx-mm> Python 调用 ChatGPT API 翻译PDF内容 历程心得
  • JDK1.1主要特性
  • 软件测试工作中-商城类项目所遇bug点
  • Java多线程面试题
  • 安徽大学《2022年+2023年831自动控制原理真题》 (完整版)
  • Vulnhub靶场案例渗透[6]- DC6
  • FreeSWITCH 分机网关路由
  • 数据交换的金钟罩:合理利用安全数据交换系统,确保信息安全
  • 区块链积分系统:重塑支付安全与商业创新的未来
  • Django学习笔记十三:优秀案例学习
  • SSH 公钥认证:从gitlab clone项目repo到本地
  • linux 搭建sentinel
  • 微服务发展历程
  • 提示词优化、GPTs逆向工程、大语言模型原理、大语言模型优化、开源模型本地私有化部署、从零构建大语言模型、智能体构建以及大语言模型的发展趋势
  • nn.Conv2d(二)
  • 获取鸿蒙设备Udid遇到的问题
  • 【华为HCIP实战课程十】OSPF网络DR和BDR实战讲解,网络工程师
  • 视频怎么去除杂音保留人声?让人声更动听!视频噪音处理攻略
  • 洗衣店数字化转型:Spring Boot订单管理
  • Koa学习
  • linux线程 | 线程的概念
  • 2024年软件设计师中级(软考中级)详细笔记【3】数据结构(下)(分值5分)
  • WPF|依赖属性SetCurrentValue方法不会使绑定失效, SetValue方法会使绑定失效?是真的吗?
  • Windows搭建Java开发环境(Building a Java development environment on Windows)
  • 用FPGA做一个全画幅无反相机
  • 使用 Go 语言与 Redis 构建高效缓存与消息队列系统
  • springboot 整合spring ai实现 基于知识库的客服问答
  • 云原生(四十九) | WordPress源码部署
  • Spring Boot 集成 LiteFlow 实现业务流程编排