def sort_by_QR(self, input_path, output_path, make_sorted_directory=True): if len(os.listdir(output_path)) > 0: return error_checker.NONEMPTY_FOLDER_ERROR image_folders = os.listdir(input_path) num_folders = len(image_folders) for index in range(num_folders): image_folder_name = image_folders[index] image_folder_path = tools.make_path(directory_path=input_path, name=image_folder_name) list_of_image_filenames = os.listdir(image_folder_path) sorted_filename = get_folder_QR(filenames=list_of_image_filenames, folder_filepath=image_folder_path, num_folders=num_folders, folder_name=image_folder_name, folder_index=index) if sorted_filename == None: print("Folder number " + image_folder_name + " is Empty") elif make_sorted_directory: output_path = tools.make_path(output_path, sorted_filename) shutil.copytree(image_folder_path, output_path) else: continue return ""
def combine(self, secondary_data_path, main_data_path): main_files = os.listdir(main_data_path) secondary_files = os.listdir(secondary_data_path) new_start = len(main_files) + 1 num_of_new_files = len(secondary_files) for index in range(num_of_new_files): old_filename = secondary_files[index] old_filepath = tools.make_path(secondary_data_path, old_filename) new_filename = make_filename(index + new_start) new_filepath = tools.make_path(main_data_path, new_filename) shutil.copyfile(old_filepath, new_filepath)
def display_image_by_index(self, input_path, folder_index, image_index=0): folders = os.listdir(input_path) folder_name = folders[folder_index] image_folder_filepath = tools.make_path(directory_path=input_path, name=folder_name) filenames = os.listdir(image_folder_filepath) image_name = filenames[image_index] image_filepath = tools.make_path(directory_path=image_folder_filepath, name=image_name) image = cv2.imread(filename=image_filepath, flags=cv2.IMREAD_GRAYSCALE) cropped_image = tools.crop(image=image, x_top_crop=X_TOP_CROP, y_top_crop=Y_TOP_CROP, x_bottom_crop=X_BOTTOM_CROP, y_bottom_crop=Y_BOTTOM_CROP)
def generate_data(image_folder_path, data_tracker): filterer = filtering.Filterer(data_tracker) detec = detector.Detector(data_tracker) max_int_proj_filepath = tools.make_path(SORTING_FOLDER_PATH, MAX_INTENSITY_PROJ_FILENAME) filterer.generate_max_intensity_proj(image_folder_path=image_folder_path, output_path=max_int_proj_filepath) initial_crop, water, root_images = detec.locate_interest_regions( max_int_proj_filepath) roots = filterer.trim_for_data_capture(initial_crop, water, root_images, image_folder_path) data_packages = {} for root_num in roots.keys(): root = roots[root_num] if root.exists: data = filterer.convert_to_data(root.image) curve_pair = data_manager.find_root_curves(data, root.hrs_per_pixel) else: curve_pair = data_manager.no_root() additional_stats = data_manager.generate_additional_stats(curve_pair) data_packages[root_num] = data_manager.DataPackage( curve_pair, additional_stats) #plotter.graph_curves(all_curves) return data_packages
def merge_data(main_data_path, secondary_data_path): main_image_folders = os.listdir(main_data_path) secondary_image_folders = os.listdir(secondary_data_path) num_of_main_image_folders = len(main_image_folders) num_of_secondary_image_folders = len(secondary_image_folders) for folder_index_secondary in range(num_of_secondary_image_folders): folder_name_secondary = secondary_image_folders[folder_index_secondary] folder_path_secondary = tools.make_path(secondary_data_path, folder_name_secondary) share_name = False for folder_index_main in range(num_of_main_image_folders): folder_name_main = main_image_folders[folder_index_main] folder_path_main = tools.make_path(main_data_path, folder_name_main) if folder_name_main == folder_name_secondary: share_name = True combine(secondary_data_path=folder_path_secondary, main_data_path=folder_path_main) if not share_name: new_path = tools.make_path(main_data_path, folder_name_secondary) shutil.copytree(folder_path_secondary, new_path) return ""
def get_folder_QR(filenames, folder_filepath, num_folders, folder_name, folder_index): QR_codes = [] num_files = len(filenames) taken = [] empty_count = 0 for i in range(SAMPLE_SIZE): rand_index = 0 while rand_index in taken: rand_index = int(np.random.uniform(num_files - 1)) taken.append(rand_index) image_name = filenames[rand_index] image_filepath = tools.make_path(directory_path=folder_filepath, name=image_name) image = cv2.imread(filename=image_filepath, flags=cv2.IMREAD_GRAYSCALE) cropped_image = tools.crop(image=image, x_top_crop=X_TOP_CROP, y_top_crop=Y_TOP_CROP, x_bottom_crop=X_BOTTOM_CROP, y_bottom_crop=Y_BOTTOM_CROP) retval, cropped_image = cv2.threshold(cropped_image, thresh=BLACK_THRESHOLD, maxval=PIXEL_MAX, type=cv2.THRESH_BINARY) try: code = QR_read.read_barcode(img=cropped_image) QR_codes.append(code) # print("YEET: QR code of image " + image_name + " in " + folder_name + " displaying") except: middle_strip = find_middle_strip(image) if tools.is_blank(middle_strip): empty_count += 1 else: # print("ERROR: QR code of image " + image_name + " (image index " + str( # rand_index) + ") in " + folder_name + # " (folder index " + str(folder_index) + ") not displaying") rand_code = np.random.uniform(num_folders, num_folders + QR_RANDOMIZER_MAX) while rand_code in QR_codes: rand_code = np.random.uniform( num_folders, num_folders + QR_RANDOMIZER_MAX) QR_codes.append(int(rand_code)) if empty_count == SAMPLE_SIZE: return None try: QR_code = stats.mode(QR_codes) print("Folder number " + str(folder_name) + " has QR code " + str(QR_code)) return QR_code except: rand_index = int(np.random.uniform(0, len(QR_codes) - 1)) QR_code = QR_codes[rand_index] print("Folder number " + str(folder_name) + " is faulty and has been given random QR_code " + str(QR_code)) return QR_code
def find_sprout_index(self, interest_region, image_folder_path): image_names = os.listdir(image_folder_path) natsort.natsorted(image_names, reverse=False) for index in range(len(image_names)): name = image_names[index] path = tools.make_path(image_folder_path, name) image = cv2.imread(path, cv2.IMREAD_GRAYSCALE) region = cropper.crop(image, crop=interest_region).image just_below_seed = cropper.crop(region, y_bottom_crop=int( .95 * region.shape[0])).image if not tools.is_blank(just_below_seed, percentage=.99): return index return 0
def call(self, args): path_in = args[0] path_out = args[1] num_boxes = args[2] project_root = os.path.dirname(os.path.abspath(__file__)) intermediate_directory = tools.make_path(project_root, INTERMEDIATE_FILENAME) os.mkdir(intermediate_directory) error_message = SortImgToFolders().call( [path_in, intermediate_directory, num_boxes]) if error_message is not "": return error_message error_message = QR_handler.sort_by_QR(path_in=intermediate_directory, path_out=path_out) os.remove(intermediate_directory) return error_message
def generate_max_intensity_proj(self, image_folder_path, output_path): files = os.listdir(image_folder_path) natsort.natsorted(files, reverse=False) first_iteration = True max_index = len(files) for index in range(max_index): file = files[index] filepath = tools.make_path(image_folder_path, file) current_image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) if first_iteration: max_projection = current_image first_iteration = False else: max_projection=np.maximum(max_projection, current_image) self.data_tracker.save_and_show(image=max_projection, filename=MAX_INTENSITY_PROJ_FILENAME, caption="Max intensity projection") return max_projection
from src.engine.functions.root_analyzer.detector import Detector from src.tools.back_end import file_toolkit as tools, cropper, models import numpy as np MINUTES_PER_IMAGE = 15 IMAGES_PER_HOUR = 60/MINUTES_PER_IMAGE IMGS_IN_24_HRS = int(IMAGES_PER_HOUR*24) MAX_IMAGES = IMGS_IN_24_HRS*2 CURRENT_PATH = os.path.abspath(__file__) PARENT_PATH = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir)) FUNCTION_PATH = os.path.abspath(os.path.join(PARENT_PATH, os.pardir)) MAX_INTENSITY_PROJ_FILENAME = "max_intensity.jpg" SORTING_FOLDER_NAME = "data" SORTING_FOLDER_PATH = tools.make_path(FUNCTION_PATH, SORTING_FOLDER_NAME) MAX = 255 class Filterer: def __init__(self, data_tracker): self.data_tracker = data_tracker self.detector = Detector(data_tracker) def convert_to_data(self, image): retval, image = cv2.threshold(image, 80, MAX, type=cv2.THRESH_BINARY) root_contour, junk_contours = self.detector.get_contour_info(image) image = self.detector.remove_contours(image, junk_contours) return image
import os from src.engine.functions.root_analyzer import filtering, data_manager import src.engine.functions.root_analyzer.detector as detector import src.tools.back_end.file_toolkit as tools from src.user_interface import plotter CURRENT_PATH = os.path.abspath(__file__) PARENT_PATH = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir)) DATA_MANAGER_PATH = os.path.abspath(os.path.join(PARENT_PATH, os.pardir)) MAX_INTENSITY_PROJ_FILENAME = "max_intensity.jpg" SORTING_FOLDER_NAME = "data" SORTING_FOLDER_PATH = tools.make_path(DATA_MANAGER_PATH, SORTING_FOLDER_NAME) MAX_INTENSITY_PROJ_FILENAME = "max_intensity.jpg" LINES_FILENAME = "lines.jpg" LINES_FILEPATH = tools.make_path(SORTING_FOLDER_PATH, LINES_FILENAME) SEEDS_FILENAME = "seeds.jpg" SEEDS_FILEPATH = tools.make_path(SORTING_FOLDER_PATH, SEEDS_FILENAME) def generate_data(image_folder_path, data_tracker): filterer = filtering.Filterer(data_tracker) detec = detector.Detector(data_tracker) max_int_proj_filepath = tools.make_path(SORTING_FOLDER_PATH, MAX_INTENSITY_PROJ_FILENAME) filterer.generate_max_intensity_proj(image_folder_path=image_folder_path, output_path=max_int_proj_filepath) initial_crop, water, root_images = detec.locate_interest_regions( max_int_proj_filepath) roots = filterer.trim_for_data_capture(initial_crop, water, root_images,
def save_and_show(self, image, filename, caption=""): filepath = tools.make_path(tools.SORTING_FOLDER_PATH, filename) cv2.imwrite(filename=filepath, img=image) self.set_data(filename=filepath, caption=caption)