对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使用教程
|
机器学习/深度学习 并行计算 算法
YOLOv8改进 | 卷积篇 |手把手教你添加动态蛇形卷积(Dynamic Snake Convolution)
YOLOv8改进 | 卷积篇 |手把手教你添加动态蛇形卷积(Dynamic Snake Convolution)
1518 0
|
JSON 人工智能 数据格式
AI计算机视觉笔记二十六:YOLOV8自训练关键点检测
本文档详细记录了使用YOLOv8训练关键点检测模型的过程。首先通过清华源安装YOLOv8,并验证安装。接着通过示例权重文件与测试图片`bus.jpg`演示预测流程。为准备训练数据,文档介绍了如何使用`labelme`标注工具进行关键点标注,并提供了一个Python脚本`labelme2yolo.py`将标注结果从JSON格式转换为YOLO所需的TXT格式。随后,通过Jupyter Notebook可视化标注结果确保准确性。最后,文档展示了如何组织数据集目录结构,并提供了训练与测试代码示例,包括配置文件`smoke.yaml`及训练脚本`train.py`,帮助读者完成自定义模型的训练与评估。
3832 2
|
机器学习/深度学习 数据可视化 测试技术
YOLO11实战:新颖的多尺度卷积注意力(MSCA)加在网络不同位置的涨点情况 | 创新点如何在自己数据集上高效涨点,解决不涨点掉点等问题
本文探讨了创新点在自定义数据集上表现不稳定的问题,分析了不同数据集和网络位置对创新效果的影响。通过在YOLO11的不同位置引入MSCAAttention模块,展示了三种不同的改进方案及其效果。实验结果显示,改进方案在mAP50指标上分别提升了至0.788、0.792和0.775。建议多尝试不同配置,找到最适合特定数据集的解决方案。
3209 0
|
10月前
|
编解码 算法 计算机视觉
YOLOv11改进策略【Head】| 增加针对 大目标 的检测层 (四个检测头)
YOLOv11改进策略【Head】| 增加针对 大目标 的检测层 (四个检测头)
1664 7
|
10月前
|
机器学习/深度学习 编解码 计算机视觉
YOLOv11改进策略【Backbone/主干网络】| 替换骨干网络为:Swin Transformer,提高多尺度特征提取能力
YOLOv11改进策略【Backbone/主干网络】| 替换骨干网络为:Swin Transformer,提高多尺度特征提取能力
726 0
YOLOv11改进策略【Backbone/主干网络】| 替换骨干网络为:Swin Transformer,提高多尺度特征提取能力
|
10月前
|
机器学习/深度学习 计算机视觉 网络架构
YOLOv11改进策略【Backbone/主干网络】| CVPR 2024替换骨干网络为 UniRepLKNet,解决大核 ConvNets 难题
YOLOv11改进策略【Backbone/主干网络】| CVPR 2024替换骨干网络为 UniRepLKNet,解决大核 ConvNets 难题
939 0
YOLOv11改进策略【Backbone/主干网络】| CVPR 2024替换骨干网络为 UniRepLKNet,解决大核 ConvNets 难题
|
机器学习/深度学习 编解码 Java
YOLO11创新改进系列:卷积,主干 注意力,C3k2融合,检测头等创新机制(已更新100+)
《YOLO11目标检测创新改进与实战案例》专栏已更新100+篇文章,涵盖注意力机制、卷积优化、检测头创新、损失与IOU优化、轻量级网络设计等多方面内容。每周更新3-10篇,提供详细代码和实战案例,帮助您掌握最新研究和实用技巧。[专栏链接](https://blog.csdn.net/shangyanaf/category_12810477.html)
YOLO11创新改进系列:卷积,主干 注意力,C3k2融合,检测头等创新机制(已更新100+)
|
XML 数据格式 Python
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
该文章提供了一个Python脚本,用于将VOC格式的XML标签文件转换为YOLO训练所需的TXT格式,包括修改数据集类别、输入图像与标注文件夹地址、转换过程和结果展示。
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
|
机器学习/深度学习 人工智能 文字识别
ultralytics YOLO11 全新发布!(原理介绍+代码详见+结构框图)
本文详细介绍YOLO11,包括其全新特性、代码实现及结构框图,并提供如何使用NEU-DET数据集进行训练的指南。YOLO11在前代基础上引入了新功能和改进,如C3k2、C2PSA模块和更轻量级的分类检测头,显著提升了模型的性能和灵活性。文中还对比了YOLO11与YOLOv8的区别,并展示了训练过程和结果的可视化
20271 0

热门文章

最新文章