trainmodel | Cell 1 | Cell 3 | Search

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.

Cell 2

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

What the code could have been:

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

Function: createdataframe

Parameters

Returns

Description

This 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.

Implementation