def __init__(self, nodenum, image_source, rate=0.2, randomize_order=True): # where to save photos home_directory = os.path.expanduser( rospy.get_param('/multi_tracker/' + nodenum + '/data_directory')) experiment_basename = rospy.get_param( '/multi_tracker/' + nodenum + '/experiment_basename', 'none') if experiment_basename == 'none': experiment_basename = time.strftime("%Y%m%d_%H%M%S_N" + nodenum, time.localtime()) directory_name = experiment_basename + '_gphoto2' self.destination = os.path.join(home_directory, directory_name) self.image_source = image_source self.rate = rate if os.path.exists(self.destination): pass else: os.mkdir(self.destination) self.images = mta_read.get_filenames(self.image_source, '.jpg') if randomize_order: np.random.shuffle(self.images) self.current_image_index = -1 # initialize the node rospy.init_node('gphoto2_' + nodenum) self.nodename = rospy.get_name().rstrip('/') self.nodenum = nodenum self.pubNewImage = rospy.Publisher( '/multi_tracker/' + str(self.nodenum) + '/gphoto2_images', String)
def get_mean_roi(directory, target_fly_filename=None, mean=np.mean): file_list = mta_read.get_filenames(directory, '.jpg') if target_fly_filename is None: target_fly_filename = file_list[0] target_fly = cv2.imread(target_fly_filename) f = open(os.path.join(directory, 'fly_data.pickle')) fly_datas = pickle.load(f) f.close() aligned_flies = [] for filename in file_list: fly = cv2.imread(filename) ellipse = None i = -1 while ellipse is None: i += 1 fly_data = fly_datas[i] if os.path.basename( fly_data['roi_filename']) == os.path.basename(filename): ellipse = fly_data['ellipse'] aligned_fly = align_two_flies(target_fly, fly, ellipse) aligned_fly_hsv = cv2.cvtColor(aligned_fly, cv2.COLOR_BGR2HSV) aligned_flies.append(aligned_fly_hsv) return mean(aligned_flies, axis=0).astype(np.uint8)
def load_fly_rois(path, color_conversion=None): file_list = mta_read.get_filenames(path, '.jpg') fly_rois = {} for file in file_list: roi = cv2.imread(file) if color_conversion is not None: roi = cv2.cvtColor(roi, color_conversion) fly_rois[file] = roi return fly_rois
def find_best_model_for_outliers(roi_directory, difference_threshold=DIFFERENCE_THRESHOLD): outlier_directory = mta_read.get_filename(roi_directory, 'model_outliers') outlier_filenames = mta_read.get_filenames(outlier_directory, '.jpg') for outlier_filename in outlier_filenames: assign_fly_to_model_and_update_models_from_filenames( roi_directory, outlier_filename, difference_threshold=difference_threshold) os.remove(outlier_filename)
def extract_all_flyimgs(directory, pixels_per_mm=42, width=None): file_list = mta_read.get_filenames(directory, '.jpg') median = create_median_gray_small_image_from_directory(directory) median_large = create_median_image_from_directory(directory) delayed_results = [] for file in file_list: queue = dask.delayed(find_flies_and_load_rois)(directory, file, pixels_per_mm, median, median_large, width=width) delayed_results.append(queue) results = dask.compute(*delayed_results)
def assign_fly_to_model_and_update_models( roi_directory, fly_filename, fly, mean_fly, model_directories, difference_threshold=DIFFERENCE_THRESHOLD): if len(model_directories) > 0: number_of_flies_per_model = [ len(mta_read.get_filenames(model_directory, '.jpg')) for model_directory in model_directories ] for i, N in enumerate(number_of_flies_per_model): if N >= 3: remove_outliers_from_model(model_directories[i], outlier_directory=None) if np.min(number_of_flies_per_model) >= 2: best_model_fit = assign_fly_to_model_std(fly, mean_fly, model_directories, difference_threshold) print 'std method' # else: best_model_fit = assign_fly_to_model(fly, mean_fly, model_directories, difference_threshold) else: best_model_fit = None if best_model_fit is None: # make new model new_model_number = len(model_directories) model_directory = os.path.join(roi_directory, 'model_' + str(new_model_number)) if os.path.exists(model_directory): pass else: os.mkdir(model_directory) aligned_fly = fly else: idx, aligned_fly = best_model_fit model_directory = os.path.join(roi_directory, 'model_' + str(idx)) aligned_fly_bgr = cv2.cvtColor(aligned_fly, cv2.COLOR_HSV2BGR) f = os.path.join(model_directory, os.path.basename(fly_filename)) #cv2.imwrite(f, aligned_fly_bgr) shutil.copyfile(fly_filename, f) if 'outlier' in fly_filename: os.remove(fly_filename)
def create_median_image_from_directory(directory, N=10): # N is the number of equidistant files to use for making the median file_list = mta_read.get_filenames(directory, '.jpg') imgs = [] indices = np.linspace(0,len(file_list)-1,N).astype(int) for i in indices: file = file_list[i] img = dask.delayed(FlyImg(directory, file, pixels_per_mm=42).load_img()) imgs.append(img) imgs = dask.compute(*imgs) median = np.median(imgs, axis=0) return median.astype(np.uint8)
def load_all_flyimgs(flyimgs_directory): flyimgs = [] file_list = mta_read.get_filenames(flyimgs_directory, 'flyimg.pickle') def open_flyimg(file): print('opening file: ', file) f = open(file, 'rb') flyimg = pickle.load(f) f.close() return flyimg for file in file_list: flyimgs.append( dask.delayed(open_flyimg)(file) ) flyimgs = dask.compute(*flyimgs) return flyimgs
def assign_fly_to_model_and_update_models_from_filenames( roi_directory, fly_filename, difference_threshold=DIFFERENCE_THRESHOLD): model_directories = mta_read.get_filenames(roi_directory, 'model_', ['outlier']) mean_fly = get_mean_roi_from_aligned_directory_in_hsv(roi_directory) fly = cv2.imread(fly_filename) fly = cv2.cvtColor(fly, cv2.COLOR_BGR2HSV) assign_fly_to_model_and_update_models( roi_directory, fly_filename, fly, mean_fly, model_directories, difference_threshold=difference_threshold)
def identify_outliers(self): outlier_directory = os.path.join(self.roi_directory, 'model_outliers') if os.path.exists(outlier_directory): outlier_filenames = mta_read.get_filenames(outlier_directory, '.jpg') self.buffer_roi_filenames.extend(outlier_filenames)
def remove_outliers_and_update_all_models(roi_directory): model_directories = mta_read.get_filenames(roi_directory, 'model_', ['outlier']) for model_directory in model_directories: remove_outliers_from_model(model_directory, outlier_directory=None) find_best_model_for_outliers(roi_directory)
fly, mean_fly, model_directories, difference_threshold=difference_threshold) if __name__ == '__main__': parser = OptionParser() parser.add_option("--path", type="str", dest="path", default='', help="directory where images can be found") (options, args) = parser.parse_args() roi_filenames = mta_read.get_filenames(options.path, '.jpg') while len(roi_filenames) > 0: roi_filename = roi_filenames.pop(0) assign_fly_to_model_and_update_models_from_filenames( options.path, roi_filename, difference_threshold=DIFFERENCE_THRESHOLD) outlier_directory = os.path.join(options.path, 'model_outliers') if os.path.exists(outlier_directory): outlier_filenames = mta_read.get_filenames(outlier_directory, '.jpg') roi_filenames.extend(outlier_filenames) #remove_outliers_and_update_all_models(options.path)