BPMDS / my_bp_dataset.py
AyadSarah's picture
Upload 4 files
8de8cd4 verified
import os
import pandas as pd
from PIL import Image
from datasets import Dataset, DatasetInfo, Features, Value, Image as HFImage
import datasets
def load_bp_dataset(csv_path, image_folder):
# Load CSV file as a DataFrame
df = pd.read_csv(csv_path)
# Create a function to load images based on the paths in the CSV
def load_image(image_path):
return Image.open(os.path.join(image_folder, image_path))
# Add image column to the DataFrame by loading images using paths in 'Image_Path'
df['image'] = df['Image_Path'].apply(load_image)
# Convert the DataFrame into a Hugging Face Dataset
dataset = Dataset.from_pandas(df)
return dataset
if __name__ == "__main__":
# Provide paths to CSV and image folder
csv_path = '/mnt/data/image_metadata.csv' # Path to your CSV file
image_folder = '/mnt/data/BP_images' # The folder where images are stored
# Load dataset
bp_dataset = load_bp_dataset(csv_path, image_folder)
# Display an example to verify everything is loaded correctly
print(bp_dataset[0])
# Optional: Save the dataset to Hugging Face format if needed
bp_dataset.push_to_hub("your_username/your_dataset_name")