├── README.md ├── json_to_xml └── json_to_xml.py ├── xml_to_csv ├── xml_to_csv.py └── xml_to_csv_multiclass.py ├── xml_to_json └── xml_to_json.py └── xml_to_yolo ├── readme.txt └── xml_to_yolo.py /README.md: -------------------------------------------------------------------------------- 1 | # Data-annotation-format-converter 2 | 3 | After labeling or if you already labeled file like xml. (Now only added xml converter) 4 | And you want to convert one format to another format means like (xml to yolo) format or (xml to csv) or (json to xml). 5 | You can use the above scripts to convert the files. 6 | 7 | 8 | Lable Format Details: 9 | 10 | TXT 11 | 12 | YOLO Darknet 13 | YOLO v3 Keras 14 | YOLO v4 PyTorch 15 | 16 | Tiny YOLO 17 | 18 | XML 19 | 20 | Pascal VOC 21 | Tensorflow Object Detection 22 | 23 | CSV 24 | 25 | Tensorflow Object Detection 26 | Multiclass Classification 27 | 28 | JSON 29 | 30 | COCO 31 | CreateML 32 | Tensorflow Object Detection 33 | 34 | TFRECORD 35 | 36 | Tensorflow Object Detection 37 | -------------------------------------------------------------------------------- /json_to_xml/json_to_xml.py: -------------------------------------------------------------------------------- 1 | 2 | import json as JS 3 | import xml.etree.cElementTree as e 4 | import os 5 | 6 | cwd = os.getcwd() 7 | 8 | l= r"D:\off\final_data\json" 9 | cwd_xml = cwd + "\\" + "xml\\" 10 | for filename in os.listdir(l): 11 | 12 | file = os.path.join(l,filename) 13 | 14 | with open(file, "r") as json_file: 15 | print(filename) 16 | print(filename[:-5]) 17 | # loading json file data 18 | # to variable data 19 | data = JS.load(json_file) 20 | # print(data) 21 | # print(json2xml(data)) 22 | r = e.Element("root") 23 | # e.SubElement(r,"folder").text = d["f older"] 24 | e.SubElement(r,"FileName").text = data[ "FileName"] 25 | # e.SubElement(r,"NumOfAnno").text = str(data["NumOfAnno"]) 26 | # e.SubElement(r,"Annotations").text = str(data["Annotations"]) 27 | 28 | ann = e.SubElement(r,"object") 29 | for z in data["Annotations"]: 30 | e.SubElement(ann,"name").text = z["classname"] 31 | # e.SubElement(ann,"Confidence").text = "Confidence" 32 | # e.SubElement(ann,"xmin").text =str( z["BoundingBox"][0]) 33 | bbox = e.SubElement(ann,"bndbox") 34 | for b in data["Annotations"]: 35 | print(b) 36 | e.SubElement(bbox,"xmin").text =str( b["BoundingBox"][0]) 37 | e.SubElement(bbox,"ymin").text =str( b["BoundingBox"][1]) 38 | e.SubElement(bbox,"xmax").text =str( b["BoundingBox"][2]) 39 | e.SubElement(bbox,"ymax").text =str( b["BoundingBox"][3]) 40 | a = e.ElementTree(r) 41 | f = filename[:-5] 42 | f = f + ".xml" 43 | f = cwd_xml + '\\' + f 44 | a.write(f) -------------------------------------------------------------------------------- /xml_to_csv/xml_to_csv.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import pandas as pd 4 | import xml.etree.ElementTree as ET 5 | 6 | 7 | def xml_to_csv(path): 8 | xml_list = [] 9 | for xml_file in glob.glob(path + '/*.xml'): 10 | tree = ET.parse(xml_file) 11 | root = tree.getroot() 12 | for member in root.findall('object'): 13 | value = (root.find('filename').text, 14 | int(root.find('size')[0].text), 15 | int(root.find('size')[1].text), 16 | member[0].text, 17 | int(member[4][0].text), 18 | int(member[4][1].text), 19 | int(member[4][2].text), 20 | int(member[4][3].text) 21 | ) 22 | xml_list.append(value) 23 | column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'] 24 | xml_df = pd.DataFrame(xml_list, columns=column_name) 25 | return xml_df 26 | 27 | 28 | def main(): 29 | for folder in ['train','test']: 30 | image_path = os.path.join(os.getcwd(), ('images/' + folder)) 31 | xml_df = xml_to_csv(image_path) 32 | xml_df.to_csv(('images/' + folder + '_labels.csv'), index=None) 33 | print('Successfully converted xml to csv.') 34 | 35 | 36 | main() 37 | -------------------------------------------------------------------------------- /xml_to_csv/xml_to_csv_multiclass.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import pandas as pd 4 | import xml.etree.ElementTree as ET 5 | 6 | 7 | def xml_to_csv(path): 8 | xml_list = [] 9 | for xml_file in glob.glob(path + '/*.xml'): 10 | tree = ET.parse(xml_file) 11 | root = tree.getroot() 12 | for member in root.findall('object'): 13 | value = (root.find('filename').text, 14 | int(root.find('size')[0].text), 15 | int(root.find('size')[1].text), 16 | member[0].text, 17 | int(member[5][0].text), 18 | int(member[5][1].text), 19 | int(member[5][2].text), 20 | int(member[5][3].text) 21 | ) 22 | xml_list.append(value) 23 | column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'] 24 | xml_df = pd.DataFrame(xml_list, columns=column_name) 25 | return xml_df 26 | 27 | 28 | # def main(): 29 | # for folder in ['test']: 30 | # image_path = os.path.join(os.getcwd(), ('annotations/' + folder)) 31 | # xml_df = xml_to_csv(image_path) 32 | # xml_df.to_csv(('annotations/' + folder + '_labels.csv'), index=None) 33 | # print('Successfully converted xml to csv.') 34 | 35 | def main(): 36 | image_path = os.path.join(os.getcwd(), 'train_xml') 37 | xml_df = xml_to_csv(image_path) 38 | xml_df.to_csv('train_xml.csv', index=None) 39 | print('Successfully converted xml to csv.') 40 | 41 | main() -------------------------------------------------------------------------------- /xml_to_json/xml_to_json.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import json 4 | import xmltodict 5 | def resizer_fun(location): 6 | l =location 7 | # w = w 8 | # h = h 9 | # print(w,h) 10 | for filename in os.listdir(l): 11 | 12 | file = os.path.join(l,filename) 13 | 14 | with open(file) as xml_file: 15 | 16 | data_dict = xmltodict.parse(xml_file.read()) 17 | xml_file.close() 18 | json_data = json.dumps(data_dict) 19 | # Write the json data to output 20 | # json file 21 | n = filename + ".json" 22 | with open(n, "w") as json_file: 23 | json_file.write(json_data) 24 | json_file.close() 25 | 26 | # print(img) 27 | # for root, directories, files in os.walk(l, topdown=False): 28 | # # for name in files: 29 | # print(root) 30 | # path = os.path.join(root, name) 31 | # rez = cv2.resize(img,(w,h),interpolation=cv2.INTER_AREA) 32 | # #writing the resized image 33 | # n = filename + ".jpg" 34 | # cv2.imwrite(filename,rez) 35 | #loading the resized image and seeing the height & width 36 | # resized = cv2.imread("resized_image.jpg") 37 | # (height,width) = resized.shape[:2] 38 | # print ("Resized Height & Width ---> \n Height : {} Width : {}".format(height,width)) 39 | 40 | -------------------------------------------------------------------------------- /xml_to_yolo/readme.txt: -------------------------------------------------------------------------------- 1 | For xml to yolo you need to specify the classes first in xml_to_yolo.py: 2 | 3 | for eg: 4 | lut["with_mask"] =0 5 | lut["without_mask"] =1 6 | lut["mask_weared_incorrect"] =2 7 | eg 2: 8 | # lut["bag"] =1 9 | # lut["shoes"] =2 10 | 11 | run the xml_to_yolo.py in xml files folder. 12 | -------------------------------------------------------------------------------- /xml_to_yolo/xml_to_yolo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from xml.dom import minidom 4 | import os 5 | import glob 6 | 7 | lut={}#'with_mask','without_mask','mask_weared_incorrect' 8 | lut["with_mask"] =0 9 | lut["without_mask"] =1 10 | lut["mask_weared_incorrect"] =2 11 | # lut["bag"] =3 12 | # lut["shoes"] =4 13 | 14 | 15 | def convert_coordinates(size, box): 16 | dw = 1.0/size[0] 17 | dh = 1.0/size[1] 18 | x = (box[0]+box[1])/2.0 19 | y = (box[2]+box[3])/2.0 20 | w = box[1]-box[0] 21 | h = box[3]-box[2] 22 | x = x*dw 23 | w = w*dw 24 | y = y*dh 25 | h = h*dh 26 | return (x,y,w,h) 27 | 28 | 29 | def convert_xml2yolo( lut ): 30 | 31 | for fname in glob.glob("*.xml"): 32 | 33 | xmldoc = minidom.parse(fname) 34 | 35 | fname_out = (fname[:-4]+'.txt') 36 | 37 | with open(fname_out, "w") as f: 38 | 39 | itemlist = xmldoc.getElementsByTagName('object') 40 | size = xmldoc.getElementsByTagName('size')[0] 41 | width = int((size.getElementsByTagName('width')[0]).firstChild.data) 42 | height = int((size.getElementsByTagName('height')[0]).firstChild.data) 43 | 44 | for item in itemlist: 45 | # get class label 46 | classid = (item.getElementsByTagName('name')[0]).firstChild.data 47 | print("classss idddddddddddddddddd",classid) 48 | if classid in lut: 49 | 50 | label_str = str(lut[classid]) 51 | else: 52 | label_str = "-1" 53 | print ("warning: label '%s' not in look-up table" % classid) 54 | 55 | # get bbox coordinates 56 | xmin = ((item.getElementsByTagName('bndbox')[0]).getElementsByTagName('xmin')[0]).firstChild.data 57 | ymin = ((item.getElementsByTagName('bndbox')[0]).getElementsByTagName('ymin')[0]).firstChild.data 58 | xmax = ((item.getElementsByTagName('bndbox')[0]).getElementsByTagName('xmax')[0]).firstChild.data 59 | ymax = ((item.getElementsByTagName('bndbox')[0]).getElementsByTagName('ymax')[0]).firstChild.data 60 | b = (float(xmin), float(xmax), float(ymin), float(ymax)) 61 | bb = convert_coordinates((width,height), b) 62 | #print(bb) 63 | 64 | f.write(label_str + " " + " ".join([("%.6f" % a) for a in bb]) + '\n') 65 | 66 | print ("wrote %s" % fname_out) 67 | 68 | 69 | 70 | def main(): 71 | convert_xml2yolo( lut ) 72 | 73 | 74 | if __name__ == '__main__': 75 | main() --------------------------------------------------------------------------------