File size: 1,458 Bytes
fcbbb8b a368945 fcbbb8b a368945 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import os
import pandas
from pathlib import Path
import string
import shutil
# read all the folders in data/train
data_folder = Path("./data/train")
folders = os.listdir(data_folder)
entries = []
image_index = 0
for folder in folders:
images_path = data_folder.joinpath(folder)
images = os.listdir(images_path)
view = 0
for image in images:
object_description = folder.replace('_', ' ')
entry = {'file_name' : folder + '/' + image, 'object_type' : 'small_scenery', 'object_description' : object_description, 'view': view, 'color1' : 'none', 'color2' : 'none', 'color3' : 'none'}
entries.append(entry)
view = view + 1
#put the entries into the metadata.csv
dataframe = pandas.DataFrame(entries)
# dont overwrite the old metadata.csv
if os.path.exists('metadata.csv'):
shutil.copyfile('metadata.csv', 'metadata_backup.csv')
# read the existing metadata.csv and add only the rows that do not exist
output_dataframe = pandas.read_csv('metadata.csv')
# drop the rows where the object description exists
obj_descs = output_dataframe['object_description']
for obj_desc in obj_descs:
dataframe = dataframe.drop(dataframe[dataframe['object_description'] == obj_desc].index)
output_dataframe = pandas.concat([output_dataframe, dataframe]).drop_duplicates().reset_index(drop=True)
output_dataframe.to_csv('metadata.csv')
else:
dataframe.to_csv('metadata.csv') |