How to read multiple images from multiple folders in python

aaku

I am writing an image recognition program in python 3.6 for which I am using anaconda.I have my image data set stored in the location E:\food-101\images in which the 'images' folder contains multiple sub folders which contain the photographs. I want to use these images for training my machine learning model.I am able to load(read) a single image stored in E:\ I want to load multiple images from the above path how do I proceed ?I am using opencv. my code is as follows any help is appreciated

import cv2
import matplotlib
import numpy
img = cv2.imread("E:\food\images\chicken_wings\a.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I get the following error

Traceback (most recent call last):

  File "<ipython-input-8-6310b5f3028e>", line 5, in <module>
    cv2.imshow('image',img)

error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\highgui\src\window.cpp:356: error: (-215) size.width>0 && size.height>0 in function cv::imshow


Traceback (most recent call last):

  File "<ipython-input-8-6310b5f3028e>", line 5, in <module>
    cv2.imshow('image',img)

error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\highgui\src\window.cpp:356: error: (-215) size.width>0 && size.height>0 in function cv::imshow 
D.Griffiths

An easy way would be to install Glob. You can do this from the anaconda prompt with pip install glob.

Glob allows you to query files like you would in a terminal. Once you have Glob you could save all of the file names to a list and then loop through this list reading your images (NumPy arrays) into a new list.

import cv2
import numpy
import glob

folders = glob.glob('path\\to\\folder\\containing\\folders\\*')
imagenames_list = []
for folder in folders:
    for f in glob.glob(folder+'/*.jpg'):
        imagenames_list.append(f)

read_images = []        
for image in imagenames_list:
    read_images.append(cv2.imread(image, cv2.IMREAD_GRAYSCALE))

You could then access an image by indexing it i.e.

plt.imshow(read_images[0])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read Images from multiple folders?

How to read multiple images from two different folders one by one using python?

Open/read multiple .tif images from multiple folders on different levels

How to read multiple images in Python?

Read and extract multiple PDF's from multiple folders using python

Insert multiple images from multiple folders in VBA

Displaying random images from multiple folders using python

read multiple text files from multiple folders

How to read a specific paragraph from from multiple folders and files

how to combine multiple CSV files from multiple folders in Python?

Combining multiple images, in many folders in Python

Read multiple images and labels in python

Reading images in multiple folders

Read *.csv as list from multiple folders

How to read multiple lists from STDIN in python?

How to copy images from multiple sub-folders to a common folder and rename them?

Combining images into video with ffmpeg, but from multiple folders into the same frames

Matlab: Accessing images from multiple folders in single loop

Read from an Excel spreadsheet, and rename multiple folders with data from XLSX

How do i make multiple folders with multiple subfolders from two lists in python

How to remove dot from multiple folders

How to import tags from multiple folders?

How to compare multiple screenshots from two folders

How to read multiple images and store them on MATLAB?

How to delete files from multiple folders excluding couple of folders?

how to make multiple folders in python at once for windows

Reading multiple CSV files from multiple folders in Python?

Read multiple folders files java

Azure Data Factory Read Csv Files from multiple portioned folders