Resize图片、分割图片、分割xml文件、统计xml文件里的某个标签的个数、图片重命名的代码

Resize_XmlPic.py

   

#-*-coding: UTF -8-*-
###############################
# 按照图片最短边缩放到512的比例对图片进行缩小或者放大
# 同时对XML进行更改
###############################
import cv2
import sys
import os
import xml.dom.minidom as minidom
reload(sys)
sys.setdefaultencoding( "utf-8" )

def resize_image(imageName, filepath,resultPath,TargetSize):
    pre_image=cv2.imread(filepath)
    pre_height = pre_image.shape[0]  #垂直像素
    print("11111111111")
    print(pre_height)
    pre_width = pre_image.shape[1]   #水平像素
    if (pre_width < pre_height):
        ratio =float(TargetSize)/ pre_width
        need_width=TargetSize
        need_height=pre_height*ratio
    else:
        ratio = float(TargetSize) / pre_height
        need_height=TargetSize
        need_width=pre_width*ratio
    image_resize=cv2.resize(pre_image,(int(need_width),int(need_height)),interpolation=cv2.INTER_AREA)  #(fx,fy)
    height=image_resize.shape[0]
    width=image_resize.shape[1]
    print width
    print height
    cv2.imwrite(resultPath  + imageName, image_resize)

def resize_xml(xmlname,xml_path,result_path,TargetSize):
    annotation = minidom.parse(xml_path)
    size = annotation.getElementsByTagName("size")
    width = size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue
    height = size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue
    width = int(str(width))
    height = int(str(height))

    # 宽长高短
    if(width > height):
        ratio = float(TargetSize) / height
        print ratio
        des_pic_width=int(ratio*width)
        des_pic_height=TargetSize
        print des_pic_width
        print des_pic_height
        size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue = unicode(str(des_pic_width), encoding='utf-8')
        size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue = unicode(str(des_pic_height), encoding='utf-8')
        bndbox = annotation.getElementsByTagName("bndbox")
        for one in bndbox:
            xmin = one.getElementsByTagName("xmin")[0].childNodes[0].nodeValue
            xmin = int(str(xmin))
            one.getElementsByTagName("xmin")[0].childNodes[0].nodeValue = unicode(str(int(ratio * xmin)),
                                                                                  encoding='utf-8')
            xmax = one.getElementsByTagName("xmax")[0].childNodes[0].nodeValue
            xmax = int(str(xmax))
            one.getElementsByTagName("xmax")[0].childNodes[0].nodeValue = unicode(str(int(ratio * xmax)),
                                                                                  encoding='utf-8')
            ymin = one.getElementsByTagName("ymin")[0].childNodes[0].nodeValue
            ymin = int(str(ymin))
            one.getElementsByTagName("ymin")[0].childNodes[0].nodeValue = unicode(str(int(ratio * ymin)),
                                                                                  encoding='utf-8')
            ymax = one.getElementsByTagName("ymax")[0].childNodes[0].nodeValue
            ymax = int(str(ymax))
            one.getElementsByTagName("ymax")[0].childNodes[0].nodeValue = unicode(str(int(ratio * ymax)),
                                                                                  encoding='utf-8')

        f = open(os.path.join(result_path, xmlname), 'w')
        annotation.writexml(f, encoding='utf-8')
        f.close()
    else:
        ratio = float(TargetSize) / width
        #print ratio
        des_pic_width=TargetSize
        des_pic_height=int(ratio*height)
        #print des_pic_width
        #print des_pic_height
        size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue = unicode(str(des_pic_width), encoding='utf-8')
        size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue = unicode(str(des_pic_height),
                                                                                    encoding='utf-8')
        bndbox = annotation.getElementsByTagName("bndbox")
        for one in bndbox:
            xmin = one.getElementsByTagName("xmin")[0].childNodes[0].nodeValue
            xmin = int(str(xmin))
            one.getElementsByTagName("xmin")[0].childNodes[0].nodeValue = unicode(str(int(ratio * xmin)),
                                                                                  encoding='utf-8')
            xmax = one.getElementsByTagName("xmax")[0].childNodes[0].nodeValue
            xmax = int(str(xmax))
            one.getElementsByTagName("xmax")[0].childNodes[0].nodeValue = unicode(str(int(ratio * xmax)),
                                                                                  encoding='utf-8')
            ymin = one.getElementsByTagName("ymin")[0].childNodes[0].nodeValue
            ymin = int(str(ymin))
            one.getElementsByTagName("ymin")[0].childNodes[0].nodeValue = unicode(str(int(ratio * ymin)),
                                                                                  encoding='utf-8')
            ymax = one.getElementsByTagName("ymax")[0].childNodes[0].nodeValue
            ymax = int(str(ymax))
            one.getElementsByTagName("ymax")[0].childNodes[0].nodeValue = unicode(str(int(ratio * ymax)),
                                                                                  encoding='utf-8')

        f = open(os.path.join(result_path, xmlname), 'w')
        annotation.writexml(f, encoding='utf-8')
        f.close()


