mirror of
https://github.com/RustyRaptor/keras-corn.git
synced 2026-12-03 15:15:19 -07:00
24 KiB
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
[0;31m---------------------------------------------------------------------------[0m [0;31merror[0m Traceback (most recent call last) [0;32m<ipython-input-45-787c39198ca2>[0m in [0;36m<module>[0;34m[0m [1;32m 2[0m [0;34m[0m[0m [1;32m 3[0m [0ma[0m[0;34m,[0m[0mb[0m [0;34m=[0m [0;34m'images_handheld'[0m[0;34m,[0m [0;34m'fixed_handheld'[0m[0;34m[0m[0;34m[0m[0m [0;32m----> 4[0;31m [0ma[0m [0;34m=[0m [0mread_all_images[0m[0;34m([0m[0ma[0m[0;34m,[0m[0mb[0m[0;34m,[0m[0mx[0m[0;34m=[0m[0;36m256[0m[0;34m,[0m [0my[0m[0;34m=[0m[0;36m256[0m[0;34m,[0m [0mend[0m[0;34m=[0m[0;36m10[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 5[0m [0mplt[0m[0;34m.[0m[0mimshow[0m[0;34m([0m[0mnp[0m[0;34m.[0m[0msqueeze[0m[0;34m([0m[0ma[0m[0;34m,[0m [0maxis[0m[0;34m=[0m[0;36m0[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;32m<ipython-input-40-b90a0841e43c>[0m in [0;36mread_all_images[0;34m(image_folder_name, csv_name, x, y, start, end, mute)[0m [1;32m 22[0m [0mimage[0m [0;34m=[0m [0mcv2[0m[0;34m.[0m[0mimread[0m[0;34m([0m[0mimage[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 23[0m [0;32mif[0m [0mx[0m [0;34m!=[0m [0;32mNone[0m [0;32mand[0m [0my[0m [0;34m!=[0m [0;32mNone[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;32m---> 24[0;31m [0mimage[0m [0;34m=[0m [0mcv2[0m[0;34m.[0m[0mresize[0m[0;34m([0m[0mimage[0m[0;34m,[0m [0;34m([0m[0my[0m[0;34m,[0m [0mx[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 25[0m [0mimage_list[0m[0;34m.[0m[0mappend[0m[0;34m([0m[0mimage[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 26[0m [0;32mif[0m [0mmute[0m [0;34m==[0m [0;36m0[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;31merror[0m: 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, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1,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