边界框提取

问题描述

我发现用于生成位于 insert into record(name,"CELL NUMBER",...) 文件中的 xmin、xmax、ymin、ymax 的函数遇到了一些问题。我不知道为什么它不起作用,我觉得这是一个我没有考虑的简单错误

.xml
def extract_Boxes(self,filename):
        
        # load and parse the file
        tree = ElementTree.parse(filename)
        # get the root of the document
        root = tree.getroot()
        # extract each bounding Box
        Boxes = list()
        for Box in root.findall('.//bndBox'):
            xmin = int(Box.find('xmin').text)
            ymin = int(Box.find('ymin').text)
            xmax = int(Box.find('xmax').text)
            ymax = int(Box.find('ymax').text)
            coors = [xmin,ymin,xmax,ymax]
            Boxes.append(coors)
        
        # extract image dimensions
        width = int(root.find('.//size/width').text)
        height = int(root.find('.//size/height').text)
        return Boxes,width,height


bbs = extract_Boxes(r'C:\Users\name\Desktop\kangaroo-master\annots\00001.xml')

解决方法

我发现了错误。我没有导入元素树,需要删除 self,因为我最初从类中提取代码,但不再在类中使用它。

import xml.etree.ElementTree as ET

def extract_boxes(filename):        
    # load and parse the file
    tree = ET.parse(filename)
    # get the root of the document
    root = tree.getroot()
    # extract each bounding box
    boxes = list()
    for box in root.findall('.//bndbox'):
        xmin = int(box.find('xmin').text)
        ymin = int(box.find('ymin').text)
        xmax = int(box.find('xmax').text)
        ymax = int(box.find('ymax').text)
        coors = [xmin,ymin,xmax,ymax]
        boxes.append(coors)
        
    # extract image dimensions
    width = int(root.find('.//size/width').text)
    height = int(root.find('.//size/height').text)
    return boxes,width,height


bbs = extract_boxes(r'C:\Users\zlesl\Desktop\kangaroo-master\annots\00001.xml')```