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

How to read xml annotation #3

Open
zhang-haojie opened this issue Oct 30, 2023 · 8 comments
Open

How to read xml annotation #3

zhang-haojie opened this issue Oct 30, 2023 · 8 comments

Comments

@zhang-haojie
Copy link

How to read 'xml' annotation to mask?

@BathVisArtData
Copy link
Owner

BathVisArtData commented Oct 30, 2023 via email

@zhang-haojie
Copy link
Author

thank you for your reply!

I would like to know if there is a reference Dataset code that can read the binary mask from the xml file in Pascal VOC annotation format. This is my first time dealing with annotations in XML format, and the information on the Internet seems to be of little help. Thank you again for your great work.

best

@BathVisArtData
Copy link
Owner

BathVisArtData commented Oct 30, 2023 via email

@nw89
Copy link

nw89 commented Oct 30, 2023

You can also use someone else's code so as to not have to process the xml directly. If you are using python, you can use this package. This is recently updated and requires Python 3.10 so might not work on older systems.

Here is a basic script which prints out whether an image has people or not and where the bounding box is. Note that it is not a binary mask but rectangular boxes.

import os

# Install with "pip install pascal-voc"
from pascal import annotation_from_xml

DATASET_FILE = "Annotations/person_test.txt"

JPEG_DIR = "JPEGImages"
ANNOTATIONS_DIR = "Annotations"

ATTR_TYPE_SPEC = {"truncated": bool, "difficult": bool}

all_files_in_set = []
with open(DATASET_FILE, "r") as f:
    for line in f:
        line_split = line.strip().split(" ")
        assert len(line_split) == 2
        assert line_split[1] in ("-1", "1")   
         
        all_files_in_set.append((line_split[0], line_split[1] == "1"))
        
for img_path, positive in all_files_in_set:
    annotation_file = os.path.join(ANNOTATIONS_DIR, img_path+".xml")

    if not positive:
        print(f"{img_path} has no people.")
        assert not os.path.exists(annotation_file)
        continue
    
    assert os.path.exists(annotation_file)

    ann = annotation_from_xml(annotation_file, ATTR_TYPE_SPEC)

    print(f"{img_path} has people annotated:")
    for obj in ann.objects:
        bbox = obj.bndbox
        print(f"xmin: {bbox.xmin}, ymin: {bbox.ymin}, "+
              f"xmin: {bbox.xmax}, xmin: {bbox.ymax}")
    print()

@zhang-haojie
Copy link
Author

Thank you for your reply. I would like to know if it is possible to read the binary mask from the xml annotation file?

@nwestlake
Copy link
Collaborator

nwestlake commented Oct 31, 2023 via email

@zhang-haojie
Copy link
Author

Sorry, I didn't describe my problem clearly at the beginning.

I wanted to get the binary mask of each obejct in the dataset (a more detailed binary mask), because I was doing a segmentation task, but I couldn't find the GT mask corresponding to the image.

@nw89
Copy link

nw89 commented Oct 31, 2023 via email

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

4 participants