예제 #1
0
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)
예제 #2
0
 def _loc_updator(self):
     if self.cur >= self.length:
         self.cur = 0
     elif self.cur < 0:
         self.cur = self.length - 1
     self.loc = self.path_list[self.cur]
     self.ID = get_name(self.loc)
예제 #3
0
 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)
예제 #4
0
    def metric(self,
               img_p: str,
               label_p: str = None,
               show_info=False,
               show_img=True,
               dst='.',
               name=None,
               show_mask=False,
               target=15,
               label_points=None) -> None:
        """true_positives = Correct objects
        false_positives = Missed objects
        false_negatives = Extra objects
        (PPV), Precision = Σ True positive / Σ Predicted condition positive
        Sensitivity, probability of detection = Σ True positive / Σ Condition positive
        If it is not form dir, we show the outcome directly"""
        if not name:
            name = get_name(img_p)
        if show_info:
            print(name)

        img = cv2.imread(img_p)
        pred_cnts, pred_mask_img = self._mask_creator(img)
        mask_img = None
        if isinstance(label_points, list):
            label_points = label_points
        elif label_p:
            mask_img = cv2.imread(label_p, 0)
        possible_right_cnts, label_num = overlap(pred_cnts,
                                                 mask_img=mask_img,
                                                 label_points=label_points)

        if show_img:
            fit_out = mask_visualization(img,
                                         possible_right_cnts,
                                         method='box',
                                         target=target)
            cv2.imwrite(os.path.join(dst, name + '_fit.jpg'), fit_out)
            pred_out = mask_visualization(img, pred_cnts, target=target)
            cv2.imwrite(os.path.join(dst, name + '_pred.jpg'), pred_out)
        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)
예제 #5
0
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 train_test_info_creator(label_dir: str,
                            tiles_dir: str,
                            dst: str,
                            percent: float = 0.2,
                            test_dir=None) -> None:
    """label_dir is the directory where point labelled images are;
    tiles_dir is the directory where all tiles are, including 'raw_imgs' and 'labels';
    dst is the directory where the 'test.pkl' and the 'others.pkl' will be saved"""
    all_names = names_creator(label_dir)
    if test_dir:
        test_names = [get_name(fn) for fn in os.listdir(test_dir)]
    else:
        test_names = names_separator(all_names, percent=percent)
    test_family, other_family = from_names_to_path_list(test_names, tiles_dir)
    save_dict(test_family, 'test', dst)
    save_dict(other_family, 'others', dst)
    return len(other_family['labels'])
예제 #7
0
 def predict_from_imgs(self,
                       img_p_list,
                       result_dst,
                       visualize_dst=None,
                       show_mask=False,
                       target=15,
                       assistance=False):
     # self.result.clear()
     for img_p in img_p_list:
         img = cv2.imread(img_p)
         ID = get_name(img_p)
         if ID in self.result:
             continue
         self.predict_from_img(img,
                               ID,
                               visualize_dst=visualize_dst,
                               show_mask=show_mask,
                               target=target,
                               assistance=assistance)
     show_result(self.result, ['Counts'], title='Count', dst=result_dst)
예제 #8
0
def test_dict(img_dir:str) -> list:
    return {get_name(fp):fp for fp in path_list_creator(img_dir)}
def names_creator(dir_path):
    return [get_name(fn) for fn in os.listdir(dir_path)]