在python中创建coco格式的边界框注释

问题描述

from numpy import random
data = {}
import json

data["images"] = []
data["annotations"] = []

# Define the classes (out of the 81) which you want to see. Others will not be shown.
category_ids = [1]
category_id_to_name = {1: 'benign_1',2: 'malignant_2'}
bBoxes =[]
NUM_OF_AUG = 3 # Number of augmentations for every image

# Fetch class IDs only corresponding to the filterClasses
catIds = coco.getCatIds() 
# Get all images containing the above Category IDs
imgIds = coco.getImgIds(catIds=0)
print("Number of images containing all the  classes:",len(imgIds))

# load and display a random image
img = coco.loadImgs(ids=2)
I = io.imread(os.path.join(dataDir,img[0]['file_name']))/255.0

# get annotations from corrosponding img
annIds = coco.getAnnIds(imgIds=img[0]['id'],catIds=catIds,iscrowd=None)
anns = coco.loadAnns(annIds)

bBoxes.append(anns[0]['bBox']) # save the bBox values
new_img = img.copy()
new_anns = anns.copy()
for i in range(NUM_OF_AUG):

    augmentations = transform(image=I,bBoxes=bBoxes,category_ids=category_ids)
    augmented_img = augmentations["image"]
    augmented_bBox = augmentations["bBoxes"]
    #visualize(augmented_img,augmented_bBox,category_ids,category_id_to_name)
    
    new_anns[0]['bBox'] = augmented_bBox
    new_img[0]['id'] = i

    x = random.rand()*10
    name = "augmented"+ str(x)+'.jpg'
    new_img[0]['file_name'] = name

    data["images"].append(new_img)
    data["annotations"].append(new_anns)


    {'annotations': [[{'area': 6305,'bBox': [(118.70517461626747,249.2004033678208,147.24821066225996,131.68045778464267)],'category_id': 1,'id': 1,'image_id': 1,'iscrowd': 0,'segmentation': []}],[{'area': 6305,'segmentation': []}]],'images': 
[[{'date_captured': '2021-01-31T14:09:29+00:00','file_name': 'benign_6.611640031287464.jpg','height': 416,'id': 4,'license': 1,'width': 416}],[{'date_captured': '2021-01-31T14:09:29+00:00','width': 416}]]}  

    {'annotations': [[{'area': 6305,'images': [[{'date_captured': '2021-01-31T14:09:29+00:00',

我想在转换后创建增强版本的图像和相应的边界框,并将其保存为 json coco 格式。但是数据总是存储与最后增强的 bBox 和图像相同的值

如何获取注释和图像的顺序版本

输出

"images": [
        {
            "id": 0,"license": 1,"file_name": "benign--184-_png.rf.0835133a4b3867b2503f7afb080d6ef7.jpg","height": 416,"width": 416,"date_captured": "2021-01-31T14:09:29+00:00"
        },{
            "id": 1,"file_name": "malignant--147-_png.rf.04e0a71de64594f0a3a65e6ac7b4794b.jpg","annotations": [
        {
            "id": 0,"image_id": 0,"category_id": 1,"bBox": [
                73,65,336,101.5
            ],"area": 34104,"segmentation": [],"iscrowd": 0
        },"image_id": 1,"category_id": 2,"bBox": [
                165,74,189,168.5
            ],"area": 31846.5,

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)