-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_features_extract.py
44 lines (36 loc) · 1.32 KB
/
image_features_extract.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
from keras.applications.vgg19 import VGG19
from keras import models
from keras import layers
from keras.preprocessing import image
import numpy as np
import os
import json
import matplotlib.pyplot as plt
class MyImageDataExtractor:
def __init__(self, file_path, target_size = None, ids = None, _max_example = 0, batch_size = 20):
self.index= 0
self.batch_size = batch_size
self.num_of_examples = _max_example
self.target_size = target_size
self.file_path = file_path
self.ids = ids
self.load_images_labels()
def load_images_labels(self):
images_dict = {}
# images = []
for image_id in self.ids:
image_name = image_id + '.jpg'
img = image.load_img(os.path.join(self.file_path, image_name), target_size = self.target_size)
# plt.imshow(img)
# plt.show()
img_tensor = image.img_to_array(img)
img_tensor /= 255.
images_dict[image_id] = img_tensor
self.images_dict = images_dict
def generate(self):
image_features = []
for id_index in self.ids:
image_feature = self.images_dict.get(id_index)
image_features.append(image_feature)
image_features = np.array(image_features)
return image_features