附加到列表时值重复

Values Duplicate When Appending to List

我有一个数组列表,我将每个索引分解为 xy 坐标。但是,第一个数组是 xminymin,第二个数组是 xmaxymax。然后它重复。所以在我这里的示例中,我将有两个 xminymin 和两个 xmaxymax(两个框)。然后我试图将所有 xminyminxmaxymax 附加到它们自己的列表中。虽然当我附加它们时,一些数字被分配到错误的位置以及重复,但我不确定为什么会这样。

我正在使用此代码从 .xml 文件中提取边界框,增加值(尽管在此示例中因为我使用的是模糊,原始值不会更改)然后重新保存它们到另一个 .xml 文件中。我很难弄清楚如何迭代这些过程。我知道有一种更短的编码方法,但这是我能够想到的,对我来说很有意义。

一如既往,如有任何帮助,我们将不胜感激。

我的代码:

import xml.etree.cElementTree as ET2
import imgaug.augmenters as iaa
import imageio
import xml.etree.ElementTree as ET
from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage

image_x = imageio.imread(r'D:\data\file[=10=]7.jpeg')
filename = r'D:\data\file[=10=]7.xml'

seq = iaa.GaussianBlur(sigma=(0, 2))

# bounding box colors
GREEN = [0, 255, 0]
ORANGE = [255, 140, 0]
RED = [255, 0, 0]

# extract bounding boxes
tree = ET.parse(filename)
root = tree.getroot()
multiple_annots = []
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)
    multiple_annots.append([xmin, ymin, xmax, ymax])

bbx_list = []
for i in multiple_annots:
    index = BoundingBox(i[0], i[1], i[2], i[3])
    bbx_list.append(index)
bbs = BoundingBoxesOnImage(bbx_list, shape=image_x.shape)

image_aug, bbs_aug = seq(image=image_x, bounding_boxes=bbs)

xy_pairs_list = []
for boxes in bbs_aug:
    for xy_pairs in boxes:
        xy_pairs_list.append(xy_pairs)
print(xy_pairs_list)
print(bbs_aug)


num = 0
xmin_list = []
ymin_list = []
xmax_list = []
ymax_list = []
for individuals in xy_pairs_list:
    if (num % 2) == 0:
        xmin = individuals[0]
        ymin = individuals[1]
    else:
        xmax = individuals[0]
        ymax = individuals[1]

    xmin_list.append(xmin)
    ymin_list.append(ymin)
    xmax_list.append(xmax)
    ymax_list.append(ymax)

    num = num + 1
print('xmin:', xmin_list, 'ymin:', ymin_list, 'xmax:', xmax_list, 'ymax:', ymax_list)

bbs_aug output: BoundingBoxesOnImage([BoundingBox(x1=30.0000, y1=1.0000, x2=67.0000, y2=44.0000, label=None), BoundingBox(x1=39.0000, y1=136.0000, x2=73.0000, y2=176.0000, label=None)], shape=(185, 186, 3))

xy_pairs_list output: [array([30.,  1.], dtype=float32), array([67., 44.], dtype=float32), array([ 39., 136.], dtype=float32), array([ 73., 176.], dtype=float32)]

xmin, ymin, xmax, ymax output: xmin: [30.0, 30.0, 39.0, 39.0] ymin: [1.0, 1.0, 136.0, 136.0] xmax: [73, 67.0, 67.0, 73.0] ymax: [176, 44.0, 44.0, 176.0]

期望的输出:

xmin: [30, 39]
ymin: [1, 136]
xmax: [67, 73]
ymax: [44, 176]

编辑:

XML 输入文件:

<?xml version="1.0"?>
-<annotation>
        <folder>jpeg</folder>
        <filename>000.jpeg</filename>   
        <path>C:\jpeg[=13=]0.jpeg</path>
        -<source>
                <database>Unknown</database>
        </source>
        -<size>
                <width>185</width>
                <height>185</height>
                <depth>3</depth>
        </size>
        <segmented>0</segmented>
        -<object>
                <name>item</name>
                <pose>Unspecified</pose>
                <truncated>0</truncated>
                <difficult>0</difficult>
        -<bndbox>
                <xmin>30</xmin>
                <ymin>1</ymin>
                <xmax>67</xmax>
                <ymax>44</ymax>
                </bndbox>
        </object>
        -<object>
                <name>item</name>
                <pose>Unspecified</pose>
                <truncated>1</truncated>
                <difficult>0</difficult>
                -<bndbox>
                        <xmin>39</xmin>
                        <ymin>136</ymin>
                        <xmax>73</xmax>
                        <ymax>176</ymax>
                  </bndbox>
        </object>
</annotation>

注意 - 我只看到了下面的代码 num=0,因为我可以找到从该代码添加的重复项。没有审查上面的代码。

将您的代码从 num=0 所在的行更改为:

num = 0
xmin_list = []
ymin_list = []
xmax_list = []
ymax_list = []
for individuals in xy_pairs_list:
    if (num % 2) == 0:
        xmin = individuals[0]
        ymin = individuals[1]
        xmin_list.append(int(xmin))
        ymin_list.append(int(ymin))
    else:
        xmax = individuals[0]
        ymax = individuals[1]
        xmax_list.append(int(xmax))
        ymax_list.append(int(ymax))
    num = num + 1
    
print('xmin:', xmin_list, 'ymin:', ymin_list, 'xmax:', xmax_list, 'ymax:', ymax_list)

@Sowjanya R Bhat

已更改

xmin_list = set()
ymin_list = set()
xmax_list = set()
ymax_list = set()

xmin_list = OrderedSet()
ymin_list = OrderedSet()
xmax_list = OrderedSet()
ymax_list = OrderedSet()

print(bbs_aug)
print(print('xmin:', xmin_list, 'ymin:', ymin_list, 'xmax:', xmax_list, 'ymax:', ymax_list)
print(df)
BoundingBoxesOnImage([BoundingBox(x1=30.0000, y1=1.0000, x2=67.0000, y2=44.0000, label=None), BoundingBox(x1=39.0000, y1=136.0000, x2=73.0000, y2=176.0000, label=None)], shape=(185, 186, 3))

xmin: [30, 39] ymin: [1, 136] xmax: [73, 67] ymax: [176, 44]

   xmin  ymin  xmax  ymax
0    30     1    73   176
1    39   136    67    44