转载:https://blog.csdn.net/qq_36848732/article/details/106464185
功能:labelImg标注过的图片水平翻转,对应的xml标注信息也同步水平翻转。
代码如下:

# -*- coding:utf-8 -*-

import cv2
import os
import sys
import re
import xml.etree.ElementTree as ET
from PIL import Image

imgreadpath = 'img/'     # 原始jpg存放的文件夹
imgwritepath = 'f_img/'  # 水平翻转后的jpg保存文件夹

xmlreadpath = 'xml/'     # 原始xml存放的文件夹
xmlwritepath = 'f_xml/'  # 水平翻转后的xml保存文件夹

# 图像水平翻转
def flitimg(imgname):
    image = cv2.imread(imgname)
    name = imgname.split('/')[-1].split('.')[-2]
    image_f = cv2.flip(image, 1)  # 1:水平翻转
    cv2.imwrite(imgwritepath + 'f' + name + '.jpg', image_f)

# xml同步水平翻转
def flitxml(xmlname):
    bwidth = ''
    bheight = ''
    bdepth = ''

    text = open(xmlname).read()
    f_test = open(xmlwritepath + 'f' + xmlname.split("/")[-1].split('.')[-2] + '.xml', 'w')

    text = re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+", u"", text)
    root = ET.fromstring(text)

    if root.findtext('folder') == ' ':
        print("no folder filename:", filename)
        print('A')
    else:
        folder = root.findtext('folder')

    if root.findtext('filename') == ' ':
        print("no filename filename:", filename)
        print('B')
    else:
        filename = root.findtext('filename')

    # if root.findall('size') == []:
    # 	print("no size filename:",filename)
    # 	print('C')
    # else:
    # 	size = root.findall('size')
    # 	print(size)
    # 	size = size[0]
    # 	bwidth = size.findtext('width')
    # 	bheight = size.findtext('height')
    # 	bdepth = size.findtext('depth')
    img = Image.open(imgwritepath + 'f' + xmlname.split("/")[-1].split('.')[-2] + '.jpg')
    bwidth, bheight = img.size
    bdepth = 3

    f_test.write('<annotation>\n')
    f_test.write('	<folder>' + folder + '</folder>\n')
    f_test.write('	<filename>' + filename + '</filename>\n')
    f_test.write('	<size>\n')
    f_test.write('		<width>' + str(bwidth) + '</width>\n')
    f_test.write('		<height>' + str(bheight) + '</height>\n')
    f_test.write('		<depth>' + str(bdepth) + '</depth>\n')
    f_test.write('	</size>\n')

    if root.findall('object') == []:
        print("no object filename:", filename)
    else:
        for object in root.findall('object'):
            label = object.findtext('name')
            x1 = object.findtext('bndbox/xmin')
            y1 = object.findtext('bndbox/ymin')
            x2 = object.findtext('bndbox/xmax')
            y2 = object.findtext('bndbox/ymax')

            f_test.write('	<object>\n')
            f_test.write('		<name>' + label + '</name>\n')
            f_test.write('		<bndbox>\n')
            f_test.write('			<xmin>' + str(int(bwidth) - int(x1) - (int(x2) - int(x1))) + '</xmin>\n')
            f_test.write('			<ymin>' + y1 + '</ymin>\n')
            f_test.write('			<xmax>' + str(int(bwidth) - int(x2) + (int(x2) - int(x1))) + '</xmax>\n')
            f_test.write('			<ymax>' + y2 + '</ymax>\n')
            f_test.write('		</bndbox>\n')
            f_test.write('	</object>\n')

    f_test.write('</annotation>\n')
    f_test.close()

if __name__ == '__main__':

    imgnames = os.listdir(imgreadpath)
    for imgname in imgnames:
        imgname = imgreadpath + imgname
        flitimg(imgname)

    xmlnames = os.listdir(xmlreadpath)
    for xmlname in xmlnames:
        xmlname = xmlreadpath + xmlname
        flitxml(xmlname)
GitHub 加速计划 / la / labelImg
22.31 K
6.24 K
下载
🎉 超级实用!LabelImg,图像标注神器,现在加入Label Studio社区,享受多模态数据标注新体验!🚀 简单易用,支持XML、YOLO和CreateML格式,适用于ImageNet等项目。不再单独维护,立即尝试Label Studio,安装一键到位,更灵活,功能更强大!👇 安装即刻开始:pip3 install labelImg,或访问<https://github.com/heartexlabs/label-studio> 获取源码构建。一起探索数据标注的新边界!👨‍💻👩‍💻【此简介由AI生成】
最近提交(Master分支:2 个月前 )
b33f965b Adds information about Label Studio community to welcome LabelImg users 2 年前
2d5537ba 2 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