批量将图片分别翻转90、180、270度,并将对应的框标注的json文件也进行相应调整,做到数据增强的效果

本文主要是介绍批量将图片分别翻转90、180、270度,并将对应的框标注的json文件也进行相应调整,做到数据增强的效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#------------------------------------矩形标注增强---------------------------------------
from PIL import Image
import os
import jsondef rotate_images_and_jsons(input_folder):output_folder = os.path.join(input_folder, "rotated_images")os.makedirs(output_folder, exist_ok=True)for filename in os.listdir(input_folder):if filename.endswith(".jpg"):image_path = os.path.join(input_folder, filename)json_path = os.path.join(input_folder, filename.replace(".jpg", ".json"))# Load JSON datawith open(json_path, 'r', encoding='utf-8') as json_file:json_data = json.load(json_file)image = Image.open(image_path)# Rotate the image by 90, 180, and 270 degreesfor angle in [90, 180, 270]:rotated_image = image.rotate(angle, expand=True)# Save rotated images with a default quality valuerotated_image_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}_{angle}.jpg")rotated_image.save(rotated_image_path, quality=160)# Update JSON data for rotated imagerotated_json_data = update_json_for_rotation(json_data, image.size, angle, os.path.basename(rotated_image_path))rotated_json_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}_{angle}.json")with open(rotated_json_path, 'w', encoding='utf-8') as rotated_json_file:json.dump(rotated_json_data, rotated_json_file, indent=4, ensure_ascii=False)def update_json_for_rotation(json_data, image_size, angle, image_path):width, height = image_sizerotated_json_data = []for shape in json_data['shapes']:points = shape['points']rotated_points = []# 提取坐标信息point1_x, point1_y = points[0]point2_x, point2_y = points[1]if angle == 90:rotated_point1 = [point1_y, width - point1_x]rotated_point2 = [point2_y, width - point2_x]elif angle == 180:rotated_point1 = [width - point1_x, height - point1_y]rotated_point2 = [width - point2_x, height - point2_y]elif angle == 270:rotated_point1 = [height - point1_y, point1_x]rotated_point2 = [height - point2_y, point2_x]else:rotated_point = pointrotated_points.append(rotated_point1)rotated_points.append(rotated_point2)rotated_shape = {'label': shape['label'],'points': rotated_points,'group_id': shape['group_id'],'shape_type': shape['shape_type'],'flags': shape['flags']}rotated_json_data.append(rotated_shape)return {'version': json_data['version'], 'flags': json_data['flags'], 'shapes': rotated_json_data, 'imagePath': image_path, 'imageData': json_data['imageData'], 'imageHeight': json_data['imageHeight'], 'imageWidth': json_data['imageWidth']}# Replace 'input_folder' with your folder containing images and JSON files
rotate_images_and_jsons('./rectangles')

这篇关于批量将图片分别翻转90、180、270度,并将对应的框标注的json文件也进行相应调整,做到数据增强的效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476442

相关文章

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I