对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。

简介: 对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。

在制作做遥感图像物体检测数据集的时候,遥感图像的物体都是平面的,有角度的问题,

可以对被检测物体实现不同角度的旋转,丰富数据集同时减少标注的工作量。

tt.png


比如上图中的飞机,机头的朝向是斜向下的,现实中的飞机可能有各种的朝向,如果不做旋转,就会降低模型的检测能力。下图是旋转90度的效果。


tt.png

需要安装的包:


labelme


scipy1.0.0版本


pyqt5



旋转最大的难点在于旋转后,需要对标注的点重新计算,保证标注的坐标不出现错乱。


旋转90度后,坐标转化:


 points=shapelabel['points']#获取初始的坐标。

 newPoints = [[float(points[0][1]), w-float(points[1][0])],

                [float(points[1][1]), w-float(points[0][0])]]#旋转90度,重新对应坐标。w表示原始图像的宽度。

选旋转180度后,坐标转化:


points = shapelabel['points']

newPoints = [[w-float(points[1][0]), h - float(points[1][1])],

                    [w-float(points[0][0]), h - float(points[0][1])]] #旋转180度,重新对应坐标。h表示原始图像的高度。

旋转270度,坐标转化:


points = shapelabel['points']


newPoints = [[h - float(points[1][1]), float(points[0][0])],

                    [h - float(points[0][1]),  float(points[1][0])]]


完整代码如下:


#scipy的版本为1.0.0


import scipy


from scipy import misc


import os


import glob


import PIL.Image


from labelme.logger import logger


from labelme import PY2


from labelme import QT4


import io


import json


import os.path as osp


import PIL.Image


from scipy import ndimage


import base64


from labelme import utils


def load_image_file(filename):

   try:

       image_pil = PIL.Image.open(filename)

   except IOError:

       logger.error('Failed opening image file: {}'.format(filename))

       return


   # apply orientation to image according to exif

   image_pil = utils.apply_exif_orientation(image_pil)


   with io.BytesIO() as f:

       ext = osp.splitext(filename)[1].lower()

       if PY2 and QT4:

           format = 'PNG'

       elif ext in ['.jpg', '.jpeg']:

           format = 'JPEG'

       else:

           format = 'PNG'

       image_pil.save(f, format=format)

       f.seek(0)

       return f.read()


def dict_json(flags,imageData,shapes,imagePath,fillColor=None,lineColor=None,imageHeight=100,imageWidth=100):

   '''

   :param imageData: str

   :param shapes: list

   :param imagePath: str

   :param fillColor: list

   :param lineColor: list

   :return: dict

   '''

   return {"version":"3.16.4","flags":flags,"shapes":shapes,'lineColor':lineColor,"fillColor":fillColor,'imagePath':imagePath.split('\\')[1],"imageData":imageData,'imageHeight':imageHeight,'imageWidth':imageWidth}



def get_image_paths(folder):

   return glob.glob(os.path.join(folder, '*.jpg'))


