def point_creator(refer_dir:str, name:str, dst:str, point_label=True, direct_mask=False, limit_pkl:str=None) -> None: """the refer_dir is the directory where all refer are; the name is the name of the created .pkl file; the dst is where the .pkl file will be saved; if the limit_pkl is provided, only the files in limit_pkl will be used, this .pkl files should follow the format of metric_path_collector.py""" containor = {} refers = path_list_creator(refer_dir) if limit_pkl: target = load_pickle(limit_pkl) refers = [refer for refer in refers if get_name(refer) in target['labels']] if point_label: for label_p in refers: points = point_detect(cv2.cvtColor(cv2.imread(label_p), cv2.COLOR_BGR2HSV)) containor[get_name(label_p)] = points elif direct_mask: for mask_p in refers: containor[get_name(mask_p)] = mask_p else: for mask_p in refers: mask = cv2.imread(mask_p, 0) cnts = mask_to_cnts_watershed(mask, min_distance=14, for_real_mask=True) points = [middle(c) for c in cnts] containor[get_name(mask_p)] = points save_dict(containor, name, dst)
def _temp_init(self): img_dir = filedialog.askdirectory( title='Select the directory where your slides are ...', mustexist=True) self.dst = join(dirname(img_dir), 'temp') os.makedirs(self.dst, exist_ok=True) self.path_list = path_list_creator(img_dir) self.cur = 0 self.length = len(self.path_list) self.loc = self.path_list[self.cur] self.ID = get_name(self.loc)
def make_gif_from_dir(metric_dir_path, dst): maid = defaultdict(dict) path_list = path_list_creator(metric_dir_path) for fp in path_list: name = get_name(fp) if '_pred' in fp: maid[name]['predicted'] = fp elif '_label' in fp: maid[name]['labeled'] = fp # elif '_fit' in fp: # maid[name]['fit'] = fp for f_name, fp_dict in maid.items(): if len(fp_dict) == 2: out_p = join(dst, f_name + '.gif') make_gif(fp_dict, out_p)
def count_from_dir(self, assist=False): if assist: self.is_assist = True else: self.is_assist = False img_dir = filedialog.askdirectory( title='Select the directory where your slides are ...', mustexist=True) self.img_p_list = path_list_creator(img_dir) amount = len(self.img_p_list) time_needed = str(datetime.timedelta(seconds=amount * 30)) self._message_prompt(title='The below is what we know ...', text=f'\n\ The directory you selected is\n\ {img_dir}\n\ {amount} possible slices are there\n\ Depending on your computer, about {time_needed} is needed.\n\ If it is OK, please PRESS "Continue" and then\n\ select a directory to save results.', normal=False)
def test_dict(img_dir:str) -> list: return {get_name(fp):fp for fp in path_list_creator(img_dir)}
def find_models(dir_p:str) -> list: files = [x for x in path_list_creator(dir_p) if x[-3:]=='.h5'] return files
if show_mask: cv2.imwrite(os.path.join(dst, name + '_mask.jpg'), pred_mask_img) values = stats(label_num, pred_cnts, possible_right_cnts) self.metric_record[name] = values if show_info: print_dict = {} print_dict.update(dict(zip(self.metric_keys.split(', '), values))) print(print_dict) def metric_from_dir(self, img_dir_path, label_dir_path, dst): for img_p, label_p, name in path_matcher(img_dir_path, label_dir_path): self.metric(img_p, label_p, show_info=False, dst=dst, name=name) show_result(self.metric_record, self.metric_keys.split(', '), title='Metric', dst=dst) if __name__ == "__main__": model_p = sys.argv[1] img_dir = sys.argv[2] dst = sys.argv[3] actor = Unet_predictor(model_p) os.makedirs(dst, exist_ok=True) try: label_dir = sys.argv[4] actor.metric_from_dir(img_dir, label_dir, dst) except IndexError: img_p_list = path_list_creator(img_dir) actor.predict_from_imgs(img_p_list, dst)