if __name__ == "__main__":
    TargetSize=512
    pic_path='./Drone_make_picture'
    pic_out_path='./pic_out/'
    XML_path='./Drone_transform_xml/'
    XML_out='./xml_out/'

    print "pic started"
    for pic_file in os.listdir(pic_path):
        filename=pic_file
        file_path = os.path.join(pic_path, filename)#路径拼接
        resize_image(filename,file_path,pic_out_path,TargetSize)
    print "xml started"
    for xml_file in os.listdir(XML_path):
        xmlname = xml_file
        xml = os.path.join(XML_path, xmlname)
        resize_xml(xmlname, xml, XML_out, TargetSize)
    print("process finish\n")

 

split_image.py 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from PIL import Image
import os
def cut_image(image):
    width, height = image.size
    item_width = int(width / 4)
    item_height = int(height / 4)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0,4):
     for j in range(0,4):
         #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
         box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)
         box_list.append(box)
    image_list = [image.crop(box) for box in box_list]
    return image_list
#保存
def save_images(image_list,pic_file):
    index = 1
    for image in image_list:     
        image.save("./ceshi/" + str(index) + "_" + pic_file)      
        index += 1
if __name__ == '__main__':
    filepath = "./imagesave"
    #filepath = r"./DSC00001.jpg_2.jpg"
    for pic_file in os.listdir(filepath):
        print(pic_file)
        file_path = os.path.join(filepath, pic_file)
        image = Image.open(file_path)
        image_list = cut_image(image)
        save_images(image_list,pic_file)

 

Rename.py 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
class BatchRename(object):
    '''
    批量重命名文件夹中的图片文件
    '''
    def __init__(self):
        self.path = './INTER_cubic'  #表示需要命名处理的文件夹
        self.somefile = './somefile.txt'
    def rename(self):
        filelist = os.listdir(self.path) #获取文件路径
        total_num = len(filelist) #获取文件长度(个数)
        with open('somefile.txt', 'r') as f:
            content = f.read().splitlines()
            print(content)
        for i , item in enumerate(filelist):
            if item.endswith('.jpg'):  #初始的图片的格式为jpg格式的(或者源文件是png格式及其他格式,后面的转换格式就可以调整为自己需要的格式即可)
                src = os.path.join(self.path, item)
                print(src)
                dst = os.path.join(os.path.abspath(self.path), content[i] + '.jpg')#处理后的格式也为jpg格式的,当然这里可以改成png格式
                #print(os.path.abspath(self.path))
                #dst = os.path.join(os.path.abspath(self.path), '0000' + format(str(i), '0>3s') + '.jpg')    这种情况下的命名格式为0000000.jpg形式,可以自主定义想要的格式
                try:
                    os.rename(src, dst)
                    #print ('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except:
                    continue
        #print ('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

 

