A tool for creating, reading and visualizing Pascal VOC annotations
Project description
Express Pascal Voc Tools
A tool for creating, reading and visualizing Pascal VOC annotations. Report Bugs here
Getting Started
Install
pip install express-pascal-voc-tools
Single file Parsing
from voc_tools import reader as voc_reader
# `from_xml()` parse XML
for anno in voc_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)
for anno in voc_reader.from_image(r"sixray_data\train\JPEGImages\P00002.jpeg"):
print(anno.xmin, anno.xmax)
# `from_file()` Get the parsed metadata as a tuple
for anno in voc_reader.from_file(r"sixray_data\train\JPEGImages\P00002.xml"):
print(anno.raw())
for anno in voc_reader.from_file(r"sixray_data\train\JPEGImages\P00002.jpeg"):
print(anno.raw())
# `from_dir()` Get the parsed metadata as a tuple for entire directory
for anno in voc_reader.from_dir("sixray_data\train")):
print(anno.raw())
Dataset level parsing
Using VOCDataset
class 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.jpeg
from voc_tools.utils import VOCDataset
dataset_path = "/my_dataset"
# initialize a dataset
my_dataset = VOCDataset(dataset_path)
# fetch annotation bulk
for annotations, jpeg in my_dataset.train.fetch():
print(annotations[0].filename, jpeg.image.shape)
# fetch annotation
for anno, jpeg in my_dataset.train.fetch(bulk=False):
print(anno, jpeg.image.shape)
# parse the annotations into memory for train dataset
my_dataset.train.load()
my_dataset.test.load()
# returns a list of class names in train dataset
my_dataset.train.class_names()
my_dataset.test.class_names()
# save parsed information into csv
my_dataset.train.load().to_csv("./train_metadata.csv")
my_dataset.test.load().to_csv("./train_metadata.csv")
# purge the parsed metadata to free memory
my_dataset.train.unload()
my_dataset.test.unload()
Caption Support
This 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 a text
directory 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.text
from voc_tools.utils import VOCDataset
dataset_path = "/my_dataset"
voc_caption_data = VOCDataset(dataset_path, caption_support=True) # init dataset with caption
# read caption bulk
for captions in voc_caption_data.train.caption.fetch():
print(captions[0].raw())
# read caption one by one
for caption in voc_caption_data.train.caption.fetch(bulk=False):
print(caption.raw())
# save captions to a CSV
voc_caption_data.train.caption.to_csv("train_captions.csv")
Visualize
from voc_tools.visulizer import from_jpeg, see_jpeg
jpeg = from_jpeg(r"sixray_data\train\JPEGImages\P00002.jpg")
jpeg.see()
# OR
see_jpeg(r"sixray_data\train\JPEGImages\P00002.jpg")
Load not PascalVOC dataset
import pathlib
from voc_tools.constants import VOC_IMAGES
from voc_tools.reader import list_dir
from voc_tools.utils import Dataset, VOCDataset
dataset = pathlib.Path(r"path/to/dataset")
# set global flags
Dataset.IMAGE_DIR = "images" # Say, instead of 'JPEGImages', the images are stored in 'images' directory
# reading filepaths
file_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' directory
voc_data = VOCDataset(dataset, caption_support=True)
Collaborate
GitHub: https://github.com/Redcof/pascal_voc_tools.git
Build and Publish
python setup.py sdist bdist_wheel
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for express-pascal-voc-tools-0.7.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17b1da7ea2abfdfa0ddc8c515075daef88807b15fb5d1dc0b46d0035198a48e5 |
|
MD5 | 96b9b1c1903ae588b7245d0e6f72ee9b |
|
BLAKE2b-256 | d7c733818aa9d7c20e029451268e74aec32258a61e98db891a0ed550377e721c |
Close
Hashes for express_pascal_voc_tools-0.7.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fc92a4a3d4e88fb88e3b3c313f98badae0e94907a062d8aaff1659e6be70165 |
|
MD5 | c4e0b24753eb1c622eb22299cb7f08aa |
|
BLAKE2b-256 | 2e49dd680fae53c7d79741f232e6858f809b72d78b7465ceb5429059bb3765f2 |