def create_mean_image_file(file_paths, save_path): frames, _ = load_state_files(file_paths) mean_image = preprocess.compute_mean_image(frames) / 255.0 viewer.show_image(mean_image, title='mean_image') mean_image = converter.hwc2chw(mean_image) save_mean_image(save_path, mean_image) return mean_image
def load_image(image_file): print('loading images from: ', image_file) image = cv2.imread(image_file) image = cv2.resize(image, dsize=(64, 64)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = converter.hwc2chw(image) / 255.0 image = image.astype(np.float32) assert np.all(0.0 <= image) and np.all(image <= 1.0) return image
def preprocess_frames(frames): preprocessed_frames = [converter.hwc2chw(frame) for frame in frames] # for i in range(3): # viewer.show_image(preprocessed_frames[0][i] + mean_image[0, :, :], # 'preprocessed image. color channel -> {} (0: R, 1: G, 2: B) '.format(i)) assert preprocessed_frames[0].shape == (3, 210, 160) return preprocessed_frames
def convert_to_grayscale(subtractor, filename): image = cv2.imread(filename) image = cv2.resize(image, dsize=(64, 64)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = converter.hwc2chw(image) / 255.0 image = image.astype(np.float32) image = np.reshape(image, newshape=((1, ) + image.shape)) print('image shape: ', image.shape) grayscale = subtractor(image) return grayscale
def show_sample_subtraction(model, args): image = cv2.imread(args.test_subtraction_image) image = cv2.resize(image, dsize=(64, 64)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = converter.hwc2chw(image) / 255.0 image = image.astype(np.float32) image = np.reshape(image, newshape=((1, ) + image.shape)) print('image shape: ', image.shape) grayscale = model(image) grayscale = np.reshape(grayscale.data, newshape=(64, 64)) print('grayscale shape: ', grayscale.shape) viewer.show_image(grayscale, is_gray=True)
def load_images(images_dir): print('loading images from: ', images_dir) images = [] files = os.listdir(images_dir) files.sort() for file_name in files: path = os.path.join(images_dir, file_name) if os.path.isfile(path) and '.jpg' in path: image = cv2.imread(path) image = cv2.resize(image, dsize=(64, 64)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = converter.hwc2chw(image) / 255.0 image = image.astype(np.float32) assert np.all(0.0 <= image) and np.all(image <= 1.0) images.append(image) return images
def test_hwc2chw(self): image = np.ndarray(shape=(28, 28, 3), dtype=np.float32) converted = converter.hwc2chw(image) assert converted.shape == (3, 28, 28)