def test_mean_color_diffs(self): file_photo = os.path.join( DATA_PATH, "photo/funny-game-of-thrones-memes-fb__700.jpg") im_photo = load_image_by_cv2(file_photo) file_cartoon = os.path.join(DATA_PATH, "cartoon/cartoon_1.jpg") im_cartoon = load_image_by_cv2(file_cartoon) bilateral_filter = BilateralFilter(30, 50, 50) color_diffs_photo = bilateral_filter.mean_color_diffs(im_photo) color_diffs_cartoon = bilateral_filter.mean_color_diffs(im_cartoon) self.assertGreater(color_diffs_photo, color_diffs_cartoon)
def _create_feature_from_dir(self, src_path: str, im_type: str, save_path: str=None): """ Creates pickle file with feature for every example in directory. Parameters the same as in create_features_from_dir. """ self.labels = os.listdir(src_path) self.labels.sort() features = list() for i in range(len(self.labels)): label = self.labels[i] print("Label: {}".format(label)) label_path = os.path.join(src_path, label) if im_type is not None: label_path = os.path.join(label_path, im_type) files = os.listdir(label_path) files.sort() for file_name in tqdm(files): file_path = os.path.join(label_path, file_name) im = load_image_by_cv2(file_path) features.append(self.extraction_pipeline(im)) features = np.array(features) if len(features.shape) == 1: features = np.reshape(features, (-1, 1)) if save_path is not None: self._save_binary(features, os.path.join(save_path, im_type + "_" + self.feature_name))
def test_apply_filter(self): file_photo = os.path.join(DATA_PATH, "base_dataset/photo/pics/pexels-photo-2873992.jpeg") print(file_photo) im_photo = load_image_by_cv2(file_photo) file_cartoon = os.path.join(DATA_PATH, "base_dataset/cartoon/pics/cartoon_1.jpeg") print(file_cartoon) im_cartoon = load_image_by_cv2(file_cartoon) gabor_filter = GaborFilter() photo_feats = gabor_filter.apply_filter(im_photo) cartoon_feats = gabor_filter.apply_filter(im_cartoon) print(photo_feats) print(cartoon_feats) print(np.linalg.norm(photo_feats)) print(np.linalg.norm(cartoon_feats)) self.assertGreater(np.linalg.norm(photo_feats), np.linalg.norm(cartoon_feats))
def test_no_of_bounding_boxes(self): file_name = 'stats.jpg' file_path = os.path.join("base_dataset", "segmentation", file_name) image = load_image_by_cv2(file_path) hl = HoughLines() edges = hl.get_edges(image) im, norm_edges, auto_min_line_len = hl.get_image_with_lines(image, edges) boxes = hl.get_bounding_boxes(im, plot=False) self.assertEqual(len(boxes), 3)
def avg_img_size(src_path, im_type): labels_choice = os.listdir(src_path) labels_choice.sort() sizes = list() x = list() y = list() labels = list() for i in range(len(labels_choice)): label = labels_choice[i] print("Label: {}".format(label)) label_path = os.path.join(src_path, label) if im_type is not None: label_path = os.path.join(label_path, im_type) files = os.listdir(label_path) files.sort() for file_name in tqdm(files): file_path = os.path.join(label_path, file_name) im = load_image_by_cv2(file_path) sizes.append(np.mean(im.shape[0:2])) y.append(im.shape[0]) x.append(im.shape[1]) labels.append(i) return sizes, x, y, labels
""" results = [] for i, a in enumerate(rect_points): results.append([]) for b in approx_points: results[i].append(np.linalg.norm(a - b)) res = [sorted(r)[0] for r in results] return sum(sorted(res)[:-1]) < 15 if __name__ == "__main__": file_name = '12_5' file_path = os.path.join(DATA_PATH, "base_dataset", "segmentation", "tests") filename = glob.glob(f'{file_path}/{file_name}*')[0] loaded_image = load_image_by_cv2(filename) x, y, _ = loaded_image.shape hl = HoughLines() l = None print(loaded_image.shape) sip = SingleImagePlotter() sip.plot_image(loaded_image) print(f'\nMinimum no of points: {"automatic" if l is None else l}') edges = hl.get_edges(loaded_image.copy(), plot=False) im, norm_edges, auto_min_line_len = hl.get_image_with_lines(loaded_image.copy(), edges, l, plot=False) print(f'Normalized edges: {norm_edges}') print(f'Automatic value of min_lines: {auto_min_line_len}')
def hough_tests_for_all_params(plot_stats=True, plot=False, verbose=True): images_path = os.path.join(DATA_PATH, "base_dataset", "segmentation", "tests") file_names_full = glob.glob(images_path + '/*') file_names = [fn.split('/')[-1] for fn in file_names_full] labels = [int(fn.split('.')[-2][-1]) for fn in file_names] if plot_stats: plot_segments_distribution(labels) cv2_images = [] for file_name in file_names_full: image = load_image_by_cv2(file_name) cv2_images.append(image) min_canny_thresholds = [50] # [40, 50, 60] max_canny_thresholds = [180, 185, 190, 195, 200] # [125, 150, 175] min_mask_thresholds = [235] # [235, 240, 245] max_mask_thresholds = [260] # [250, 260, 270] df = pd.DataFrame(columns=[ 'time', 'min_canny', 'max_canny', 'min_mask', 'max_mask', 'auto_fun', 'auto_param1', 'auto_param2', 'num_wrong', 'num_all' ]) timestamp = time.time() for min_ct in min_canny_thresholds: for max_ct in max_canny_thresholds: for min_mask in min_mask_thresholds: for max_mask in max_mask_thresholds: hl = HoughLines(min_ct, max_ct, min_mask, max_mask) cv2_edges = get_edges_for_given_params(cv2_images, hl) funs = [min] params1 = [7, 7.5, 8] # [8, 8.5, 9, 9.5, 10, 10.5, 11] params2 = [0.9] # [0.8, 0.85, 0.9] for fun in funs: fun_name = fun.__name__ for param2 in params2: for param1 in params1: if verbose: print( '------------------------------------') print( f'Params: canny_thresholds = ({hl.min_canny_threshold}, {hl.max_canny_threshold}), ' f'mask_thresholds = ({hl.min_mask_threshold}, {hl.max_mask_threshold}), ' f'function = {fun_name} with MIN_LINE_LEN = {param1} and param2 = {param2}' ) autotunes = count_autotune_param_for_all_edges( cv2_edges, fun=fun, param1=param1, param2=param2) n_wrong, n_all, t = count_wrongly_labeled_images( hl, cv2_images, cv2_edges, autotunes, labels, file_names, verbose=verbose) df = df.append( { 'time': t, 'min_canny': min_ct, 'max_canny': max_ct, 'min_mask': min_mask, 'max_mask': max_mask, 'auto_fun': fun_name, 'auto_param1': param1, 'auto_param2': param2, 'num_wrong': n_wrong, 'num_all': n_all }, ignore_index=True) save_df_to_csv(df, timestamp, min_ct, max_ct, min_mask, max_mask) return df, timestamp