This is how I solved it.
Step 1: XML to CSV format
### xml to csv
import cv2
import os
import pandas as pd
import xml.etree.ElementTree as ET
def xml2csv(xml_path):
"""Convert XML to CSV
Args:
xml_path (str): Location of annotated XML file
Returns:
pd.DataFrame: converted csv file
"""
print("xml to csv {}".format(xml_path))
xml_list = []
xml_df=pd.DataFrame()
try:
tree = ET.parse(xml_path)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
except Exception as e:
print('xml conversion failed:{}'.format(e))
return pd.DataFrame(columns=['filename,width,height','class','xmin','ymin','xmax','ymax'])
return xml_df
Calling function to get the converted CSV
xml_path='/path/to/sample_annotation.xml'
xml_csv=xml2csv(xml_path)
Intermediate CSV looks like this:
filename width height class xmin ymin xmax ymax
0 sample_annotation.jpg 720 540 cube 90 87 196 194
1 sample_annotation.jpg 720 540 cube 498 188 607 296
Step 2: CSV to LabelMe JSON
import cv2
import numpy as np
import os
import json
import pandas as pd
import base64
def df2labelme(symbolDict,image_path,image):
""" convert annotation in CSV format to labelme JSON
Args:
symbolDict (dataframe): annotations in dataframe
image_path (str): path to image
image (np.ndarray): image read as numpy array
Returns:
JSON: converted labelme JSON
"""
try:
symbolDict['min']= symbolDict[['xmin','ymin']].values.tolist()
symbolDict['max']= symbolDict[['xmax','ymax']].values.tolist()
symbolDict['points']= symbolDict[['min','max']].values.tolist()
symbolDict['shape_type']='rectangle'
symbolDict['group_id']=None
height,width,_=image.shape
symbolDict['height']=height
symbolDict['width']=width
encoded = base64.b64encode(open(image_path, "rb").read())
symbolDict.loc[:,'imageData'] = encoded
symbolDict.rename(columns = {'class':'label','filename':'imagePath','height':'imageHeight','width':'imageWidth'},inplace=True)
converted_json = (symbolDict.groupby(['imagePath','imageWidth','imageHeight','imageData'], as_index=False)
.apply(lambda x: x[['label','points','shape_type','group_id']].to_dict('r'))
.reset_index()
.rename(columns={0:'shapes'})
.to_json(orient='records'))
converted_json = json.loads(converted_json)[0]
except Exception as e:
converted_json={}
print('error in labelme conversion:{}'.format(e))
return converted_json
Calling JSON converter
image_path='path/to/sample_annotation.jpg'
image=cv2.imread(image_path)
csv_json=df2labelme(xml_csv,image_path,image)
Final output
{'imageData': '/9j/2w.........../9k=',
'imageHeight': 540,
'imagePath': 'sample_annotation.jpg',
'imageWidth': 720,
'shapes': [{'group_id': None,
'label': 'cube',
'points': [[90, 87], [196, 194]],
'shape_type': 'rectangle'},
{'group_id': None,
'label': 'cube',
'points': [[498, 188], [607, 296]],
'shape_type': 'rectangle'}]}