python 利用xml文件中bndbox座標截圖並保存

import cv2
import numpy as np 

import xml.dom.minidom
import os
import argparse

img_path = 'images/'
anno_path = 'annotations/'
cut_path = 'cut/'
imagelist = os.listdir(img_path)

for image in imagelist:
	image_pre, ext = os.path.splitext(image)
	img_file = img_path + image
	img = cv2.imread(img_file)
	xml_file = anno_path + image_pre + '.xml'
	DOMTree = xml.dom.minidom.parse(xml_file)
	collection = DOMTree.documentElement
	objects=collection.getElementsByTagName("object")


	for object in objects:
		print ("*****Object*****")
		bndbox = object.getElementsByTagName('bndbox')[0]
		xmin = bndbox.getElementsByTagName('xmin')[0]
		xmin_data=xmin.childNodes[0].data
		ymin = bndbox.getElementsByTagName('ymin')[0]
		ymin_data=ymin.childNodes[0].data
		xmax = bndbox.getElementsByTagName('xmax')[0]
		xmax_data=xmax.childNodes[0].data
		ymax = bndbox.getElementsByTagName('ymax')[0]
		ymax_data=ymax.childNodes[0].data
		xmin = int(xmin_data)
		xmax = int(xmax_data)
		ymin = int(ymin_data)
		ymax = int(ymax_data)
		img_cut = img[ymin:ymax,xmin:xmax,:]
		cv2.imwrite(cut_path+'cut_img_{}.jpg'.format(image_pre),img_cut)

參考1: python-----截取xml文件畫框的圖片並保存

參考1代碼:

from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np
ImgPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\output/'
AnnoPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\Annotations/'
ProcessedPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\cut/'

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

參考2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章