Search code examples
pythonimagefeature-extraction

Python : Get features of several images


I would like to get the feature of a several images located in the same folder.

My codes are as follow - Prerequisites (librairies needed):

import numpy as np
from PIL import Image
import glob
import cv2
import os

Definition of folder where are located the images (around 6000)

images_dir = "TrainImages"

Creation of a function that defines the different variables et compute them

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

Creation of a variable that scans and analyses the images

image_files = [x.path for x in os.scandir(images_dir)]

Creation of a loop function

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])))

Running the stuff

image_features_list = get_data_from_image(image_files[i])

Look at the output

image_features_list

The output provides only the feature of one image, instead of all images located in the folder

[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

After some commments from friendly persons, here is an additional information for those who would be interested by the response : The output to look at is mylist.

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]]]

Thanks for your help. It is a great forum here !


Solution

  • 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)