<jupyter_start><jupyter_text><jupyter_code>import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET


import torch
from IPython.display import Image, clear_output  # to display images
#from utils.google_utils import gdrive_download  # to download models/datasets


def xml_to_csv(path):
    xml_list = []
    for xml_file in glob.glob(path):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            bbx = member.find('bndbox')
            xmin = int(bbx.find('xmin').text)
            ymin = int(bbx.find('ymin').text)
            xmax = int(bbx.find('xmax').text)
            ymax = int(bbx.find('ymax').text)
            #label = member.find('name').text

            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     #label,
                     [xmin,
                     ymin,
                     xmax,
                     ymax],
                     root.find('source')[0].text
                     )
            xml_list.append(value)
    column_name = ['image_id', 'width', 'height',
                   'bbox' , 'source']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df
%cd ..
!rm -rf *
%cd /content/
!mkdir train
%cd /content/
!gdown --id 1T-ys3R8ousn3G-PEDA-AVqe0l02D6ZnY
!unzip Dataset_all_jpg.zip; rm Dataset_all_jpg.zip;


import os
os.getcwd()
datasets = 'Dataset_all_jpg'
path = os.path.join(os.getcwd(), datasets , "*.xml")
all_xml = glob.glob(path)
len(all_xml)
# 231.xml has error in parsing as it was not stored in VOC XML format
df = pd.DataFrame()
for i in all_xml:
    try:
        xml_df  = xml_to_csv(i)
        xml_df["image_id"] = i
        df = df.append(xml_df, ignore_index = True)
    except:
        continue
# EOL error if you put \ at the end of a string. Replacing it with \\ will work fine
left = "/content/Dataset_all_jpg/" 
df['image_id'] = df['image_id'].map(lambda x: x.lstrip(left).rstrip('.xml')) + ".jpg"
df.drop("image_id",axis=1, inplace = True)
df.head(n=100)
# checking
df["image_id"].nunique()
# convert to csv 
df.to_csv("dhaka_ai.csv", index=False)
from google.colab import drive
drive.mount('/content/gdrive')
!cp /content/dhaka_ai.csv  My\ Drive/efficientDet
%cd /content/Dataset_all_jpg
%ls<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13536026655560182, 'fraction_numerical': 0.007496876301541024, 'mean_word_length': 2.4119318181818183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 19}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '155292', 'license': 'MIT', 'n_tokens_mistral': 851, 'n_tokens_neox': 826, 'n_words': 184, 'path': 'VOC_XML_to_csv_For_Dhaka_AI.ipynb', 'repo_name': 'AIFahim/VOC_XML_to_CSV', 'size': '2401'}