Package: express-pascal-voc-tools
Express Pascal Voc ToolsA tool for creating, reading and visualizing Pascal VOC annotations.Report BugshereGetting StartedInstallpip install express-pascal-voc-toolsSingle file Parsingfromvoc_toolsimportreaderasvoc_reader# `from_xml()` parse XMLforannoinvoc_reader.from_xml(r"sixray_data\train\Annotations\P00002.xml"):print(anno.xmin,anno.xmax)# `from_image()` Parse XML by providing image path(it will automatically choose the correct XML)forannoinvoc_reader.from_image(r"sixray_data\train\JPEGImages\P00002.jpeg"):print(anno.xmin,anno.xmax)# `from_file()` Get the parsed metadata as a tupleforannoinvoc_reader.from_file(r"sixray_data\train\JPEGImages\P00002.xml"):print(anno.raw())forannoinvoc_reader.from_file(r"sixray_data\train\JPEGImages\P00002.jpeg"):print(anno.raw())# `from_dir()` Get the parsed metadata as a tuple for entire directoryforannoinvoc_reader.from_dir("sixray_data\train")):print(anno.raw())Dataset level parsingUsingVOCDatasetclass we can address a Pascal VOC dataset. In general Pascal VOC
Datasets are organised as below:my_dataset
    |
    +- train
    |   |
    |   +- Annotations
    |   |  |
    |   |  +- ITEM001.xml
    |   |  +- ITEM002.xml
    |   +- JPEGImages
    |       |
    |       +- ITEM001.jpeg
    |       +- ITEM002.jpeg
    +- test
        |
        +- Annotations
        |  |
        |  +- ITEM0010.xml
        |  +- ITEM0020.xml
        +- JPEGImages
            |
            +- ITEM0010.jpeg
            +- ITEM0020.jpegfromvoc_tools.utilsimportVOCDatasetdataset_path="/my_dataset"# initialize a datasetmy_dataset=VOCDataset(dataset_path)# fetch annotation bulkforannotations,jpeginmy_dataset.train.fetch():print(annotations[0].filename,jpeg.image.shape)# fetch annotationforanno,jpeginmy_dataset.train.fetch(bulk=False):print(anno,jpeg.image.shape)# parse the annotations into memory for train datasetmy_dataset.train.load()my_dataset.test.load()# returns a list of class names in train datasetmy_dataset.train.class_names()my_dataset.test.class_names()# save parsed information into csvmy_dataset.train.load().to_csv("./train_metadata.csv")my_dataset.test.load().to_csv("./train_metadata.csv")# purge the parsed metadata to free memorymy_dataset.train.unload()my_dataset.test.unload()Caption SupportThis is an optional feature introduced to facilitate the new trends in prompt engineering and text based Generative AI.
In this case the dataset must contain atextdirectory as below:my_dataset
    |
    +- train
    |   |
    |   +- Annotations
    |   |  |
    |   |  +- ITEM001.xml
    |   |  +- ITEM002.xml
    |   +- JPEGImages
    |       |
    |       +- ITEM001.jpeg
    |       +- ITEM002.jpeg
    |   +- text
    |       |
    |       +- ITEM001.text
    |       +- ITEM002.text
    +- test
        |
        +- Annotations
        |  |
        |  +- ITEM0010.xml
        |  +- ITEM0020.xml
        +- JPEGImages
            |
            +- ITEM0010.jpeg
            +- ITEM0020.jpeg
        +- text
            |
            +- ITEM0010.text
            +- ITEM0020.textfromvoc_tools.utilsimportVOCDatasetdataset_path="/my_dataset"voc_caption_data=VOCDataset(dataset_path,caption_support=True)# init dataset with caption# read caption bulkforcaptionsinvoc_caption_data.train.caption.fetch():print(captions[0].raw())# read caption one by oneforcaptioninvoc_caption_data.train.caption.fetch(bulk=False):print(caption.raw())# save captions to a CSVvoc_caption_data.train.caption.to_csv("train_captions.csv")Visualizefromvoc_tools.visulizerimportfrom_jpeg,see_jpegjpeg=from_jpeg(r"sixray_data\train\JPEGImages\P00002.jpg")jpeg.see()# ORsee_jpeg(r"sixray_data\train\JPEGImages\P00002.jpg")Load not PascalVOC datasetimportpathlibfromvoc_tools.constantsimportVOC_IMAGESfromvoc_tools.readerimportlist_dirfromvoc_tools.utilsimportDataset,VOCDatasetdataset=pathlib.Path(r"path/to/dataset")# set global flagsDataset.IMAGE_DIR="images"# Say, instead of 'JPEGImages', the images are stored in 'images' directory# reading filepathsfile_paths=list(list_dir(str(dataset/"train"),dir_flag=VOC_IMAGES,fullpath=True))file_paths.extend(list(list_dir(str(dataset/"test"),dir_flag=VOC_IMAGES,fullpath=True)))# if you have captions stored in 'captions' directoryvoc_data=VOCDataset(dataset,caption_support=True)CollaborateGitHub:https://github.com/Redcof/pascal_voc_tools.gitBuild and Publishpython setup.py sdist bdist_wheelpython -m twine upload dist/*