tayyardurden commited on
Commit
27d72f1
1 Parent(s): 949a4bc

Upload preprocessing.py

Browse files
Files changed (1) hide show
  1. preprocessing.py +59 -0
preprocessing.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def load_dataset_images(directory):
2
+ import pickle
3
+ import os
4
+ import cv2
5
+ import numpy as np
6
+
7
+ # Set the paths to the dataset folders
8
+ dataset_dir = "general"
9
+ train_dir = os.path.join(dataset_dir, "PANTOLON")
10
+
11
+ # Set the image size to be used as the input data for the network
12
+ image_size = (224, 224)
13
+
14
+ preprocessed_images_file = os.path.join(directory, 'preprocessed_images.pkl')
15
+ labels_file = os.path.join(directory, 'labels.pkl')
16
+ filenames_file = os.path.join(directory, 'filenames.pkl')
17
+
18
+ # Check if preprocessed images, labels and filenames already exist
19
+ if os.path.exists(preprocessed_images_file) and os.path.exists(labels_file) and os.path.exists(filenames_file):
20
+ # Load preprocessed images, labels and filenames from disk
21
+ with open(preprocessed_images_file, 'rb') as f:
22
+ images = pickle.load(f)
23
+ with open(labels_file, 'rb') as f:
24
+ labels = pickle.load(f)
25
+ with open(filenames_file, 'rb') as f:
26
+ filenames = pickle.load(f)
27
+ else:
28
+ # Preprocess images, labels and filenames and save them to disk
29
+ images = []
30
+ labels = []
31
+ filenames = []
32
+
33
+ for filename in os.listdir(directory):
34
+ if filename.endswith(".txt"):
35
+ with open(os.path.join(directory, filename), "r", encoding="utf-8") as file:
36
+ product = file.readline().strip().replace("Product: ", "")
37
+ labels.append(product)
38
+
39
+ if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(
40
+ ".jpeg") or filename.endswith(".png"):
41
+ img_path = os.path.join(directory, filename)
42
+ img = cv2.imread(img_path)
43
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
44
+ img = cv2.resize(img, image_size)
45
+ img = img.astype("float32") / 255.0 # Normalize the pixel values
46
+ images.append(img)
47
+ filenames.append(filename)
48
+
49
+ images = np.array(images)
50
+
51
+ # Save preprocessed images, labels and filenames to disk
52
+ with open(preprocessed_images_file, 'wb') as f:
53
+ pickle.dump(images, f)
54
+ with open(labels_file, 'wb') as f:
55
+ pickle.dump(labels, f)
56
+ with open(filenames_file, 'wb') as f:
57
+ pickle.dump(filenames, f)
58
+
59
+ return images, labels, filenames