def load_dataset_images(directory): import pickle import os import cv2 import numpy as np # Set the paths to the dataset folders dataset_dir = "general" train_dir = os.path.join(dataset_dir, "PANTOLON") # Set the image size to be used as the input data for the network image_size = (224, 224) preprocessed_images_file = os.path.join(directory, 'preprocessed_images.pkl') labels_file = os.path.join(directory, 'labels.pkl') filenames_file = os.path.join(directory, 'filenames.pkl') # Check if preprocessed images, labels and filenames already exist if os.path.exists(preprocessed_images_file) and os.path.exists(labels_file) and os.path.exists(filenames_file): # Load preprocessed images, labels and filenames from disk with open(preprocessed_images_file, 'rb') as f: images = pickle.load(f) with open(labels_file, 'rb') as f: labels = pickle.load(f) with open(filenames_file, 'rb') as f: filenames = pickle.load(f) else: # Preprocess images, labels and filenames and save them to disk images = [] labels = [] filenames = [] for filename in os.listdir(directory): if filename.endswith(".txt"): with open(os.path.join(directory, filename), "r", encoding="utf-8") as file: product = file.readline().strip().replace("Product: ", "") labels.append(product) if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith( ".jpeg") or filename.endswith(".png"): img_path = os.path.join(directory, filename) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, image_size) img = img.astype("float32") / 255.0 # Normalize the pixel values images.append(img) filenames.append(filename) images = np.array(images) # Save preprocessed images, labels and filenames to disk with open(preprocessed_images_file, 'wb') as f: pickle.dump(images, f) with open(labels_file, 'wb') as f: pickle.dump(labels, f) with open(filenames_file, 'wb') as f: pickle.dump(filenames, f) return images, labels, filenames