I would like to get the feature of a several images located in the same folder.
import numpy as np
from PIL import Image
import glob
import cv2
import os
images_dir = "TrainImages"
def get_data_from_image(image_path):
cv_img = cv2.imread(image_path)
(means, stds) = cv2.meanStdDev(cv_img)
stats = np.concatenate([means, stds]).flatten()
image_features_list = [stats.tolist()]
return image_features_list
image_files = [x.path for x in os.scandir(images_dir)]
i = 0
mylist =[]
for i in range (4): # I test only 4 images, could be more
mylist.append((get_data_from_image(image_files[i])))
image_features_list = get_data_from_image(image_files[i])
image_features_list
[Out]:
[[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]
I would be grateful if I could have a solution on how to have the features of all images (not only one). At this effect, do not hesitate to correct the code. Thanks and kindest regards
mylist
[Out]:
[[[144.28788548752834, 151.28145691609978, 148.6195351473923, 51.50620316379085, 53.36979275398226, 52.2493589172815]], [[56.220865079365076, 59.99653968253968, 60.28386507936508, 66.72797279655177, 65.24673515467009, 64.93141350917332]], [[125.2066064453125, 118.1168994140625, 145.0827685546875, 68.95463582009148, 52.65138276425348, 56.68269683130363]], [[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]]
Try this approach and tell me if its successful
import os, os.path
import numpy as np
from PIL import Image
import cv2
def get_data_from_image(image_path):
cv_img = cv2.imread(image_path)
(means, stds) = cv2.meanStdDev(cv_img)
stats = np.concatenate([means, stds]).flatten()
image_features_list = [stats.tolist()]
return image_features_list
images_dir = 'C:\\Users\\User\\Directory\\TrainImages\\'
images_names = []
with os.scandir(images_dir) as dirs:
for entry in dirs:
images_names.append(entry.name)
for image in images_names:
path = images_dir + image
image_features_list = get_data_from_image(path))
print(image_features_list)