split_xml.py 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import sys
import os
import time
import xml.dom.minidom as minidom
reload(sys)
def new_size(old, new_xmin, new_ymin, new_xmax, new_ymax):
    old.getElementsByTagName("xmin")[0].childNodes[0].nodeValue = unicode(str(int(new_xmin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("xmax")[0].childNodes[0].nodeValue = unicode(str(int(new_xmax)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymin")[0].childNodes[0].nodeValue = unicode(str(int(new_ymin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymax")[0].childNodes[0].nodeValue = unicode(str(int(new_ymax)),
                                                                          encoding='utf-8')
def resize_xml(xmlname,xml_path,result_path,n):
    annotation = minidom.parse(xml_path)
    print(annotation)
    size = annotation.getElementsByTagName("size")
    name = annotation.getElementsByTagName("filename")

    width = size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue
    width = int(width)

    height = size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue
    name = name[0].childNodes[0].data

    height = int(height)
    item_width = int(width / n)
    item_height = int(height / n)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0, n):
        for j in range(0, n):
            box = (j * item_width, i * item_height, (j + 1) * item_width, (i + 1) * item_height)
            box_list.append(box)
    for i in range(len(box_list)):
        ef = False
        new_ann = minidom.parse(xml_path)
        obj = new_ann.getElementsByTagName("object")
        bndbox = new_ann.getElementsByTagName("bndbox")
        for j, z in enumerate(bndbox):
            xmin = int(str(z.getElementsByTagName("xmin")[0].childNodes[0].nodeValue))
            xmax = int(str(z.getElementsByTagName("xmax")[0].childNodes[0].nodeValue))
            ymin = int(str(z.getElementsByTagName("ymin")[0].childNodes[0].nodeValue))
            ymax = int(str(z.getElementsByTagName("ymax")[0].childNodes[0].nodeValue))
            #box_list[i][0],box_list[i][1],box_list[i][2],box_list[i][3]
            if (xmin > box_list[i][2] or ymin > box_list[i][3] or ymax < box_list[i][1] or xmax < box_list[i][0]):
                new_ann.documentElement.removeChild(obj[j])
                continue
            else:
                ef = True
                #f = open(os.path.join(result_path, (str(i) + "_" + xmlname.split('.')[0] + "_" + str(j) + '.xml')), 'w')
                if xmin > box_list[i][0] and xmax < box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], 0, xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, xmin - box_list[i][0], 0, xmax - box_list[i][0], box_list[i][3] - box_list[i][1])
                elif xmin < box_list[i][0] and xmax < box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, 0, xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], xmax - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, 0, 0, xmax - box_list[i][0], box_list[i][3] - box_list[i][1])

                elif xmin > box_list[i][0] and xmax > box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], 0, box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, xmin - box_list[i][0], 0, box_list[i][2] - box_list[i][0], box_list[i][3] - box_list[i][1])
                else:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, 0, box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, 0, 0, box_list[i][2] - box_list[i][0], box_list[i][3] - box_list[i][1])

        if ef == True:
            f = open(os.path.join(result_path, (str(i) + "_" + xmlname.split('.')[0] + "_" + str(j) + '.xml')), 'w')
            size = new_ann.getElementsByTagName("size")
            for j, z in enumerate(size):
                z.getElementsByTagName("width")[0].childNodes[0].nodeValue = unicode(str(item_width),
                                                                                      encoding='utf-8')
                z.getElementsByTagName("height")[0].childNodes[0].nodeValue = unicode(str(item_height),
                                                                                     encoding='utf-8')
            new_ann.writexml(f, encoding='utf-8')
            f.close()
        else:
            continue

if __name__ == "__main__":
    n=2
    xml='./DSC00005.xml'
    XML_out='./xml_out'
    xmlname = "DSC00005.xml"
    resize_xml(xmlname, xml, XML_out,n)

 

