Spaces:
Sleeping
Sleeping
File size: 2,409 Bytes
27d72f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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 |