Skip to main content

Tool to work with annotation formats

Project description

pascal

Python utility to work with PascalVoc annotation format

Image examples from PascalVoc2007 dataset

Code Example
import json
from pathlib import Path

from PIL import Image

from pascal import annotation_from_xml
from pascal.utils import save_xml

ds_path = Path("/home/VOCtest_06-Nov-2007/VOCdevkit/VOC2007")

img_src = ds_path / "JPEGImages"
ann_src = ds_path / "Annotations"

out_labelme = ds_path / "converted_labelme"

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

label_map = {"car": 1, "dog": 0, "person": 2, "train": 3}

if __name__ == "__main__":
    for file in img_src.glob("*.jpg"):
        # Get annotation file path
        ann_file = (ann_src / file.name).with_suffix(".xml")
        # Make pascal annotation object
        ann = annotation_from_xml(ann_file, attr_type_spec)
        print(ann)
        # Save to xml file (same as ann_file)
        xml = ann.to_xml()
        out_xml_name = file.with_suffix(".xml").name
        save_xml(out_xml_name, xml)
        # Save yolo annotation
        yolo_ann = ann.to_yolo(label_map)
        out_yolo_name = file.with_suffix(".txt").name
        with open(out_yolo_name, "w") as f:
            f.write(yolo_ann)
        # Convert to labelme and save json file
        res = ann.to_labelme(file, save_img_data=False)
        with open((out_labelme / file.name).with_suffix(".json"), "w") as f:
            json.dump(res, f, indent=2)
        # Draw objects
        img = Image.open(file)
        draw_img = ann.draw_boxes(img)
        draw_img.show()
Visualization example:
draw_img = ann.draw_boxes(img)
draw_img.show()

vis_example

Installation

From source

python setup.py install

Using pip

pip install pascal-voc

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pascal_voc-2.1.5.tar.gz (218.8 kB view details)

Uploaded Source

Built Distribution

pascal_voc-2.1.5-py3-none-any.whl (425.9 kB view details)

Uploaded Python 3

File details

Details for the file pascal_voc-2.1.5.tar.gz.

File metadata

  • Download URL: pascal_voc-2.1.5.tar.gz
  • Upload date:
  • Size: 218.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pascal_voc-2.1.5.tar.gz
Algorithm Hash digest
SHA256 2b6bf12ebaa11718a67f4dbff6fa4b0b2a594b36b53662bd93dd7e762a2cc045
MD5 0fe7f323093cfa2e6bc47a9fabc98145
BLAKE2b-256 926006c61f1029d5e7a74afa0b871bd929ca8ff0a45f43305ff2656a6700d580

See more details on using hashes here.

File details

Details for the file pascal_voc-2.1.5-py3-none-any.whl.

File metadata

  • Download URL: pascal_voc-2.1.5-py3-none-any.whl
  • Upload date:
  • Size: 425.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pascal_voc-2.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 07be3c2ba06c84e69e993b346caf175beabdfc3b898ee4c58211b88b8c558914
MD5 9b435f0b8530caa7afdde4e5ce506dba
BLAKE2b-256 1da1b238f1e44d0089d9f1f0444ab8fa8cb06f5d2dfe03d4b89fce9a84292a82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page