Python Forum

Full Version: Insert images in a folder into dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

Im trying to read images from folders into a dataframe , where each row in the dataframe is all the images for a folder :

import cv2

import os,glob
import matplotlib.pylab as plt
from os import listdir,makedirs
from os.path import isfile,join
import pandas as pd
import PIL
import numpy as np
from scipy.ndimage import imread

pth = 'C:/Users/sarmad/Documents/n/'

folders =  os.listdir(pth)



videos = pd.DataFrame()
for folder in folders:
    pth_upd = pth + folder + '/'
    #print(pth_upd)
    
    allfiles = os.listdir(pth_upd)
    files = []
    columns = ['data']
    index = [folders]
    for file in allfiles:
        files.append(file) if ('.bmp' in file) else None
    
    samples =  np.empty((0,64,64))
    for file in files:
        img = cv2.imread(os.path.join(pth_upd,file),cv2.IMREAD_GRAYSCALE)
        
        #print(img)
        # read image
        img = img.reshape(1,64,64)            
        samples = np.append(samples, img, axis=0)
        df = pd.DataFrame( index=folders)
        df['img_frames'] = pd.DataFrame([samples],index=folders)

  
I can read all the images in each folder to {samples} list but when I try to insert them into dataframe I get this error :

Quote:ValueError Traceback (most recent call last)
<ipython-input-99-db4b01fe9450> in <module>
20 samples = np.append(samples, img, axis=0)
21 df = pd.DataFrame( index=folders)
---> 22 df['img_frames'] = pd.DataFrame([samples],index=folders)
23
24

ValueError: Must pass 2-d input