Python将Labelme的Json标注文件进行增、删、改、查

Python将Labelme的Json标注文件进行增、删、改、查

  • 前言
  • 前提条件
  • 相关介绍
  • 实验环境
  • Json标注文件的增、删、改、查
      • 代码实现
      • 输出结果
      • 代码实现
      • 输出结果
      • 代码实现
      • 输出结果
      • 代码实现
      • 输出结果

在这里插入图片描述

前言

  • 由于本人水平有限,难免出现错漏,敬请批评改正。
  • 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏或我的个人主页查看
  • YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
  • 基于DETR的人脸伪装检测
  • YOLOv7训练自己的数据集(口罩检测)
  • YOLOv8训练自己的数据集(足球检测)
  • YOLOv5:TensorRT加速YOLOv5模型推理
  • YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
  • 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
  • YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
  • YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
  • Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
  • YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
  • 使用Kaggle GPU资源免费体验Stable Diffusion开源项目

前提条件

  • 熟悉Python

相关介绍

  • Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
  • PyTorch 是一个深度学习框架,封装好了很多网络和深度学习相关的工具方便我们调用,而不用我们一个个去单独写了。它分为 CPU 和 GPU 版本,其他框架还有 TensorFlow、Caffe 等。PyTorch 是由 Facebook 人工智能研究院(FAIR)基于 Torch 推出的,它是一个基于 Python 的可续计算包,提供两个高级功能:1、具有强大的 GPU 加速的张量计算(如 NumPy);2、构建深度神经网络时的自动微分机制。
  • YOLOv5是一种单阶段目标检测算法,该算法在YOLOv4的基础上添加了一些新的改进思路,使其速度与精度都得到了极大的性能提升。它是一个在COCO数据集上预训练的物体检测架构和模型系列,代表了Ultralytics对未来视觉AI方法的开源研究,其中包含了经过数千小时的研究和开发而形成的经验教训和最佳实践。
  • Labelme是一款图像标注工具,由麻省理工(MIT)的计算机科学和人工智能实验室(CSAIL)研发。它是用Python和PyQT编写的,开源且免费。Labelme支持Windows、Linux和Mac等操作系统。
  • 这款工具提供了直观的图形界面,允许用户在图像上标注多种类型的目标,例如矩形框、多边形、线条等,甚至包括更复杂的形状。标注结果以JSON格式保存,便于后续处理和分析。这些标注信息可以用于目标检测、图像分割、图像分类等任务。
  • 总的来说,Labelme是一款强大且易用的图像标注工具,可以满足不同的图像处理需求。
  • Labelme标注json文件是一种用于存储标注信息的文件格式,它包含了以下几个主要的字段:
    • version: Labelme的版本号,例如"4.5.6"。
    • flags: 一些全局的标志,例如是否是分割任务,是否有多边形,等等。
    • shapes: 一个列表,每个元素是一个字典,表示一个标注对象。每个字典包含了以下几个字段:
      • label: 标注对象的类别名称,例如"dog"。
      • points: 一个列表,每个元素是一个坐标对,表示标注对象的边界点,例如[[10, 20], [30, 40]]。
      • group_id: 标注对象的分组编号,用于表示属于同一组的对象,例如1。
      • shape_type: 标注对象的形状类型,例如"polygon",“rectangle”,“circle”,等等。
      • flags: 一些针对该标注对象的标志,例如是否是难例,是否被遮挡,等等。
    • lineColor: 标注对象的边界线颜色,例如[0, 255, 0, 128]。
    • fillColor: 标注对象的填充颜色,例如[255, 0, 0, 128]。
    • imagePath: 图像文件的相对路径,例如"img_001.jpg"。
    • imageData: 图像文件的二进制数据,经过base64编码后的字符串,例如"iVBORw0KGgoAAAANSUhEUgAA…"。
    • imageHeight: 图像的高度,例如600。
    • imageWidth: 图像的宽度,例如800。

以下是一个Labelme标注json文件的示例:

{"version": "4.5.6","flags": {},"shapes": [{"label": "dog","points": [[121.0,233.0],[223.0,232.0],[246.0,334.0],[121.0,337.0]],"group_id": null,"shape_type": "polygon","flags": {}}],"lineColor": [0,255,0,128],"fillColor": [255,0,0,128],"imagePath": "img_001.jpg","imageData": "iVBORw0KGgoAAAANSUhEUgAA...","imageHeight": 600,"imageWidth": 800
}

实验环境

  • Python 3.x (面向对象的高级语言)

Json标注文件的增、删、改、查

  • 背景:将标注好的Labelme的Json文件进行增、删、改、查。
  • 目录结构示例
    在这里插入图片描述
  • jsons:原始Labelme标注文件所在的文件夹。
  • output_jsons:修改后的Labelme标注文件所在的文件夹。

{"version": "4.5.7","flags": {},"shapes": [{"label": "22","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.9600000000000009,20.06000000000001],[442.19008,399.21015]]}],"imagePath": "000000000034.jpg","imageData": null,"imageHeight": 425,"imageWidth": 640
}

代码实现

def add_info_in_json(in_json_path,out_json_path):'''添加信息到json文件'''shapes_dict = {'label': '0', 'points': [[20,50],[30,70]], # [[x1,y1],[x2,y2]]'group_id': None, 'shape_type': 'rectangle', 'flags': {}}with open(in_json_path,'r') as f:json_data = json.load(f)# print(json_data)json_data['shapes'].append(shapes_dict)# 创建一个写文件with open(out_json_path, "w", encoding='utf-8') as f:# 将修改后的数据写入文件f.write(json.dumps(json_data))

输出结果

{"version": "4.5.7","flags": {},"shapes": [{"label": "22","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.9600000000000009,20.06000000000001],[442.19008,399.21015]]},{"label": "0","points": [[20,50],[30,70]],"group_id": null,"shape_type": "rectangle","flags": {}}],"imagePath": "000000000034.jpg","imageData": null,"imageHeight": 425,"imageWidth": 640
}

{"version": "4.5.7","flags": {},"shapes": [{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[1.0799999999999699,187.69008],[612.66976,473.53008]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[311.73024,4.310160000000001],[631.01024,232.99032]]},{"label": "50","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[249.60032000000007,229.27032],[565.84032,474.35016]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.0003200000000092018,13.510080000000002],[434.48032,388.63007999999996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[376.2,40.35996],[451.75007999999997,86.88996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[465.7797119999999,38.969952],[523.849728,85.63996800000001]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[385.70016000000004,73.65983999999999],[469.72,144.16992]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[364.04959999999994,2.4900960000000016],[458.80992,73.559856]]}],"imagePath": "000000000009.jpg","imageData": null,"imageHeight": 480,"imageWidth": 640
}

代码实现

def del_info_in_json(in_json_path,out_json_path):'''删除json文件的信息'''with open(in_json_path,'r') as f:json_data = json.load(f)# print(json_data)# 以删除指定label为例,比如删除掉'label' == "49"的标注信息json_data_shape =  copy.deepcopy(json_data['shapes'])for i in json_data_shape:if i['label'] == "49":json_data['shapes'].remove(i)# 创建一个写文件with open(out_json_path, "w", encoding='utf-8') as f:# 将修改后的数据写入文件f.write(json.dumps(json_data))

输出结果

{"version": "4.5.7","flags": {},"shapes": [{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[1.0799999999999699,187.69008],[612.66976,473.53008]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[311.73024,4.310160000000001],[631.01024,232.99032]]},{"label": "50","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[249.60032000000007,229.27032],[565.84032,474.35016]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.0003200000000092018,13.510080000000002],[434.48032,388.63007999999996]]}],"imagePath": "000000000009.jpg","imageData": null,"imageHeight": 480,"imageWidth": 640
}

{"version": "4.5.7","flags": {},"shapes": [{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[1.0799999999999699,187.69008],[612.66976,473.53008]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[311.73024,4.310160000000001],[631.01024,232.99032]]},{"label": "50","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[249.60032000000007,229.27032],[565.84032,474.35016]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.0003200000000092018,13.510080000000002],[434.48032,388.63007999999996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[376.2,40.35996],[451.75007999999997,86.88996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[465.7797119999999,38.969952],[523.849728,85.63996800000001]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[385.70016000000004,73.65983999999999],[469.72,144.16992]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[364.04959999999994,2.4900960000000016],[458.80992,73.559856]]}],"imagePath": "000000000009.jpg","imageData": null,"imageHeight": 480,"imageWidth": 640
}

代码实现

def alter_info_in_json(in_json_path,out_json_path):'''修改json文件的信息'''with open(in_json_path,'r') as f:json_data = json.load(f)# print(json_data)# 以修改label为例,比如将'label' == "49"改为 'label' == "orange"json_data_shape =  copy.deepcopy(json_data['shapes'])for i in json_data_shape:if i['label'] == "49":i['label'] = "orange"json_data['shapes'] = json_data_shape# 创建一个写文件with open(out_json_path, "w", encoding='utf-8') as f:# 将修改后的数据写入文件f.write(json.dumps(json_data))

输出结果

{"version": "4.5.7","flags": {},"shapes": [{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[1.0799999999999699,187.69008],[612.66976,473.53008]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[311.73024,4.310160000000001],[631.01024,232.99032]]},{"label": "50","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[249.60032000000007,229.27032],[565.84032,474.35016]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.0003200000000092018,13.510080000000002],[434.48032,388.63007999999996]]},{"label": "orange","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[376.2,40.35996],[451.75007999999997,86.88996]]},{"label": "orange","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[465.7797119999999,38.969952],[523.849728,85.63996800000001]]},{"label": "orange","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[385.70016000000004,73.65983999999999],[469.72,144.16992]]},{"label": "orange","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[364.04959999999994,2.4900960000000016],[458.80992,73.559856]]}],"imagePath": "000000000009.jpg","imageData": null,"imageHeight": 480,"imageWidth": 640
}

{"version": "4.5.7","flags": {},"shapes": [{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[1.0799999999999699,187.69008],[612.66976,473.53008]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[311.73024,4.310160000000001],[631.01024,232.99032]]},{"label": "50","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[249.60032000000007,229.27032],[565.84032,474.35016]]},{"label": "45","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[0.0003200000000092018,13.510080000000002],[434.48032,388.63007999999996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[376.2,40.35996],[451.75007999999997,86.88996]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[465.7797119999999,38.969952],[523.849728,85.63996800000001]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[385.70016000000004,73.65983999999999],[469.72,144.16992]]},{"label": "49","score": 0.99,"group_id": null,"shape_type": "rectangle","flags": {},"points": [[364.04959999999994,2.4900960000000016],[458.80992,73.559856]]}],"imagePath": "000000000009.jpg","imageData": null,"imageHeight": 480,"imageWidth": 640
}

代码实现

def query_info_in_json(in_json_path,out_json_path):'''查询json文件的信息'''with open(in_json_path,'r') as f:json_data = json.load(f)# print(json_data)# 以查询label信息为例,比如输出'label' == "49"的标注信息json_data_shape =  copy.deepcopy(json_data['shapes'])for i in json_data_shape:if i['label'] == "49":print(i)

输出结果

在这里插入图片描述

  • 由于本人水平有限,难免出现错漏,敬请批评改正。
  • 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏或我的个人主页查看
  • YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
  • 基于DETR的人脸伪装检测
  • YOLOv7训练自己的数据集(口罩检测)
  • YOLOv8训练自己的数据集(足球检测)
  • YOLOv5:TensorRT加速YOLOv5模型推理
  • YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
  • 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
  • YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
  • YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
  • Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
  • YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
  • 使用Kaggle GPU资源免费体验Stable Diffusion开源项目

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/231308.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数值计算方法(黄明游)】常微分方程初值问题的数值积分法:欧拉方法(向后Euler)【理论到程序】

文章目录 一、数值积分法1. 一般步骤2. 数值方法 二、欧拉方法(Euler Method)1. 向前欧拉法(前向欧拉法)2. 向后欧拉法(后向欧拉法)a. 基本理论b. 算法实现 常微分方程初值问题的数值积分法是一种通过数值方…

uniapp打包ios有时间 uniapp打包次数

我们经常用的解决方案有,分包,将图片上传到服务器上,减少插件引入。但是还有一个方案好多刚入门uniapp的人都给忽略了,就是在源码视图中配置,开启分包优化。 1.分包 目前微信小程序可以分8个包,每个包的最大存储是2M,也就是说你文件总体的大小不能超过16M,每个包的大…

智能优化算法应用:基于闪电搜索算法无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用:基于闪电搜索算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用:基于闪电搜索算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.闪电搜索算法4.实验参数设定5.算法结果6.参考…

Unity中Shader变体优化

文章目录 前言一、在Unity中查看变体个数,以及有哪些变体二、若使用预定义的变体太多,我们只使用其中的几个变体,我们该怎么做优化一:可以直接定义需要的那个变体优化二:使用 skip_variants 剔除不需要的变体 三、变体…

创建Asp.net MVC项目Ajax实现视图页面数据与后端Json传值显示

简述回顾 继上篇文章创建的mvc传值这里说明一下Json传值。在mvc框架中,不可避免地会遇到前台传值到后台,前台接收后台的值的情况(前台指view,后台指controller),有时只需要从控制器中返回一个处理的结果&a…

开源英伟达 Vulkan 驱动程序 NVK 达到 Vulkan 1.0 标准

导读Collabora今天宣布,其开源NVK图形驱动程序Mesa现已正式符合英伟达图灵硬件上的Vulkan 1.0图形应用程序接口。 “这是任何Nouveau驱动程序首次在任何API上获得Khronos一致性徽章”。Collabora表示,NVK获得Vulkan官方认证是Nouveau图形驱动程序首次在…

人工智能对人脑的探索研究!物理限制推动类脑人工智能的发展

原创 | 文 BFT机器人 在一项开创性的研究中,剑桥科学家采用了一种新颖的人工智能方法,展示了物理约束如何深刻影响了人工智能系统的发展。 这项研究会让人想起人脑的发育和能力限制,为复杂神经系统的进化提供了新的见解。通过整合这些限制&a…

leetcode刷题详解十二

回文子串 首先回文子串问题涉及到的都是单个字符串,所以如果是单个字符串用动态规划的基本都是二维的,i-j 其次,回文字符串,都是从后往前遍历的,这个要记住。因为dp的状态转移方程特性决定的 647. 回文子串 还是用…

sizeof 和 strlen的对比及笔试题目

目录 题目1: 题目2: 题目3: 题目4: 题目5: 题目6: 二维数组题(重点): 上述题目总结: 若想了解其他的字符函数和字符串函数请移步:深入理解…

金色传说:SAP-BC-SM36/SM37后台执行报表查询结果导出时分页和标题多行问题解决

文章目录 前言一、后台执行的启动方式?二、使用步骤1.点击程序-后台执行2.结果查看和导出三、结果显示和导出问题1.结果显示不全问题解决2.结果如何导出到Excel中3.==导出的文件有分页和标题和结果都是多行显示问题解决==总结前言 例如:随着项目数据量增长,很多报表的查询时…

Django连接数据库

连接数据库 接下来讲的几个步骤不需要区分先后,但都得进行操作 settings.py的操作 #!settings.py ....别的代码DATABASES {default: { ENGINE: django.db.backends.mysql, NAME: day1121, # 数据库名字,要先创建好 USER: root, PASSWORD: 123456, HO…

HarmonyOS——UI开展前的阶段总结

当足够的了解了HarmonyOS的相关特性之后,再去介入UI,你会发现无比的轻松,特别当你有着其他的声明式UI开发的经验时,对于HarmonyOS的UI,大致一扫,也就会了。 如何把UI阐述的简单易懂,又能方便大…