批量将图片分别翻转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

相关文章

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

pandas批量拆分与合并Excel文件的实现示例

《pandas批量拆分与合并Excel文件的实现示例》本文介绍了Pandas中基于整数位置的iloc和基于标签的loc方法进行数据索引和切片的操作,并将大Excel文件拆分合并,具有一定的参考价值,感... 目录一、Pandas 进行索引和切编程片的iloc、loc方法二、Pandas批量拆分与合并Exce

JAVA SpringBoot集成Jasypt进行加密、解密的详细过程

《JAVASpringBoot集成Jasypt进行加密、解密的详细过程》文章详细介绍了如何在SpringBoot项目中集成Jasypt进行加密和解密,包括Jasypt简介、如何添加依赖、配置加密密钥... 目录Java (SpringBoot) 集成 Jasypt 进行加密、解密 - 详细教程一、Jasyp

MySQL中的DELETE删除数据及注意事项

《MySQL中的DELETE删除数据及注意事项》MySQL的DELETE语句是数据库操作中不可或缺的一部分,通过合理使用索引、批量删除、避免全表删除、使用TRUNCATE、使用ORDERBY和LIMI... 目录1. 基本语法单表删除2. 高级用法使用子查询删除删除多表3. 性能优化策略使用索引批量删除避免

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

使用MyBatis TypeHandler实现数据加密与解密的具体方案

《使用MyBatisTypeHandler实现数据加密与解密的具体方案》在我们日常的开发工作中,经常会遇到一些敏感数据需要存储,比如用户的手机号、身份证号、银行卡号等,为了保障数据安全,我们通常会对... 目录1. 核心概念:什么是 TypeHandler?2. 实战场景3. 代码实现步骤步骤 1:定义 E

使用C#导出Excel数据并保存多种格式的完整示例

《使用C#导出Excel数据并保存多种格式的完整示例》在现代企业信息化管理中,Excel已经成为最常用的数据存储和分析工具,从员工信息表、销售数据报表到财务分析表,几乎所有部门都离不开Excel,本文... 目录引言1. 安装 Spire.XLS2. 创建工作簿和填充数据3. 保存为不同格式4. 效果展示5