Last active
September 29, 2021 05:09
-
-
Save dusskapark/3f023460ba4d84cf5f177e386a2ce6fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
from xml.etree.ElementTree import Element, SubElement | |
def beautify(elem, indent=0): | |
""" | |
xml 트리를 문자열로 변환합니다. | |
Converts the XML tree to a string. | |
:param elem: xml element | |
:param indent: Indent Level to display in front | |
""" | |
result0 = f"{' ' * indent}<{elem.tag}>" | |
# 값이 있는 태그면 값을 바로 출력 | |
# If there is a value in the tag, immediately print the value | |
if elem.text is not None: | |
result0 += elem.text + f"</{elem.tag}>\n" | |
# 값이 없고 자식 노드가 있으면 재귀 호출로 출력합니다. | |
# If the tag has no value and has child nodes, call the recursive function. | |
else: | |
result0 += "\n" | |
for _child in elem: | |
result0 += beautify(_child, indent + 1) | |
result0 += f"{' ' * indent}</{elem.tag}>\n" | |
return result0 | |
def isvalidbdnbox(xmin,xmax,ymin,ymax): | |
""" | |
Validate the bnd box. | |
다중 if를 추가해서 bnd 박스를 검증해야 합니다. | |
""" | |
# bounding box(bndbox)가 음수이면 안됩니다. | |
# bounding box(bndbox) cannot be negative. | |
if(xmin<0 or xmax<0 or ymin<0 or ymax<0): | |
print("bndbox cannot be negative.") | |
return False | |
# bndbox의 xmax 값은 1440, ymax 값은 2560을 넘으면 안됩니다. | |
# The xmax value of bndbox cannot exceed 1440 and the ymax value cannot exceed 2560. | |
if(xmax>1440 or ymax>2560): | |
print("bndbox cannot exceed the screenshot") | |
return False | |
# xmin 값은 xmax 보다 클 수 없습니다. 또는 ymin 값은 ymax 보다 클 수 없습니다. | |
# xmin cannot be greater than xmax or ymin cannot be greater than ymax. | |
if(xmin>xmax or ymin>ymax): | |
print("xmin>xmax or ymin>ymax") | |
return False | |
return True | |
def recursive(child, result_out): | |
""" | |
원하는 역할을 하기 위해서 재귀호출을 할 수 있는 함수를 생성합니다. | |
Create a function that makes a recursive call. | |
""" | |
obj = Element("object") | |
# Set bounds | |
bounds = child['bounds'] | |
# Set name | |
SubElement(obj, "name").text = child['componentLabel'] | |
# Set difficult | |
SubElement(obj, "difficult").text = '0' | |
# Set bndbox | |
bndbox = SubElement(obj, "bndbox") | |
xmin=bounds[0] | |
ymin=bounds[1] | |
xmax=bounds[2] | |
ymax=bounds[3] | |
if(isvalidbdnbox(xmin,xmax,ymin,ymax)): | |
SubElement(bndbox, "xmin").text = str(xmin) | |
SubElement(bndbox, "ymin").text = str(ymin) | |
SubElement(bndbox, "xmax").text = str(xmax) | |
SubElement(bndbox, "ymax").text = str(ymax) | |
result_out.append(beautify(obj)) | |
# 생성한 object 태그를 문자열로 변환해서 추가합니다. | |
# Convert the created object tag to a string and add it. | |
if 'children' not in child: | |
return | |
# 자식 노드가 있는 경우 자식 노드에 대해 재귀 호출을 수행합니다. | |
# If there is a child node, make a recursive call to the child node. | |
for ch in child.get('children', []): | |
recursive(ch, result_out) | |
def json2xml(infile, outfile): | |
""" | |
json2xml function | |
param infile : | |
ourfile : | |
""" | |
result_out = [] | |
imgName = infile.replace("./json/", "") | |
imgName = imgName.replace("json", "jpg") | |
# Read the file | |
with open(infile, "r", encoding="UTF-8") as f: | |
data = json.load(f) | |
children = data['children'] | |
# 자식 노드에 대해 재귀 호출을 수행합니다. | |
# Make a recursive call on child nodes. | |
for child in children: | |
recursive(child, result_out) | |
# 그리고 해당 결과를 파일로 저장합니다. | |
# And save the result to the XML file. | |
with open(outfile, "w", encoding="UTF-8") as f: | |
f.write("".join("<annotation><folder />")) | |
f.write("".join("<filename>" + imgName + "</filename>\n" | |
"<path>" + imgName + "</path>\n"+ | |
"<source><database>RICO</database></source><size><width>1440</width><height>2560</height><depth>3</depth></size><segmented>0</segmented>")) | |
f.write("".join(result_out)) | |
f.write("".join("</annotation>")) | |
def search(mypath): | |
onlyfiles = [f for f in os.listdir(mypath) | |
if os.path.isfile(os.path.join(mypath, f))] | |
onlyfiles.sort() | |
return onlyfiles | |
def main(): | |
""" | |
메인 함수 | |
The main function | |
""" | |
files = search("json") | |
# print(files) | |
for infile in files: | |
infile = f'./json/{infile}' | |
outfile = infile.replace("json", "xml") | |
print(infile, outfile) | |
json2xml(infile, outfile) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment