Giter Site home page Giter Site logo

alek-dr / pascalvoc Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 1.43 MB

Python utility to work with PascalVoc annotation format

License: MIT License

Python 100.00%
pascal-voc labelme-json labelme annotations labelimg yoloformat annotation annotationview dataset annotation-processing

pascalvoc's Introduction

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

pascalvoc's People

Contributors

alek-dr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pascalvoc's Issues

negative bndbox values are loaded as string

When loading annotations from xml with the annotation_from_xml function the library loads negative bndbox values as type. This seems wrong and causes issues when using other functions like draw_boxes.

The xml shouldn't have negative values but it happens with some labeling tools. In my case i used https://www.makesense.ai/

Example voc_xml:

<annotation>
	<folder>my-project-name</folder>
	<filename>test.jpg</filename>
	<path>/my-project-name/test.jpg</path>
	<source>
		<database>Unspecified</database>
	</source>
	<size>
		<width>355</width>
		<height>539</height>
		<depth>3</depth>
	</size>
	<object>
		<name>cat1</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>-2</xmin>
			<ymin>-3</ymin>
			<xmax>355</xmax>
			<ymax>333</ymax>
		</bndbox>
	</object>
	<object>
		<name>cat1</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>230</xmin>
			<ymin>441</ymin>
			<xmax>336</xmax>
			<ymax>525</ymax>
		</bndbox>
	</object>
</annotation>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.