Git Product home page Git Product logo

xmltojson_mask_rcnn's Introduction

XML to JSON converter and polygon shape builder for MASK-RCNN

Annotation builder to use segmentation in Mask_RCNN, even if your annotations are rectangular instead of polygon. A false masks does not help to be more precise, as it is not providing new information beyond the bounding boxes. Generates the polygon shape between the four points of the rectangle in order to achieve the polygon shape.

Requirements

  • Python >=3.6

Usage

  • /dataset
    • /train
    • /val
  • main.py

$ python main.py

How should your XML be?

You can check this xml and see the result JSON.

<annotation>
	<folder>keuring</folder>
	<filename>IMG_20180413_091455.jpg</filename>
	<path>D:\Seafile\Archive\Projects\125 IWAUC\IMG_20180413_091455.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>3968</width>
		<height>2976</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>SD</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>2241</xmin>
			<ymin>997</ymin>
			<xmax>2713</xmax>
			<ymax>1325</ymax>
		</bndbox>
	</object>
</annotation>

to achieve this

{
    "IMG_20180413_091455.jpg": {
        "filename": "IMG_20180413_091455.jpg",
        "regions": {
            "0": {
                "name": "polygon",
                "region_attributes": {
                    "name": "SD"
                },
                "shape_attributes": {
                    "all_points_x": [
                        2241, # xmin
                        2477, # X
                        2713, # xmax
                        2713, # xmax
                        2713, # xmax
                        2477, # X
                        2241, # xmin
                        2241, # xmin
                        2241  # xmin
                    ],
                    "all_points_y": [
                        997,  #ymin
                        997,  #ymin
                        997,  #ymin
                        1161, #Y
                        1325, #ymax
                        1325, #ymax
                        1325, #ymax
                        1161, #Y
                        997   #ymin
                    ]
                }
            }
        },
        "size": 1231831
    }
}

keyworks

xml, json, converterter, xml to json, convertidor de xml a json, mask rcnn

X = xmin + ((xmax-xmin)/2)
Y = ymin + ((ymax-ymin)/2)

The way to create a false mask for MaskRCNN with annotations made with LabelImg.

Author

xmltojson_mask_rcnn's People

Contributors

adions025 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xmltojson_mask_rcnn's Issues

integerating single XML file into the code

@adions025 , how and where I have to make changes in the code if I only have a single xml file containing information about all the annotations?

Kindly, it will be very helpful if specific changes in the code are mentioned. Thanks

Assertion error-- Path file issue

while trying to convert my xml file to json I am getting the following error:

Traceback (most recent call last):
File "main.py", line 82, in
remove_file(file_train)
File "/home/hiwi/Auto-Annotate/xml2VIA/util.py", line 67, in remove_file
assert isfile(file_name), "-- check your path file"
AssertionError: -- check your path file

I have also editted the main.py file path accordingly like this:

`"""
converterXMLtoJSON

Detect annotations in xml files (bounding boxes) and
converts to json (polygon shape).

@author Adonis Gonzalez

"""
from os.path import dirname, realpath
import xml.etree.cElementTree as et
from util import *
import json

Paths

ROOT_DIR = '/path/to/xmlfile/train.xml'
data_dir = '/path/to/images'
train_dir ='/path/to/train'
#val_dir = join(data_dir, "val")

Files to create, just path definition

filename_json = "VIA4Annotate.json"
file_train = join(train_dir, filename_json)
#file_val = join(val_dir, filename_json)

def convert_xml_to_json(path: str, image_list: list):
"""
Convert from xml to json. For each img is necessary
a xml file annotation. This function save json file.

:param path: A str like /to/path/file
:param image_list: A list of images in the same path
"""
all_json = {}

for img in image_list:
    name_xml = img.split('.jpg')[0] + '.xml'
    images = ({"filename": img})
    root = et.ElementTree(file=join('/path/to/xml file', train.xml)).getroot()
    obj_counter, regi = {}, {}
    number = 0
    for child_of_root in root:
        if child_of_root.tag == 'object':
            for child_of_object in child_of_root:
                if child_of_object.tag == 'name':
                    obj_id = child_of_object.text.split(' ')[0]  # cause some <name>SD 1<name>, just use SD
                    obj_counter[obj_id] = number
                if child_of_object.tag == 'bndbox':
                    for child_of_root in child_of_object:
                        if child_of_root.tag == 'xmin':
                            x_min = int(child_of_root.text)
                        if child_of_root.tag == 'xmax':
                            x_max = int(child_of_root.text)
                        if child_of_root.tag == 'ymin':
                            y_min = int(child_of_root.text)
                        if child_of_root.tag == 'ymax':
                            y_max = int(child_of_root.text)

            x_value, y_value = calculate_xy(x_max, x_min, y_max, y_min)
            coord = get_points(x_max, x_min, y_max, y_min, x_value, y_value)

            regions = ({"region_attributes": {"name": obj_id}})
            regions.update({"shape_attributes": coord})
            regions.update({"name": "polygon"})
            regi[number] = regions.copy()
            regions = {"regions": regi}
            images.update(regions)
            images.update({"size": os.path.getsize(join(path, img))})
            all_json[img] = images.copy()
            number += 1

out_file = open(join(path, filename_json), "a")
json.dump(all_json, out_file)
print("File dataset.json was save in: ", path)

if name == "main":
# I convert for both train and val dataset annotation.
# If you just have a folder, just comment step for val.
# If not file dataset.json, just comment next two lines.
remove_file(file_train)
#remove_file(file_val)

# Grab images and save a log in both train and val
images_train = save_images_log(train_dir)
#images_val = save_images_log(val_dir)

# Convert from xml to json in both train and val
convert_xml_to_json(train_dir, images_train)
#convert_xml_to_json(val_dir, images_val)

# json1 = json.dumps(read_json(train_dir, "dataset.json"), sort_keys=True)
# json2 = json.dumps(read_json(train_dir, "dataset_good.json"), sort_keys=True)
# if json1 == json2:
#     print("Equals!")`

Can you please tell me what to do? Thanks.

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.