def debug_history(self): iv = ImageViewer() iv.remove_axis_values() iv.add(self.source, 'source', cmap='bgr') for img, func in zip(self.debug_out_list, self.functions): iv.add(img, str(func), cmap='bgr') return iv
time = evaluation['time'] kwargs = evaluation['kwargs'] test_perc = evaluation['test_perc'] all_dices.append(dice_vals) all_filenames = filenames if all_filenames is None else all_filenames pairs = list(zip(dice_vals, filenames)) pairs.sort() dice_vals = [x for x, _ in pairs] filenames = [x for _, x in pairs] plt.plot(dice_vals, label=f'dice {i}') plt.axhline(y=mean, color='rgb'[i % 3], ls='--', label=f'mean {i}') print( f'{FILENAME:<32} mean={mean:.04f} time={time:.02f}s fps={len(filenames)/time:.02f} test_perc={test_perc} kwargs={kwargs}' ) plt.title('\n'.join(show_filenames)) plt.legend() plt.show() if len(all_dices) == 2: worst = [(max(a - b, b - a), f) for a, b, f in zip(*all_dices, filenames)] worst.sort() iv = ImageViewer() for s, f in worst[-12:]: iv.add(cv2.imread(f'data_test/paintings_gt/imgs/{f}'), cmap='bgr', title=f'{s}') iv.show()
# I created a script to have the filenames of the images we use as tests # for filename in TEST_PAINTINGS: filename = "data_test/painting_09/00_calibration.jpg" img = np.array(cv2.imread(filename)) # Via the append command you can add a function to the pipeline, # In this case I have to do it because the last function takes as source img pipeline.append(Function(highlight_paintings, source=img, pad=100)) # Using the run command I execute the functions in order. # with debug=True for each step debug images are created that then # can be displayed in sequence # with print_time=True the times are printed for each function # Filename is optional, it's for printing out = pipeline.run(img, debug=True, print_time=True, filename=filename) # debug_history() returns an ImageViewer class with the output sequence for each function plots.append(pipeline.debug_history()) iv.add(out, cmap='bgr') # With pop the last function is removed from the pipeline list pipeline.pop() for plot in plots: # all debugging graphs are displayed plot.show() iv.show() # IMPORTANTE ####################################################################################### # La Pipeline eseguita con il comando debug=False ritornerà i valori dell'ultima funzione che non sono # obbligatoriamente delle immagini, infatti in paintings_rectification.py la uso per ottenere i corners # dei quadri trovati. Mentre con debug=True ritornerà l'ultima immagine di debug creata. # # Ogni funzione che viene inserita nella pipeline accetta una variabile input ed una variabile debug
def start_at(arr): assert arr.ndim == 1 for i, val in enumerate(arr): if val != 0: break return i if __name__ == "__main__": from image_viewer import show_me, ImageViewer from data_test.standard_samples import RANDOM_PAINTING, FISH_EYE, CHESSBOARD, TEST_DISTORTION, get_random_paintings for paint in [FISH_EYE, ]: img = cv2.imread(paint) img = cv2.resize(img, (1280, 720)) iv = ImageViewer() mask = generate_mask(img) iv.add(mask, cmap='bgr', title='mask') canvas = np.empty_like(mask, dtype=np.float32) canvas += _apply_edge_detection(img, 150, 250) canvas = np.clip(canvas, 0, 255) iv.add(img, cmap='bgr', title='original') iv.add(canvas, cmap='bgr', title='canvas') k = HTRDC(img, steps=50, range_min=-0.25, range_max=0) canvas = undistort(img, k1=k) cv2.imwrite('data_test/00_calibration_desired.jpg', canvas) iv.add(canvas, cmap='bgr', title=f'{paint} k={k:.06f}') iv.show()
if __name__ == "__main__": from image_viewer import ImageViewer from data_test.standard_samples import RANDOM_PAINTING, PAINTINGS_DB, TEST_RETRIEVAL from painting_rectification import four_point_transform, remove_pad from painting_detection import painting_detection dataset_images = [cv2.imread(url) for url in PAINTINGS_DB] for painting_path in TEST_RETRIEVAL: img = cv2.imread(painting_path) painting_contours = painting_detection(img) iv = ImageViewer(cols=4) for i, corners in enumerate(painting_contours): img_sec = four_point_transform(img, corners) if not img_sec is None: scores = retrieve_painting(img_sec, dataset_images, verbose=False, threshold=30, resize_factor=1, mse=False) res, diff = best_match(scores) target = dataset_images[res] iv.add(img_sec, cmap='bgr') iv.add(target, cmap='bgr', title=f'[{res}] {scores[res]} diff={diff}') iv.add(img, title=painting_path.split('/')[-1], cmap='bgr') iv.show()
if not found_correct_shape: paintings_contours.append(rect_contour(contour, pad)) return paintings_contours if __name__ == "__main__": from data_test.standard_samples import TEST_PAINTINGS, PEOPLE, TEST_RETRIEVAL, PERSPECTIVE, get_random_paintings from image_viewer import ImageViewer, show_me from stopwatch import Stopwatch from step_06_corners_detection import draw_lines iv = ImageViewer(cols=3) watch = Stopwatch() for filename in [ 'data_test/painting_09/00_calibration.jpg', ]: img = cv2.imread(filename) watch.start() paintigs_contours = painting_detection(img) res = _draw_all_contours(paintigs_contours, img) time = watch.stop() iv.add(res, cmap='bgr', title=f'time={time:.02f}s') print(f'filename={filename} time={time:.02f}s') show_me( res, title= f'filename={filename} time={time:.02f}s n={len(paintigs_contours)}' ) iv.show()
if __name__ == "__main__": from data_test.standard_samples import TEST_PAINTINGS, PERSPECTIVE, get_random_paintings from image_viewer import show_me from step_06_corners_detection import draw_corners # rgbImage = cv2.imread(PERSPECTIVE) for filename in TEST_PAINTINGS[::-1]: rgbImage = cv2.imread(filename) # rgbImage = cv2.rotate(rgbImage, cv2.ROTATE_90_CLOCKWISE) painting_contours = painting_detection(rgbImage, area_perc=0.96) rgbImage_num = rgbImage.copy() for i, corners in enumerate(painting_contours): rgbImage_num = cv2.putText(rgbImage_num, str(i + 1), mean_center(corners), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255), 20, cv2.LINE_AA) for i, corners in enumerate(painting_contours): iv = ImageViewer() iv.add(rgbImage_num, 'original', cmap='bgr') sec = cut_section(rgbImage_num, corners) iv.add(sec, 'target', cmap='bgr') cv2.imwrite('data_test/target.jpg', sec) img = four_point_transform(rgbImage, np.array(corners)) if not img is None: iv.add(img, 'VIT painting {}'.format(i + 1), cmap='bgr') cv2.imwrite('data_test/vit.jpg', img) img = painting_rectification(rgbImage, np.array(corners)) if not img is None: iv.add(img, 'STACK painting {}'.format(i + 1), cmap='bgr') cv2.imwrite('data_test/stack.jpg', img) iv.show()