简体   繁体   English

边界框提取

[英]Bounding box extraction

I am having some trouble with a function I found for generating the xmin, xmax, ymin, ymax located in a .xml file.我发现 function 用于生成位于.xml文件中的 xmin、xmax、ymin、ymax 时遇到了一些问题。 I am not sure why is it not working and I feel like it's a simple mistake that I am not considering.我不确定为什么它不起作用,我觉得这是一个我没有考虑的简单错误。

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'

I found the mistake.我发现了错误。 I did not import Element Tree in and needed to remove self because I originally pulled the code from a class, but am not using it in a class anymore.我没有将元素树导入并需要删除self ,因为我最初是从 class 中提取代码,但不再在 class 中使用它。

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')```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM