目标检测任务数据集的数据增强中,图像垂直翻转和xml标注文件坐标调整

需求:

数据集的数据增强中,有时需要用到图像垂直翻转的操作,图像垂直翻转后,对应的xml标注文件也需要做坐标的调整。
解决方法:
使用python+opencv+import xml.etree.ElementTree对图像垂直翻转和xml标注文件坐标调整。代码如下:

python 复制代码
import cv2
import os
import glob
import xml.etree.ElementTree as et


def flip_images(source_dir):
    images_list = glob.glob(os.path.join(source_dir, "*.jpg"))
    index = 0
    for image_path in images_list:
        image = cv2.imread(image_path)
        flip_image = cv2.flip(image,0)
        cv2.imwrite(image_path.replace(".jpg", "_flip.jpg"), flip_image)
        tree_ = et.ElementTree()
        tree_.parse(image_path.replace(".jpg", ".xml"))

        root = et.Element("annotation")
        folder = et.SubElement(root, "folder")
        folder.text = "images"
        filename = et.SubElement(root, "filename")
        filename.text = tree_.find(".//filename").text.replace(".jpg", "_flip.jpg")
        path = et.SubElement(root, "path")
        path.text = "/home/mapgoo/test"
        source = et.SubElement(root, "source")
        database = et.SubElement(source, "database")
        database.text = "Unknown"
        size = et.SubElement(root, "size")
        width = et.SubElement(size, "width")
        width.text = tree_.find(".//width").text
        height = et.SubElement(size, "height")
        height.text = tree_.find(".//height").text
        depth = et.SubElement(size, "depth")
        depth.text = "3"
        segmented = et.SubElement(root, "segmented")
        segmented.text = "0"

        for bndbox in tree_.findall(".//object"):
            xmin = bndbox.find(".//xmin")
            ymin = bndbox.find(".//ymin")
            xmax = bndbox.find(".//xmax")
            ymax = bndbox.find(".//ymax")
            xmin_text = xmin.text
            ymin_text = ymin.text
            xmax_text = xmax.text
            ymax_text = ymax.text

            object_ = et.SubElement(root, "object")
            name = et.SubElement(object_, "name")
            name.text = bndbox.find("name").text
            pose = et.SubElement(object_, "pose")
            pose.text = "Unspecified"
            truncated = et.SubElement(object_, "truncated")
            truncated.text = "0"
            difficult = et.SubElement(object_, "difficult")
            difficult.text = "0"
            bndbox = et.SubElement(object_, "bndbox")
            xmin = et.SubElement(bndbox, "xmin")
            xmin.text = xmin_text
            ymin = et.SubElement(bndbox, "ymin")
            ymin.text = str(image.shape[0] - int(ymax_text))
            xmax = et.SubElement(bndbox, "xmax")
            xmax.text = xmax_text
            ymax = et.SubElement(bndbox, "ymax")
            ymax.text = str(image.shape[0] - int(ymin_text))

        tree = et.ElementTree(root)
        tree.write(image_path.replace(".jpg", "_flip.xml"), encoding="utf-8")
        print(image_path, index)
        index += 1


if __name__ == '__main__':
    source_dir = "/home/Desktop/test"
    flip_images(source_dir)

使用以上代码需要修改原图像和标注文件所在文件夹路径(source_dir)。亲测可用。

相关推荐
KuaFuAI6 分钟前
微软推出的AI无代码编程微应用平台GitHub Spark和国产AI原生无代码工具CodeFlying比到底咋样?
人工智能·github·aigc·ai编程·codeflying·github spark·自然语言开发软件
Make_magic15 分钟前
Git学习教程(更新中)
大数据·人工智能·git·elasticsearch·计算机视觉
shelly聊AI19 分钟前
语音识别原理:AI 是如何听懂人类声音的
人工智能·语音识别
源于花海22 分钟前
论文学习(四) | 基于数据驱动的锂离子电池健康状态估计和剩余使用寿命预测
论文阅读·人工智能·学习·论文笔记
雷龙发展:Leah23 分钟前
离线语音识别自定义功能怎么用?
人工智能·音频·语音识别·信号处理·模块测试
4v1d27 分钟前
边缘计算的学习
人工智能·学习·边缘计算
风之馨技术录31 分钟前
智谱AI清影升级:引领AI视频进入音效新时代
人工智能·音视频
sniper_fandc40 分钟前
深度学习基础—Seq2Seq模型
人工智能·深度学习
goomind43 分钟前
深度学习模型评价指标介绍
人工智能·python·深度学习·计算机视觉
youcans_44 分钟前
【微软报告:多模态基础模型】(2)视觉理解
人工智能·计算机视觉·大语言模型·多模态·视觉理解