Len 函数在 python 中返回不同的值

问题描述 投票:0回答:0

我正在使用一个名为 chitra.link 的库-https://chitra.readthedocs.io/en/latest/chitra/coordinates/ 我基本上写了一个函数来获取图像及其边界框数据(一个图像可以包含多个对象)并调整它们的大小,然后我保存调整大小的图像并更新边界框值。问题出现在第 38 行,基本上函数需要边界框的长度和边界框各自标签的列表长度相同,并且显示长度不相同的错误(1,4 ).我已经使用打印检查过,长度相同(4,4),所以请帮我找出问题所在。

代码- anns_file_path = 'data/annotations.json' # 读取注释 用 open(anns_file_path, 'r') 作为 f: 数据集 = json.loads(f.read())

categories = dataset['categories']
anns = dataset['annotations']
imgs = dataset['images']

images={}
images_bbox = defaultdict(list)
images_labels= defaultdict(list)

for i in imgs:
    images[i['id']]=i['file_name']

for j in anns:
    my_coco_box = j['bbox']
    coco_bbox = BoundingBox.from_coco(*my_coco_box)
    voc_bbox = coco_bbox.to_voc()
    voc_bbox_values = coco_bbox.to_voc(return_values=True)  
    voc_bbox_values=list(voc_bbox_values)
    images_bbox[j['image_id']].append(voc_bbox_values)
    images_labels[j['image_id']].append(categories[j['category_id']]['supercategory'])


size = (256,224)
i=0
for key,value in images.items():
    url='data/'+value
    im=Image.open(url)
    box=images_bbox[key]
    labels=images_labels[key]
    print(box,labels)
    print(len(box),len(labels))
    image = Chitra(im, box, labels)
    image.resize_image_with_bbox(size)
    image.image.save('resized_'+url)
    for j in image.bboxes:
        a=list(image.bboxes[0][0])
        b=list(image.bboxes[0][1])
        a.append(b[0])
        a.append(b[1])
        dataset['annotations'][i]['bbox']=a
        i+=1
              
with open("resized_data/annotations.json", "wt") as jsonFile:
    json.dump(str(dataset), jsonFile)

输出-

[[517, 127, 964, 1449]] ['Bottle']

1 1

[[1, 457, 1430, 1976], [531, 292, 1537, 964]] ['Carton', 'Carton']

2 2

[[632, 987, 1132, 1361], [632, 989, 676, 1040]] ['Bottle', 'Bottle cap']

2 2

[[209, 920, 663, 1482], [1212, 822, 1391, 1268], [634, 1442, 663, 1481], [589, 548,               930, 953]] ['Bottle', 'Bottle', 'Bottle cap', 'Can']

4 4

---------------------------------------------------------------------------

UserWarning                               Traceback (most 
recent call last)
~\AppData\Local\Temp\ipykernel_15916\3031951978.py in <module>
     35     print(box,labels)
     36     print(len(box),len(labels))
---> 37     image = Chitra(im, box, labels)
     38     image.resize_image_with_bbox(size)
     39     image.image.save('resized_'+url)

~\anaconda3\lib\site-packages\chitra\image\image.py in __init__(self, data, bboxes, labels,      box_format, cache, *args, **kwargs)
     72 
     73         if bboxes is not None:
---> 74             self.bboxes = BoundingBoxes(bboxes, labels)
     75 
     76     @staticmethod

~\anaconda3\lib\site-packages\chitra\coordinates.py in __init__(self, bboxes, labels,     box_format)
     31 
     32         if len(bboxes) != len(labels):
---> 33             raise UserWarning(
     34                 f"len of boxes and labels not matching: {len(bboxes), len(labels)}"
     35             )

UserWarning: len of boxes and labels not matching: (1, 4)
python image-processing deep-learning computer-vision
© www.soinside.com 2019 - 2024. All rights reserved.