【问题标题】:Bounding box extraction边界框提取
【发布时间】:2021-04-16 19:04:40
【问题描述】:

我发现用于生成位于 .xml 文件中的 xmin、xmax、ymin、ymax 的函数遇到了一些问题。我不确定为什么它不起作用,我觉得这是一个我没有考虑的简单错误。

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')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-b6a299d4377e> in <module>
     23 
     24 
---> 25 bbs = extract_boxes(r'C:\Users\name\Desktop\kangaroo-master\annots\00001.xml')
     26 
     27 

TypeError: extract_boxes() missing 1 required positional argument: 'filename'

【问题讨论】:

  • 您似乎将extract_boxes 定义为在一个类中,但您实际上并没有将它放在一个类中。
  • 啊,是的,我确实从课堂上提取了它。所以我不应该需要self 参数吗?而且我不太熟悉ElementTree.parse 在做什么。

标签: python bounding-box


【解决方案1】:

我发现了错误。我没有将元素树导入并需要删除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')```

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2019-08-26
    • 2018-02-10
    • 2014-06-20
    • 2022-08-16
    • 2014-02-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多