Reputation: 51
I am having trouble converting a bunch of XML files to csv for an object detection project. I was following this link https://github.com/wagonhelm/TF_ObjectDetection_API/blob/master/ChessObjectDetection.ipynb
imagepath = os.path.join(r'C:\Users\snehal\Desktop\JPEGImages')
labelpath = os.path.join(r'C:\Users\snehal\Desktop\XMLAnnotations')
def xml_to_csv(path):
xml_list=[]
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
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)
return xml_df
def main():
for i in imagepath:
path = i
folder = os.path.basename(os.path.normpath(i))
xml_df = xml_to_csv(path)
xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)
print('Converted')
main()
The expected output was supposed to be a lot of csv files in a folder but instead I get an error:
runfile('C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py', wdir='C:/Users/snehal/Desktop/Ansh stuff/Object detection')
Traceback (most recent call last):
File "<ipython-input-40-caaa983ccae1>", line 1, in <module>
runfile('C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py', wdir='C:/Users/snehal/Desktop/Ansh stuff/Object detection')
File "C:\Users\snehal\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 826, in runfile
execfile(filename, namespace)
File "C:\Users\snehal\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py", line 48, in <module>
main()
File "C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py", line 45, in main
xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)
AttributeError: 'NoneType' object has no attribute 'to_csv'
How do I fix this?
Upvotes: 0
Views: 263
Reputation: 3845
I think it may be to do with the line folder = os.path.basename(os.path.normpath(i))
. The value of i
may not be an actual file path, or you may not have permission to write to it.
You can print out the value of i
to clarify.
So when the code execution gets to xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)
the value of folder
is None
and so execution cannot continue.
Upvotes: 1