光达版

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cv2
import sys
import os
import time
import xml.dom.minidom as minidom
reload(sys)
def new_size(old, new_xmin, new_ymin, new_xmax, new_ymax):
    old.getElementsByTagName("xmin")[0].childNodes[0].nodeValue = unicode(str(int(new_xmin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("xmax")[0].childNodes[0].nodeValue = unicode(str(int(new_xmax)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymin")[0].childNodes[0].nodeValue = unicode(str(int(new_ymin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymax")[0].childNodes[0].nodeValue = unicode(str(int(new_ymax)),
                                                                          encoding='utf-8')
def resize_xml(xmlname,xml_path,result_path,n):
    annotation = minidom.parse(xml_path)
    print(annotation)
    size = annotation.getElementsByTagName("size")
    name = annotation.getElementsByTagName("filename")

    width = size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue
    width = int(width)

    height = size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue
    name = name[0].childNodes[0].data

    height = int(height)
    item_width = int(width / n)
    item_height = int(height / n)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0, n):
        for j in range(0, n):
            box = (j * item_width, i * item_height, (j + 1) * item_width, (i + 1) * item_height)
            box_list.append(box)
    for i in range(len(box_list)):
        ef = False
        new_ann = minidom.parse(xml_path)
        obj = new_ann.getElementsByTagName("object")
        bndbox = new_ann.getElementsByTagName("bndbox")
        for j, z in enumerate(bndbox):
            xmin = int(str(z.getElementsByTagName("xmin")[0].childNodes[0].nodeValue))
            xmax = int(str(z.getElementsByTagName("xmax")[0].childNodes[0].nodeValue))
            ymin = int(str(z.getElementsByTagName("ymin")[0].childNodes[0].nodeValue))
            ymax = int(str(z.getElementsByTagName("ymax")[0].childNodes[0].nodeValue))
            #box_list[i][0],box_list[i][1],box_list[i][2],box_list[i][3]
            if (xmin > box_list[i][2] or ymin > box_list[i][3] or ymax < box_list[i][1] or xmax < box_list[i][0]):
                new_ann.documentElement.removeChild(obj[j])
                continue
            else:
                ef = True
                #f = open(os.path.join(result_path, (str(i) + "_" + xmlname.split('.')[0] + "_" + str(j) + '.xml')), 'w')
                if xmax < box_list[i][2]:
                    if ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    else:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0], box_list[i][3] - box_list[i][1])
                elif ymax < box_list[i][3]:
                    new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], box_list[i][2]- box_list[i][0], ymax- box_list[i][1])
                else:
                    new_size(z, xmin - box_list[i][0], ymin- box_list[i][1], box_list[i][2] - box_list[i][0], box[3]- box_list[i][1])
        if ef == True:
            f = open(os.path.join(result_path, (str(i) + "_" + xmlname.split('.')[0] + "_" + str(j) + '.xml')), 'w')
            size = new_ann.getElementsByTagName("size")
            for j, z in enumerate(size):
                z.getElementsByTagName("width")[0].childNodes[0].nodeValue = unicode(str(item_width),
                                                                                      encoding='utf-8')
                z.getElementsByTagName("height")[0].childNodes[0].nodeValue = unicode(str(item_height),
                                                                                     encoding='utf-8')
            new_ann.writexml(f, encoding='utf-8')
            f.close()
        else:
            continue

if __name__ == "__main__":
    n=2
    xml='./DSC00005.xml'
    XML_out='./xml_out'
    xmlname = "DSC00005.xml"
    resize_xml(xmlname, xml, XML_out,n)

 

自己写的版本

 split_xml.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import sys
