The following code was used to create JSON. But I have \ in JSON.
"[{\"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}]"
Then I also have "[ at the beginning and ]" at end of file in printing to JSON file. Why I have those at the beginning and end of JSON file and how to remove?
The code is as follow.
import os
import xml.etree.ElementTree as ET
import pickle
from os import listdir, getcwd
from os.path import join
import numpy as np
import cv2
import math
import random
import json
xmlspath="/home/itc/Data/NumberPlate/PlateDetection/annotations/xmls/"
xmlFiles = [f for f in listdir(xmlspath) if f.endswith("." + "xml")];
num_objs = 0
iou = 0.0
num_hits = 0
train = []
val = []
for idx,xmlFile in enumerate(xmlFiles):
if(idx > 10):
break
tree = ET.parse(xmlspath+xmlFile)
root = tree.getroot()
filename = xmlFile.split(".")[0]+".jpeg"
size = root.find("size")
width = int(size.find("width").text)
height = int(size.find("height").text)
depth = int(size.find("depth").text)
for obj in root.iter("object"):
newitem={
"name":{"fileref":"","size":0,"filename":"","file_attributes":{},"regions":{"attribute":{"shape_attributes":{"name":"polygon","all_points_x":[],"all_points_y":[]},"region_attributes":{"width":0,"height":0,"depth":3}}}}
}
newitem[filename]=newitem.pop("name")
newitem[filename]["filename"]=filename
bndbox = obj.find("bndbox")
category = obj.find("name")
xmin=int(bndbox.find("xmin").text)
ymin=int(bndbox.find("ymin").text)
xmax=int(bndbox.find("xmax").text)
ymax=int(bndbox.find("ymax").text)
if(obj.find("name").text == "plate"):
newitem[filename]["regions"]["plate"] = newitem[filename]["regions"].pop("attribute")
newitem[filename]["regions"]["plate"]["region_attributes"]["width"]=width
newitem[filename]["regions"]["plate"]["region_attributes"]["height"]=height
elif(obj.find("name").text == "textline"):
newitem[filename]["regions"]["textline"] = newitem[filename]["regions"].pop("attribute")
newitem[filename]["regions"]["textline"]["region_attributes"]["width"]=width
newitem[filename]["regions"]["textline"]["region_attributes"]["height"]=height
if(idx%8 == 0):
val.append(newitem)
else:
train.append(newitem)
print(idx)
jsontrain = json.dumps(train)
jsonval = json.dumps(val)
with open("numplate/train/train.json", "w") as outfile:
json.dump(jsontrain, outfile)
with open("numplate/val/val.json", "w") as outfile:
json.dump(jsonval, outfile)
dumpanddumpsthe value.[]because you're creating and dumping a list.[]andappendto it in a loop…