-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriplet_Generator.py
49 lines (40 loc) · 1.43 KB
/
Triplet_Generator.py
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
import glob
from keras.utils import Sequence
import cv2
import numpy as np
import tenserflow as tf
import os
class MyDataGenerator(Sequence):
def __init__(self, image_path, label_path, batch_size, Test=False):
self.image_path = image_path
self.label_path = label_path
self.batch_size = batch_size
self.Test = Test
def __get_images__(self):
self.images_path = glob.glob(os.path.join(self.image_path, "*.jpg"))
np.random.shuffle(self.images_path)
return self.images_path
def __len__(self):
return len(self.image_paths) // self.batch_size
def __getitem__(self, index):
# Get current batch of image paths and labels
batch_paths = self.image_paths[index * self.batch_size:(index + 1) * self.batch_size]
# Load and pre-process images
batch_images = []
for path in batch_paths:
image = cv2.imread(path)
# if Testing show the images
if self.Test:
cv2.imshow("image", image)
image = tf.resize(
images=image,
size=[224, 224],
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
preserve_aspect_ratio=False,
antialias=False,
name=None
)
batch_images.append(image)
# Convert to NumPy arrays
batch_images = np.array(batch_images)
return batch_images