0

I'm attempting to run a CNN model with raster files for environmental observations and I'm trying to import a folder of tiff files as the input for the model. I'm relatively new to python and ML and need help with reading the folder of rasters as a 3d numpy array. Below code has been my attempt at reading the folder with gdal:

flix = r'/Desktop/x'
fliy = r'/Desktop/y'

# in_directory = r'C:\Data'
files_to_process = glob(os.path.join(flix, '*.tif')

for data_path in files_to_process:
    raster_dataset = gdal.Open(data_path, gdal.GA_ReadOnly)

Note: I somehow am receiving a syntax error at the for loop definition itself as well.

2
  • You are missing a closing bracket ) on the files_to_process = glob(os.path.join(flix, '*.tif') line, hence the SyntaxError. To read into a numpy array, use raster_dataset.ReadAsArray() gdal.org/api/python/osgeo.gdal.html
    – user2856
    Commented Mar 21, 2023 at 8:37
  • What does the input images look like, RGB, Greyscale, multispectral, hyper-spectral?
    – PyMapr
    Commented Mar 28, 2023 at 14:30

1 Answer 1

0

As per the comment of @user2856.

flix = r'/Desktop/x'
fliy = r'/Desktop/y'

# in_directory = r'C:\Data'
files_to_process = glob(os.path.join(flix, '*.tif')) # closing bracket for glob()

for data_path in files_to_process:
    raster_dataset = gdal.Open(data_path, gdal.GA_ReadOnly)
    array = raster_dataset.ReadAsArray() # This will read the file as a numpy array. It will also overwrite the array with each iteration of the loop.

You will also need to read the label (fliy) and match it to the corresponding image (flix).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.