• python-----截取xml文件画框的图片并保存


    from __future__ import division
    import os
    from PIL import Image
    import xml.dom.minidom
    import numpy as np
    ImgPath = r'D:	mpvideo_wang_mod10022_8253_0021_3output/'
    AnnoPath = r'D:	mpvideo_wang_mod10022_8253_0021_3Annotations/'
    ProcessedPath = r'D:	mpvideo_wang_mod10022_8253_0021_3cut/'
    
    imagelist = os.listdir(ImgPath)
    
    for image in imagelist:
        image_pre, ext = os.path.splitext(image)
        imgfile = ImgPath + image
        print(imgfile)
        if not os.path.exists(AnnoPath + image_pre + '.xml' ):
            continue
        xmlfile = AnnoPath + image_pre + '.xml'
        DomTree = xml.dom.minidom.parse(xmlfile)
        annotation = DomTree.documentElement
        filenamelist = annotation.getElementsByTagName('filename')
        filename = filenamelist[0].childNodes[0].data
        objectlist = annotation.getElementsByTagName('object')
        i = 1
        for objects in objectlist:
            namelist = objects.getElementsByTagName('name')
            objectname = namelist[0].childNodes[0].data
            savepath = ProcessedPath + objectname
            if not os.path.exists(savepath):
                os.makedirs(savepath)
            bndbox = objects.getElementsByTagName('bndbox')
            cropboxes = []
            for box in bndbox:
                x1_list = box.getElementsByTagName('xmin')
                x1 = int(x1_list[0].childNodes[0].data)
                y1_list = box.getElementsByTagName('ymin')
                y1 = int(y1_list[0].childNodes[0].data)
                x2_list = box.getElementsByTagName('xmax')
                x2 = int(x2_list[0].childNodes[0].data)
                y2_list = box.getElementsByTagName('ymax')
                y2 = int(y2_list[0].childNodes[0].data)
                w = x2 - x1
                h = y2 - y1
                obj = np.array([x1,y1,x2,y2])
                shift = np.array([[1,1,1,1]])
                XYmatrix = np.tile(obj,(1,1))
                cropboxes = XYmatrix * shift
                img = Image.open(imgfile)
                for cropbox in cropboxes:
                    cropedimg = img.crop(cropbox)
                    cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg')
                    i += 1
  • 相关阅读:
    asking邱宝裕
    一文深度解读量化交易(下)
    一文深度解读量化交易(上)
    关于日内波段交易系统:一个期货高手的交易思路独白
    财务报表分析(张新民教授)-第一章笔记
    定增游戏(二)
    leetcode -- Restore IP Addresses
    leetcode -- Minimum Window Substring
    leetcode -- Maximal Rectangle TODO O(N)
    leetcode -- Unique Binary Search Trees II
  • 原文地址:https://www.cnblogs.com/xiaodai0/p/10146130.html
Copyright © 2020-2023  润新知