- 1 year ago
How assign each folder's label for all image files in it and then give it to model in Python
Comment
1 / 1
To assign labels to image files in each folder and pass them to a model in Python, you can follow these steps:
- Import the required libraries:
pythonimport os
import glob
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
- Define the root directory where your image folders are located:
pythonroot_dir = 'path/to/root/directory'
- Get the list of subdirectories (image folders) in the root directory:
pythonsub_dirs = [sub_dir for sub_dir in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, sub_dir))]
- Iterate over each subdirectory and assign labels to image files:
pythonlabels = []
image_paths = []
for label, sub_dir in enumerate(sub_dirs):
dir_path = os.path.join(root_dir, sub_dir)
file_paths = glob.glob(os.path.join(dir_path, '*.jpg')) # Adjust the file extension based on your image files
labels.extend([label] * len(file_paths))
image_paths.extend(file_paths)
In this example, we assume that the image files have the '.jpg' extension. Adjust the file extension in the glob.glob
line according to your image file types (e.g., '.png', '.jpeg', etc.).
- Encode the labels using
LabelEncoder
:
pythonlabel_encoder = LabelEncoder()
encoded_labels = label_encoder.fit_transform(labels)
- Split the data into training and testing sets using
train_test_split
:
pythonX_train, X_test, y_train, y_test = train_test_split(image_paths, encoded_labels, test_size=0.2, random_state=42)
Adjust the test_size
parameter according to the desired split ratio between the training and testing sets.
- Pass the image paths and labels to your model for further processing and training.
By following these steps, you can assign labels to image files in each folder and prepare the data for passing it to a machine learning model in Python.