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 Examples

Read annotation from xml file
from pathlib import Path

import cv2
from pascal import PascalVOC

ds = Path("./datasets/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/Annotations")
img_path = Path("./datasets/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages")

if __name__ == '__main__':
    for file in ds.glob("*.xml"):
        ann = PascalVOC.from_xml(file)
        img = cv2.imread(str(img_path / ann.filename))
        for obj in ann.objects:
            p1 = (obj.bndbox.xmin, obj.bndbox.ymin)
            p2 = (obj.bndbox.xmax, obj.bndbox.ymin)
            p3 = (obj.bndbox.xmax, obj.bndbox.ymax)
            p4 = (obj.bndbox.xmin, obj.bndbox.ymax)
            cv2.line(img, p1, p2, color=(0, 255, 0), thickness=3)
            cv2.line(img, p2, p3, color=(0, 255, 0), thickness=3)
            cv2.line(img, p3, p4, color=(0, 255, 0), thickness=3)
            cv2.line(img, p4, p1, color=(0, 255, 0), thickness=3)
        cv2.imshow("Image", img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
Make annotation
from pascal import PascalVOC, PascalObject, BndBox, size_block

if __name__ == '__main__':
    obj = PascalObject("chair", "Rear", truncated=False, difficult=False, bndbox=BndBox(263, 211, 324, 339))
    pascal_ann = PascalVOC("000005.jpg", size=size_block(500, 375, 3), objects=[obj])
    pascal_ann.save("000005.xml")
Convert to labelme format
from pathlib import Path
import json

from pascal import PascalVOC

ds = Path("./datasets/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007")
annotations = ds / "Annotations"
img_path = ds / "JPEGImages"
out = ds / "label_me"

if __name__ == '__main__':
    for file in annotations.glob("*.xml"):
        ann = PascalVOC.from_xml(file)
        lbl_me = ann.to_labelme(img_path, save_img_data=False)
        with open(out / f"{file.stem}.json", "w") as f:
            json.dump(lbl_me, f)

Labelme

Convert to YOLO format
from pathlib import Path

from pascal import PascalVOC

ds = Path("xmls")

label_map = {
    "plate": 0,
    "other": 1,
    "taxi": 2,
    "standard": 3
}

if __name__ == '__main__':
    for file in ds.glob("*.xml"):
        ann = PascalVOC.from_xml(file)
        yolo = ann.to_yolo(label_map)
        out_name = f"{file.stem}.txt"
        with open(out_name, "w") as f:
            f.write(yolo)

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-0.0.7.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

pascal_voc-0.0.7-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pascal_voc-0.0.7.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pascal_voc-0.0.7.tar.gz
Algorithm Hash digest
SHA256 d1de9d31f453009264df6a7b3ecff888d0e358d7c9aa39a5b2db82ee82225498
MD5 8ff350778b5b8efe8157136a15615cd1
BLAKE2b-256 46417edfef0e81f328bba29837f241a1c736ee4b0ab2b35941f2c7cac46d5414

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pascal_voc-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pascal_voc-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 462c32318353d53806d676621a24b92510d098a449226cdb2bad1077195e20e1
MD5 9d7ff6486bd08e1dcf6847e964f28680
BLAKE2b-256 6e58e9f1f4a2f790fe37a16289519441b7e205d8d16e194bf4c4b4cecd8f11a0

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