def test_shuffle_shreds(self): im_path = os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG') im_grayscaled = cv2.cvtColor(cv2.imread(im_path), cv2.COLOR_RGB2GRAY) t = 5 shuffled = Shredder.shred(im_grayscaled, t, shuffle_shreds=True) reconstructed = Shredder.reconstruct(shuffled) trimmed = im_grayscaled[0:reconstructed.shape[0], 0:reconstructed.shape[1]] assert np.linalg.norm(trimmed - reconstructed) > 1000 # just some magic number
def shred_shuffle_and_reconstruct(x, t): f = lambda v: Shredder.reconstruct(Shredder.shred(v, t, shuffle_shreds=True)) if type(x) == np.ndarray: if x.ndim == 2: return f(x) assert x.ndim == 3, 'Invalid dimensions' return list_of_images_to_numpy([f(im) for im in x[:]]) else: # Here, we got a list of images with different size return [f(v) for v in x]
def test_reconstruction(self): for im_path in [os.path.join(self.__class__.shredder_tests_root, 'fish_orig.JPEG'), os.path.join(self.__class__.shredder_tests_root, 'doc_orig.jpg')]: im = cv2.imread(im_path) im_grayscaled = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) for t in (2, 4, 5): shredded = Shredder.shred(im, t) reconstructed = Shredder.reconstruct(shredded) trimmed = im_grayscaled[0:reconstructed.shape[0], 0:reconstructed.shape[1]] np.testing.assert_equal(reconstructed, trimmed)
def predict(images): reconstructed_image = Shredder.reconstruct(images) print('It is ', end='') if fish_or_doc_classifier.is_fish([reconstructed_image])[0]: print('image') solver_with_comparator = image_type_to_solver_with_comparator[ ImageType.IMAGES] else: print('document') solver_with_comparator = image_type_to_solver_with_comparator[ ImageType.DOCUMENTS] labels = solver_with_comparator.predict(images) return labels
def visualize_crops(crops_list, show=False, save_path=None): t = round(math.sqrt(len(crops_list))) grid_color = 255 img = Shredder.reconstruct(crops_list) if t > 1: dx = img.shape[0] // t dy = img.shape[1] // t img[:, ::dy] = grid_color img[::dx, :] = grid_color plt.imshow(img, cmap='gray') plt.axis('off') if show: plt.show() if save_path is not None: plt.savefig(save_path) plt.clf()