Having error "name 'file' is not defined" in Xml to CSV conversion

I used the code that Pulkit Sir has provided in one blog-

import os, sys, random
import xml.etree.ElementTree as ET
from glob import glob
import pandas as pd
from shutil import copyfile

annotations = glob(‘C:\Users\BCCD_Dataset-master\BCCD_Dataset-master\BCCD\Annotations*.xml’)

df =
cnt = 0
for file in annotations:
prev_filename = file.split(’/’)[-1].split(’.’)[0] + ‘.jpg’
filename = str(cnt) + ‘.jpg’
row =
parsedXML = ET.parse(file)
for node in parsedXML.getroot().iter(‘object’):
blood_cells = node.find(‘name’).text
xmin = int(node.find(‘bndbox/xmin’).text)
xmax = int(node.find(‘bndbox/xmax’).text)
ymin = int(node.find(‘bndbox/ymin’).text)
ymax = int(node.find(‘bndbox/ymax’).text)

row = [prev_filename, filename, blood_cells, xmin, xmax,ymin, ymax]
cnt += 1

data = pd.DataFrame(df, columns=[‘prev_filename’, ‘filename’, ‘cell_type’,‘xmin’, ‘xmax’, ‘ymin’, ‘ymax’])

data[[‘filename’, ‘cell_type’, ‘xmin’, ‘xmax’, ‘ymin’, ‘ymax’]].to_csv(‘C:\Users\BCCD_Dataset-master\BCCD_Dataset-master\BCCD\csv.csv’,index=None)

It is showing error as - name ‘file’ is not defined

© Copyright 2013-2019 Analytics Vidhya