The createdataframe
function takes a directory path as input and returns two lists: image_paths
and labels
, containing the paths to images and their corresponding labels, respectively. It traverses the directory structure to extract this information.
def createdataframe(dir):
image_paths = []
labels = []
for label in os.listdir(dir):
for imagename in os.listdir(os.path.join(dir,label)):
image_paths.append(os.path.join(dir,label,imagename))
labels.append(label)
print(label, "completed")
return image_paths,labels
import os
from pathlib import Path
import pandas as pd
def create_dataframe(root_dir):
"""
Create a DataFrame from a directory structure with images and corresponding labels.
Args:
- root_dir (str): The root directory containing the image labels.
Returns:
- image_paths (list): A list of paths to the images.
- labels (list): A list of corresponding labels for the images.
"""
# Get the list of labels
labels = [label for label in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, label))]
# Initialize lists to store image paths and labels
image_paths = []
label_paths = []
# Iterate over each label
for label in labels:
# Get the list of image paths for the current label
for imagename in os.listdir(os.path.join(root_dir, label)):
image_path = os.path.join(root_dir, label, imagename)
image_paths.append(image_path)
label_paths.append(label)
print(label, "completed")
# Create a DataFrame from the image paths and labels
df = pd.DataFrame({'image_path': image_paths, 'label': label_paths})
return image_paths, label_paths, df
createdataframe
dir
: The path to the directory containing images and labelsimage_paths
: A list of paths to the imageslabels
: A list of corresponding labelsThis function traverses a directory structure, extracting paths to images and their corresponding labels. It returns two lists: image_paths
containing the paths to the images and labels
containing the corresponding labels.
image_paths
and labels
lists, respectively.image_paths
and labels
lists.