import os import shutil import pandas as pd # Define the image folder and CSV files image_folder = "images/" train_csv = 'train_caption.csv' val_csv = 'validation_caption.csv' test_csv = 'test_caption.csv' # Load the CSV files train_df = pd.read_csv(train_csv) val_df = pd.read_csv(val_csv) test_df = pd.read_csv(test_csv) # Create directories for train, test, validation images train_dir = 'images/train/' val_dir = 'images/validation/' test_dir = 'images/test/' os.makedirs(train_dir, exist_ok=True) os.makedirs(val_dir, exist_ok=True) os.makedirs(test_dir, exist_ok=True) # Define a function to copy images to the respective folder def copy_images(df, destination_folder): for file_name in df['file_name']: # Construct the destination path dest_path = os.path.join(destination_folder, os.path.basename(file_name)) # Copy the image if os.path.exists(file_name): shutil.copy(file_name, dest_path) # Copy the images based on the CSVs copy_images(train_df, train_dir) copy_images(val_df, val_dir) copy_images(test_df, test_dir) # Load the CSV files # train_df = pd.read_csv('train.csv') # val_df = pd.read_csv('validation.csv') # test_df = pd.read_csv('test.csv') # # Update file_name with respective image directories # train_df['file_name'] = 'images/train/' + train_df['file_name'].apply(os.path.basename) # val_df['file_name'] = 'images/validation/' + val_df['file_name'].apply(os.path.basename) # test_df['file_name'] = 'images/test/' + test_df['file_name'].apply(os.path.basename) # # Combine all three datasets # combined_df = pd.concat([train_df, val_df, test_df], ignore_index=True) # # Save the combined dataset # combined_df.to_csv('metadata.csv', index=False)