Created
May 7, 2021 11:18
-
-
Save PranjalDureja0002/3ad90e8036fbc060c4912dd0d967953d to your computer and use it in GitHub Desktop.
xml_parse
This file contains hidden or 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
res_dim = 1024 | |
if __name__ == "__main__": | |
"""loading the data, | |
reading the file annotations, | |
appending the tabular coordinates to formulate a dataframe | |
""" | |
df_org = pd.DataFrame() | |
directory = '/content/drive/MyDrive/data_cs2' | |
final_col_directory = '/content/drive/MyDrive/cs2_col' | |
final_table_directory = '/content/drive/MyDrive/cs2_table' | |
for file in os.listdir(directory): | |
filename = os.fsdecode(file) | |
if filename.endswith(".xml"): | |
filename = filename[:-4] | |
#https://docs.python.org/3/library/xml.etree.elementtree.html | |
tree = ET.parse(directory +'//'+ filename + '.xml') | |
root = tree.getroot() | |
f_name = root.find('./filename').text | |
depth = root.find('./size/depth').text | |
height = root.find('./size/height').text | |
width = root.find('./size/width').text | |
# Empty lists to append the coordinates as shown in the previous sample case | |
xmin=[] | |
ymin=[] | |
xmax=[] | |
ymax=[] | |
for i in root.findall('object'): | |
bndbox = i.find('bndbox') | |
xmin_ = int(bndbox.find('xmin').text) | |
xmin.append(xmin_) | |
xmax_ = int(bndbox.find('xmax').text) | |
xmax.append(xmax_) | |
ymin_ = int(bndbox.find('ymin').text) | |
ymin.append(ymin_) | |
ymax_ = int(bndbox.find('ymax').text) | |
ymax.append(ymax_) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment