I have a list of arrays that I am breaking each index up into a x
and y
coordinate. However, the first array is xmin
, ymin
and the second array is xmax
, ymax
. Then it reapeats. So in my example here, I would have two xmin
, ymin
and two xmax
, ymax
(two boxes). I then am trying to append all the xmin
, ymin
, xmax
, ymax
into their own lists. Though when I append them, some of the numbers are being assigned to the wrong spot as well as duplicating and I am unsure why this is happening.
I am using this code to extract bounding boxes from an .xml
file, augment the values (though in this example because I am using a blur, the original values will not be changed) and then resave them into another .xml
file. I am having difficulty figuring out how to iterate through the processes. I understand that there is a much shorter way to code this, but this is what I have been able to come up and makes sense to me.
As always, any help would be much appreciated.
My Code:
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\007.jpeg')
filename = r'D:\data\file\007.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]
Desired Output:
xmin: [30, 39]
ymin: [1, 136]
xmax: [67, 73]
ymax: [44, 176]
EDITS:
XML input file:
<?xml version="1.0"?>
-<annotation>
<folder>jpeg</folder>
<filename>000.jpeg</filename>
<path>C:\jpeg\000.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>
NOTE - I have only seen the code below num=0
, since I could find duplicates added from that code. Not reviewed code above that.
Change your code from the line where you have num=0
to this :
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)