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

相关文章

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统