Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: list index out of range #36

Open
anmspro opened this issue Jul 3, 2020 · 2 comments
Open

IndexError: list index out of range #36

anmspro opened this issue Jul 3, 2020 · 2 comments

Comments

@anmspro
Copy link

anmspro commented Jul 3, 2020

Full error:

Traceback (most recent call last):
  File "segment.py", line 108, in <module>
    class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]
IndexError: list index out of range

Here's the code:

INFO = {
    "description": "Fashion Dataset",
    "url": "https://github.com/waspinator/pycococreator",
    "version": "0.1.0",
    "year": 2020,
    "contributor": "Abu Noman Md. Sakib",
    "date_created": datetime.datetime.utcnow().isoformat(' ')
}

LICENSES = [
    {
        "id": 1,
        "name": "GB",
        "url": "GB"
    }
]

CATEGORIES = [
    {
        'id': 1,
        'name': 'mask',
        'supercategory': 'fashion',
    }
]

coco_output = {
    "info": INFO,
    "licenses": LICENSES,
    "categories": CATEGORIES,
    "images": [],
    "annotations": []
}

image_id = 1
segmentation_id = 1

ROOT_DIR = "train"
IMAGE_DIR = "train/images"
ANNOTATION_DIR = "train/annotations"

image_files = [f for f in listdir(IMAGE_DIR) if isfile(join(IMAGE_DIR, f))]
annotation_files = [f for f in listdir(ANNOTATION_DIR) if isfile(join(ANNOTATION_DIR, f))]

# go through each image
for image_filename in image_files:
    image = Image.open(IMAGE_DIR + '/' + image_filename)
    image_info = pycococreatortools.create_image_info(image_id, os.path.basename(image_filename), image.size)

    coco_output["images"].append(image_info)

# go through each associated annotation
for annotation_filename in annotation_files:

    print(annotation_filename)
    class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]
    category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
    binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8)

    annotation_info = pycococreatortools.create_annotation_info(segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2)

    if annotation_info is not None:
        coco_output["annotations"].append(annotation_info)

    segmentation_id = segmentation_id + 1

    image_id = image_id + 1

with open('train/images.json', 'w') as output_json_file:
    json.dump(coco_output, output_json_file)

I still don't know if the code works or not. Please help!

@kp421
Copy link

kp421 commented Nov 15, 2020

Did you manage to solve this?

@kp421
Copy link

kp421 commented Nov 15, 2020

I just managed to solve it - the name for one of the categories was misspelled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants