import os
import shutil
import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
def load_sort_data(meta_filename = str, image_folder = str, output_folder = str):
"""[summary]
Args:
meta_filename ([type], optional): [description]. Defaults to str.
image_folder ([type], optional): [description]. Defaults to str.
output_folder ([type], optional): [description]. Defaults to str.
Returns:
[type]: [description]
"""
data_dir = os.getcwd() + "/data/"
dest_dir = data_dir + output_folder
metadata = pd.read_csv(data_dir + '/' + meta_filename)
labels = metadata['dx'].unique()
label_images = []
for i in labels:
if os.path.exists(dest_dir + str(i) + '/'):
shutil.rmtree(dest_dir + str(i) + '/')
os.mkdir(dest_dir + str(i) + '/')
sample = metadata[metadata['dx'] == i]['image_id']
label_images.extend(sample)
for id in label_images:
shutil.copyfile((data_dir + image_folder + '/' + id + '.jpg'), (dest_dir + i + '/' + id + '.jpg'))
label_images = []
return metadata, dest_dir
def transform(path, size = (300, 225)):
# create a list of images
img_list = [fn for fn in os.listdir(path) if fn.endswith('.jpg')]
#iterating over each .jpg
for fn in img_list:
fp = path + '/' + fn
current_image = image.load_img(fp, target_size = size,
color_mode = 'grayscale')
# covert image to a matrix
img_ts = image.img_to_array(current_image)
# turn that into a vector / 1D array
img_ts = [img_ts.ravel()]
try:
# concatenate different images
full_mat = np.concatenate((full_mat, img_ts))
except UnboundLocalError:
# if not assigned yet, assign one
full_mat = img_ts
return full_mat