import os
import time
import xml.dom.minidom as minidom
reload(sys)
def new_size(old, new_xmin, new_ymin, new_xmax, new_ymax):
    old.getElementsByTagName("xmin")[0].childNodes[0].nodeValue = unicode(str(int(new_xmin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("xmax")[0].childNodes[0].nodeValue = unicode(str(int(new_xmax)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymin")[0].childNodes[0].nodeValue = unicode(str(int(new_ymin)),
                                                                          encoding='utf-8')
    old.getElementsByTagName("ymax")[0].childNodes[0].nodeValue = unicode(str(int(new_ymax)),
                                                                          encoding='utf-8')
def resize_xml(xmlname,xml_path,result_path,n):
    annotation = minidom.parse(xml_path)
    print(annotation)
    size = annotation.getElementsByTagName("size")
    name = annotation.getElementsByTagName("filename")

    width = size[0].getElementsByTagName("width")[0].childNodes[0].nodeValue
    width = int(width)

    height = size[0].getElementsByTagName("height")[0].childNodes[0].nodeValue
    name = name[0].childNodes[0].data

    height = int(height)
    item_width = int(width / n)
    item_height = int(height / n)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0, n):
        for j in range(0, n):
            box = (j * item_width, i * item_height, (j + 1) * item_width, (i + 1) * item_height)
            box_list.append(box)
    for i in range(len(box_list)):
        ef = False
        new_ann = minidom.parse(xml_path)
        obj = new_ann.getElementsByTagName("object")
        bndbox = new_ann.getElementsByTagName("bndbox")
        for j, z in enumerate(bndbox):
            xmin = int(str(z.getElementsByTagName("xmin")[0].childNodes[0].nodeValue))
            xmax = int(str(z.getElementsByTagName("xmax")[0].childNodes[0].nodeValue))
            ymin = int(str(z.getElementsByTagName("ymin")[0].childNodes[0].nodeValue))
            ymax = int(str(z.getElementsByTagName("ymax")[0].childNodes[0].nodeValue))
            #box_list[i][0],box_list[i][1],box_list[i][2],box_list[i][3]
            if (xmin > box_list[i][2] or ymin > box_list[i][3] or ymax < box_list[i][1] or xmax < box_list[i][0]):
                new_ann.documentElement.removeChild(obj[j])
                continue
            else:
                ef = True
                #f = open(os.path.join(result_path, (str(i) + "_" + xmlname.split('.')[0] + "_" + str(j) + '.xml')), 'w')
                if xmin > box_list[i][0] and xmax < box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], 0, xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], xmax - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, xmin - box_list[i][0], 0, xmax - box_list[i][0], box_list[i][3] - box_list[i][1])
                elif xmin < box_list[i][0] and xmax < box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, 0, xmax - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], xmax - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, 0, 0, xmax - box_list[i][0], box_list[i][3] - box_list[i][1])

                elif xmin > box_list[i][0] and xmax > box_list[i][2]:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], 0, box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, xmin - box_list[i][0], ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, xmin - box_list[i][0], 0, box_list[i][2] - box_list[i][0], box_list[i][3] - box_list[i][1])
                else:
                    if ymin > box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin < box_list[i][1] and ymax < box_list[i][3]:
                        new_size(z, 0, 0, box_list[i][2] - box_list[i][0],
                                 ymax - box_list[i][1])
                    elif ymin > box_list[i][1] and ymax > box_list[i][3]:
                        new_size(z, 0, ymin - box_list[i][1], box_list[i][2] - box_list[i][0],
                                 box_list[i][3] - box_list[i][1])
                    else:
                        new_size(z, 0, 0, box_list[i][2] - box_list[i][0], box_list[i][3] - box_list[i][1])

        if ef == True:
            f = open(os.path.join(result_path, (str(i) + "_" + str(j) + xmlname.split('.')[0] + '.xml')), 'w')
            size = new_ann.getElementsByTagName("size")
            for k, z in enumerate(size):
                z.getElementsByTagName("width")[0].childNodes[0].nodeValue = unicode(str(item_width),
                                                                                      encoding='utf-8')
                z.getElementsByTagName("height")[0].childNodes[0].nodeValue = unicode(str(item_height),
                                                                                     encoding='utf-8')
            new_ann = minidom.parse(xml_path)
            annotation = new_ann.getElementsByTagName("annotation")
            for n in annotation:
                n.getElementsByTagName("filename")[0].childNodes[0].nodeValue = unicode(
                    str(i) + "_" + str(j) + xmlname.split('.')[0] + '.xml',
                    encoding='utf-8')
            new_ann.writexml(f, encoding='utf-8')
            f.close()
        else:
            continue

if __name__ == "__main__":
    n=2
    XML_path='./Drone_transform_xml'
    XML_out='./xml_out'
    for xml_file in os.listdir(XML_path):
        xmlname = xml_file
        xml = os.path.join(XML_path, xmlname)

        resize_xml(xmlname, xml, XML_out,n)

 

Statistics_tab.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import os
import xml.dom.minidom as minidom
reload(sys)
trace = []
crack = []
line = []
def resize_xml(xml):
    #global counter
    global trace
    global crack
    global line
    annotation = minidom.parse(xml)
    object = annotation.getElementsByTagName("object")

    for i, obj in enumerate(object):
        name = obj.getElementsByTagName("name")[0].childNodes[0].nodeValue
        if name == "trace":
            trace.append(name)
        elif name == "crack":
            crack.append(name)
        else:
            line.append(name)
if __name__ == "__main__":
    XML_path='./xml_out'
    for xml_file in os.listdir(XML_path):
        xml = os.path.join(XML_path,xml_file)
        resize_xml(xml)
    print trace
    print "trace=",len(trace)
    print crack
    print "crack=",len(crack)
    print line
    print "line=",len(line)

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值