Files
keras-corn/irtesting.ipynb

24 KiB

In [39]:
import csv, os
import cv2, h5py
import numpy as np

#  add path for csv of other data sets when using them    
csv_location = './input/fixed_handheld.csv'
image_folder = './input/images_handheld/'
In [40]:
def read_image_data_pairs(csv_name):
    data = []
    csv_file = open('./input/' + csv_name + '.csv')  #  setting up csv reading
    csv_read = csv.reader(csv_file)
    for row in csv_read:  #  read through the csv
        concise_row = [row[3], row[1]] # isolate image ID and blight bool
        data.append(concise_row)  #  add to list for ease of manipulation
    return data

def get_all_Bools(csv_name, start=None, end=None):  #  return list of blight bools in order
    to_bool = {"True":1, "False":0}
    bools = [to_bool[i[0]] for i in read_image_data_pairs(csv_name)]
    return bools[start:end] #  add slice

def read_all_images(image_folder_name, csv_name, x=None, y=None, start=None, end=None, mute=0):  #  returns all images, resized as specified
    image_ID_list = ['./input/' + image_folder_name + str(i[1]) for i in read_image_data_pairs(csv_name)]
    image_ID_list = image_ID_list[start:end]  #  !!remove when using full dataset, this just saves time when iterating through and debugging!!
    image_list = []
    for image in image_ID_list:
        if mute == 0:
            print("Processing images: %.1f%% complete" % (100*image_ID_list.index(image)/len(image_ID_list)), end="\r")
        image = cv2.imread(image)
        if x != None and y != None:
            image = cv2.resize(image, (y, x))
        image_list.append(image)
    if mute == 0:
        print("\rProcessing images: 100% complete ")                                         #  possible future error debug, size given is that of first image,
        print("Processed {} images, sized {}".format(len(image_list), image_list[0].shape))  #  <----- not necessarily of all images
    return image_list

def images_to_hdf5(file_name, data_name="pics", start=None, end=None, mute=0):  # write image data into an hdf5 file to save read time (<1/6 load time from observations)
    '''Use only when creating a new read file for crop data,
    read from existing file otherwise to save time and memory overhead'''
    filepath = './' + str(file_name) + '.hdf5'
    pictures = read_all_images(x=256, y=256, start=start, end=end, mute=mute)
    if os.path.isfile(filepath) == True:
        os.remove(filepath)
    os.mknod(filepath)
    pictures = np.array(pictures).tolist()  #  make into list
    with h5py.File(filepath, 'w') as f:
        f.create_dataset(data_name, data=pictures, dtype="uint8")
        
In [45]:
import matplotlib.pyplot as plt

a,b = 'images_handheld', 'fixed_handheld'
a = read_all_images(a,b,x=256, y=256, end=10)
plt.imshow(np.squeeze(a, axis=0))
Processing images: 0.0% complete
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-45-787c39198ca2> in <module>
      2 
      3 a,b = 'images_handheld', 'fixed_handheld'
----> 4 a = read_all_images(a,b,x=256, y=256, end=10)
      5 plt.imshow(np.squeeze(a, axis=0))

<ipython-input-40-b90a0841e43c> in read_all_images(image_folder_name, csv_name, x, y, start, end, mute)
     22         image = cv2.imread(image)
     23         if x != None and y != None:
---> 24             image = cv2.resize(image, (y, x))
     25         image_list.append(image)
     26     if mute == 0:

error: OpenCV(4.1.0) /home/etv/OpenCV/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
In [44]:
get_all_Bools('fixed_handheld')
Out [44]:
[1,
 1,
 0,
 1,
 0,
 0,
 1,
 1,
 1,
 1,
 0,
 0,
 0,
 0,
 0,
 0,
 1,
 1,
 1,
 0,
 1,
 1,
 0,
 0,
 0,
 1,
 1,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 1,
 1,
 1,
 1,
 0,
 0,
 1,
 0,
 0,
 0,
 0,
 0,
 1,
 0,
 0,
 1,
 1,
 0,
 1,
 0,
 1,
 1,
 1,
 0,
 1,
 1,
 1,
 0,
 1,
 1,
 1,
 0,
 1,
 1,
 0,
 1,
 1,
 1,
 0,
 0,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 0,
 0,
 0,
 1,
 1,
 0,
 0,
 0,
 0,
 0,
 1,
 1,
 0,
 0,
 1,
 0,
 1,
 1,
 0,
 1,
 1,
 1,
 1,
 1,
 0,
 1,
 0,
 0,
 1,
 1,
 1,
 1,
 0,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 0,
 1,
 1,
 1,
 1,
 1,
 0,
 1,
 0,
 1,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