rct_dataset / create_json.py
frutiemax's picture
Add script to append rows not present in existing metadata.csv
a368945
raw
history blame
No virus
1.46 kB
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')