Created
November 24, 2018 13:58
-
-
Save alpercalisir/568190a5e55a79e08be318c285688457 to your computer and use it in GitHub Desktop.
Creates PASCAL VOC formatted XML given a csv file
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 pandas as pd | |
import numpy as np | |
from lxml import etree | |
import xmlAnnotation.etree.cElementTree as ET | |
fields = ['NAME_ID', 'XMIN', 'YMIN', 'W', 'H', 'XMAX', 'YMAX'] | |
df = pd.read_csv('loose_bb_test.csv', usecols=fields) | |
# Change the name of the file. | |
# This will replace the / with - | |
def nameChange(x): | |
x = x.replace("/", "-") | |
return x | |
df['NAME_ID'] = df['NAME_ID'].apply(nameChange) | |
for i in range(0, 2): | |
height = df['H'].iloc[i] | |
width = df['W'].iloc[i] | |
depth = 3 | |
annotation = ET.Element('annotation') | |
ET.SubElement(annotation, 'folder').text = 'images' | |
ET.SubElement(annotation, 'filename').text = str(df['NAME_ID'].iloc[i]) | |
ET.SubElement(annotation, 'segmented').text = '0' | |
size = ET.SubElement(annotation, 'size') | |
ET.SubElement(size, 'width').text = str(width) | |
ET.SubElement(size, 'height').text = str(height) | |
ET.SubElement(size, 'depth').text = str(depth) | |
ob = ET.SubElement(annotation, 'object') | |
ET.SubElement(ob, 'name').text = 'face' | |
ET.SubElement(ob, 'pose').text = 'Unspecified' | |
ET.SubElement(ob, 'truncated').text = '0' | |
ET.SubElement(ob, 'difficult').text = '0' | |
bbox = ET.SubElement(ob, 'bndbox') | |
ET.SubElement(bbox, 'xmin').text = str(df['XMIN'].iloc[i]) | |
ET.SubElement(bbox, 'ymin').text = str(df['YMIN'].iloc[i]) | |
ET.SubElement(bbox, 'xmax').text = str(df['XMAX'].iloc[i]) | |
ET.SubElement(bbox, 'ymax').text = str(df['YMAX'].iloc[i]) | |
fileName = str(df['NAME_ID'].iloc[i]) | |
tree = ET.ElementTree(annotation) | |
tree.write(fileName + ".xml", encoding='utf8') |
@imadgohar happy to hear that.
Yes, I am trying to convert my csv file to xml format.
…On Wed, Aug 10, 2022, 5:02 PM Laudari Sudip ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi @kunaljain0 <https://github.com/kunaljain0> , are you trying to
convert your csv to xml?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/568190a5e55a79e08be318c285688457#gistcomment-4262110>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AK5Y3FWCQA2DTSKQLQKKS63VYNVZPANCNFSM4M64SIYQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Hi, recently received few emails regarding conversion. If you are still facing problem in csv to xml conversion, please raise an issue here. I would be happy to help.
https://github.com/Laudarisd/Data_preprocessing/blob/860d76b0e12576aaf0333ba215bf6f9d628201e5/csv_to_voc_xml.py
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Laudarisd,
Your code worked for me. Thanks