def create_read_img(filename):

   data = json.load(open(filename.split('.')[0]+'.json'))

   shapes = data["shapes"]

   version = data["version"]

   flags = data["flags"]

   lineColor = data["lineColor"]

   fillColor = data['fillColor']

   newshapes = []

   im = misc.imread(filename)

   h,w,d=im.shape

   img_rote_90 = ndimage.rotate(im, 90)

   img_path_90=filename[:-4]+'_90.jpg'

   scipy.misc.imsave(img_path_90,img_rote_90)

   imageData_90 = load_image_file(img_path_90)

   imageData_90 = base64.b64encode(imageData_90).decode('utf-8')

   imageHeight =w

   imageWidth = h

   for shapelabel in shapes:

       newLabel=shapelabel['label']

       newline_color=shapelabel['line_color']

       newfill_color=shapelabel['fill_color']

       points=shapelabel['points']

       newPoints = [[float(points[0][1]), w-float(points[1][0])],

                [float(points[1][1]), w-float(points[0][0])]]

       newshape_type=shapelabel['shape_type']

       newflags=shapelabel['flags']

       newshapes.append({'label':newLabel,'line_color':newline_color,'fill_color':newfill_color,'points':newPoints,'shape_type':newshape_type,'flags':newflags})

   data_90 = dict_json(flags, imageData_90, newshapes, img_path_90, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_90[:-4] + '.json'

   json.dump(data_90, open(json_file, 'w'))

   img_rote_180 = ndimage.rotate(im, 180)

   img_path_180=filename[:-4]+'_180.jpg'

   scipy.misc.imsave(img_path_180,img_rote_180)

   imageData_180 = load_image_file(img_path_180)

   imageData_180 = base64.b64encode(imageData_180).decode('utf-8')

   imageHeight = h

   imageWidth = w

   newshapes = []

   for shapelabel in shapes:

       newLabel = shapelabel['label']

       newline_color = shapelabel['line_color']

       newfill_color = shapelabel['fill_color']

       points = shapelabel['points']

       newPoints = [[w-float(points[1][0]), h - float(points[1][1])],

                    [w-float(points[0][0]), h - float(points[0][1])]]

       newshape_type = shapelabel['shape_type']

       newflags = shapelabel['flags']

       newshapes.append(

           {'label': newLabel, 'line_color': newline_color, 'fill_color': newfill_color, 'points': newPoints,

            'shape_type': newshape_type, 'flags': newflags})

   data_180 = dict_json(flags, imageData_180, newshapes, img_path_180, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_180[:-4] + '.json'

   json.dump(data_180, open(json_file, 'w'))

   img_rote_270 = ndimage.rotate(im, 270)

   img_path_270=filename[:-4]+'_270.jpg'

   scipy.misc.imsave(img_path_270,img_rote_270)


   imageData_270 = load_image_file(img_path_270)

   imageData_270 = base64.b64encode(imageData_270).decode('utf-8')

   imageHeight = w

   imageWidth = h

   newshapes = []

   for shapelabel in shapes:

       newLabel = shapelabel['label']

       newline_color = shapelabel['line_color']

       newfill_color = shapelabel['fill_color']

       points = shapelabel['points']

       newPoints = [[h - float(points[1][1]), float(points[0][0])],

                    [h - float(points[0][1]),  float(points[1][0])]]

       newshape_type = shapelabel['shape_type']

       newflags = shapelabel['flags']

       newshapes.append(

           {'label': newLabel, 'line_color': newline_color, 'fill_color': newfill_color, 'points': newPoints,

            'shape_type': newshape_type, 'flags': newflags})

   data_270 = dict_json(flags, imageData_270, newshapes, img_path_270, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_270[:-4] + '.json'

   json.dump(data_270, open(json_file, 'w'))

   print(filename)

img_path = 'USA'  #这个路径是所有图片在的位置

imgs = get_image_paths(img_path)

print (imgs)

for i in imgs:

   create_read_img(i)



目录
相关文章
|
XML 数据格式 Python
旋转标注工具roLabelImg使用教程
旋转标注工具roLabelImg使用教程
旋转标注工具roLabelImg使用教程
|
JSON 编解码 人工智能
labelme 安装使用及常见问题汇总
labelme批量使用、安装、问题解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0 module 'labelme.utils' has no attribute 'draw_label' Polygon must have points more than 2 ModuleNotFoundError: No module named 'skimage' Inconsistent use of tabs and s
labelme 安装使用及常见问题汇总
|
机器学习/深度学习 人工智能 算法
Python+YOLO v8 实战:手把手教你打造专属 AI 视觉目标检测模型
本文介绍了如何使用 Python 和 YOLO v8 开发专属的 AI 视觉目标检测模型。首先讲解了 YOLO 的基本概念及其高效精准的特点,接着详细说明了环境搭建步骤,包括安装 Python、PyCharm 和 Ultralytics 库。随后引导读者加载预训练模型进行图片验证,并准备数据集以训练自定义模型。最后,展示了如何验证训练好的模型并提供示例代码。通过本文,你将学会从零开始打造自己的目标检测系统,满足实际场景需求。
15033 1
Python+YOLO v8 实战:手把手教你打造专属 AI 视觉目标检测模型
|
机器学习/深度学习 编解码 监控
目标检测实战(六): 使用YOLOv8完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)
这篇文章详细介绍了如何使用YOLOv8进行目标检测任务,包括环境搭建、数据准备、模型训练、验证测试以及模型转换等完整流程。
28599 59
目标检测实战(六): 使用YOLOv8完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)
|
XML 机器学习/深度学习 数据格式
YOLOv8训练自己的数据集+常用传参说明
YOLOv8训练自己的数据集+常用传参说明
27311 3
|
机器学习/深度学习 编解码 计算机视觉
YOLOv11改进策略【卷积层】| ECCV-2024 小波卷积WTConv 增大感受野,降低参数量计算量,独家创新助力涨点
YOLOv11改进策略【卷积层】| ECCV-2024 小波卷积WTConv 增大感受野,降低参数量计算量,独家创新助力涨点
1626 0
YOLOv11改进策略【卷积层】| ECCV-2024 小波卷积WTConv 增大感受野,降低参数量计算量,独家创新助力涨点
|
XML JSON 数据可视化
数据集学习笔记(六):目标检测和图像分割标注软件介绍和使用,并转换成YOLO系列可使用的数据集格式
本文介绍了labelImg和labelme两款图像标注工具的安装、使用、数据转换和验证方法,适用于目标检测和图像分割任务,支持YOLO等数据集格式。
6450 2
数据集学习笔记(六):目标检测和图像分割标注软件介绍和使用,并转换成YOLO系列可使用的数据集格式
|
机器学习/深度学习 JSON 算法
实例分割笔记(一): 使用YOLOv5-Seg对图像进行分割检测完整版(从自定义数据集到测试验证的完整流程)
本文详细介绍了使用YOLOv5-Seg模型进行图像分割的完整流程,包括图像分割的基础知识、YOLOv5-Seg模型的特点、环境搭建、数据集准备、模型训练、验证、测试以及评价指标。通过实例代码,指导读者从自定义数据集开始,直至模型的测试验证,适合深度学习领域的研究者和开发者参考。
7284 3
实例分割笔记(一): 使用YOLOv5-Seg对图像进行分割检测完整版(从自定义数据集到测试验证的完整流程)
|
XML JSON 数据可视化
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)
本文详细介绍了不同数据集格式之间的转换方法,包括YOLO、VOC、COCO、JSON、TXT和PNG等格式,以及如何可视化验证数据集。
5205 1
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)
|
机器学习/深度学习 存储 并行计算
深度学习之声纹识别
基于深度学习的声纹识别(Speaker Recognition)是一种通过分析和识别人的声音特征来确认身份的技术。
3449 2