Skip to content

Commit

Permalink
add CVAT XML to PS2.0 JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
aimuch committed Apr 21, 2021
1 parent 7d48bee commit 61f5248
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cvat2psJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import shutil
import json
import xml.dom.minidom
from numpy.lib.function_base import delete
from tqdm import tqdm

point_type = {"T":0, "L":1}
slot_type = {"vertical":1, "perpendicular":1}
point_type = {"T":0,"U":0, "V":0, "ST":0, "L":1, "SL":1}
slot_type = {"vertical":0, "slanted":0, "perpendicular":1, "horizontal":1}

def parse_args():
parser = argparse.ArgumentParser(description='Convert CVAT labels to PS2.0 JSON')
Expand Down Expand Up @@ -46,13 +47,19 @@ def __init__(self, srcLabelPath, srcJpgPath, dstJsonPath, dstJpgPath, roiShape)
self.usingROI = True
self.roiXbias = 0
self.roiYbias = 0
self.logPath = "./log.txt"
self.logWriter = open(self.logPath, "w")
print("init done!")

def __del__(self):
self.logWriter.close()

def run(self):
cvatXmlList = os.listdir(self.srcLabelPath)
for xml in cvatXmlList:
xml_path = os.path.join(self.srcLabelPath, xml)
self.cvat2json(xml_path)
self.logWriter.flush() # every xml flush IO buffer

def write_json(self, name, data) -> None:
"""
Expand All @@ -79,7 +86,7 @@ def cvat2json(self, xml_path) -> None:
# ---- An xml file in the cvat annotation file corresponds to a picture of a video ---- #
img_list = annotation.getElementsByTagName('image')

for img in img_list:
for img in tqdm(img_list):
imageID = img.getAttribute("id")
imageName = img.getAttribute("name")

Expand All @@ -97,6 +104,7 @@ def cvat2json(self, xml_path) -> None:
picturepath = os.path.join(self.srcJpgPath, imageName)
dstImgPath = os.path.join(self.dstJpgPath, imageName)
if not os.path.exists(picturepath):
self.logWriter.write(picturepath + "\n")
print(picturepath, " is not exists!")
continue
imgSrc = cv2.imread(picturepath)
Expand Down Expand Up @@ -178,7 +186,6 @@ def cvat2json(self, xml_path) -> None:
os.makedirs(dstJpgPath)
print("%s is not exists, it has created!"%dstJpgPath)

roiShape = (-1, -1)
instance = cvat2jsons(srcLabelPath, srcJpgPath, dstJsonPath, dstJpgPath, roiShape)
instance.run()
print("Done!")

0 comments on commit 61f5248

Please sign in to comment.