Ejemplo n.º 1
0
def enhance(path, save_dir):
	# Initialization.
	images = tf.placeholder(tf.float32, [None, None, None, 1], name='images')
	model = SRCNN(images)
	with tf.Session() as sess:
		sess.run(tf.global_variables_initializer())

		# Load the saved checkpoint.
		saver = tf.train.Saver()
		checkpoint_dir = ckpt_dir
		if load_ckpt(sess, checkpoint_dir, saver):
			print('Successfully loaded checkpoint.')
		else:
			print('Failed to load checkpoint.')

		if os.path.isfile(path):
			print('Upscaling image', path, '...')
			test_data, test_color = prepare_raw(path)

			# Generate super-resolutioned image.
			conv_out = model.eval({images: test_data})
			conv_out = conv_out.squeeze()

			result_bw = revert(conv_out)
			result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
			result[:, :, 0] = result_bw
			result[:, :, 1:3] = test_color
			result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
			save_path = os.path.join(save_dir, os.path.basename(path))
			scipy.misc.imsave(save_path, result)

			# Upscale_single_image(model, path, save_dir)
			print('Finished upscaling image', path)

		elif os.path.isdir(path):
			for root, dirs, files in os.walk(path):
				for im_name in files:
					img_path = os.path.join(path, im_name)

					print('Upscaling image', img_path, '...')
					test_data, test_color = prepare_raw(img_path)

					# Generate super-resolutioned image.
					conv_out = model.eval({images: test_data})
					conv_out = conv_out.squeeze()

					result_bw = revert(conv_out)
					result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
					result[:, :, 0] = result_bw
					result[:, :, 1:3] = test_color
					result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
					save_path = os.path.join(save_dir, os.path.basename(img_path))
					scipy.misc.imsave(save_path, result)
					print('Finished upscaling image', img_path)
			print('Finished upscaling all images.')
		else:
			print(' [*] Invalid input path.')
Ejemplo n.º 2
0
def enhance(x, path, save_dir):
    # Initialization.
    model = SRCNN(x)
    l2_loss = tf.reduce_mean(tf.square(labels - model))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        l2_loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print('Generating super-resolutioned image...')

        # Load the saved checkpoint.
        saver = tf.train.Saver()
        if load_ckpt(sess, ckpt_dir, saver):
            print('Successfully loaded checkpoint.')
        else:
            print('Failed to load checkpoint.')

        if os.path.isfile(path):
            print('Upscaling image', path, '...')
            img = cv.imread(path)
            h, w = img.shape[0], img.shape[1]
            num_hor = math.ceil((h * multiplier - size_input) / 21)
            num_ver = math.ceil((w * multiplier - size_input) / 21)

            test_data, test_color = prepare_raw(path)
            # with h5py.File(data_path, 'r') as hf:
            # 	test_data = np.array(hf.get('data'))
            # 	test_color = np.array(hf.get('color'))

            # Generate super-resolutioned image.
            conv_out = model.eval({images: test_data
                                   })  # Result in patch of size 21x21.
            height, width = conv_out.shape[1], conv_out.shape[2]
            # print('conv_out has shape:', conv_out.shape)
            result = np.zeros([height * num_hor, width * num_ver, 3])
            # original = np.zeros([height * num_hor, width * num_ver, 1])
            # print('result has shape:', result.shape)
            # print('num_hor =', num_hor, 'num_ver =', num_ver)
            i, j = 0, 0
            for idx, image in enumerate(conv_out):
                j = idx // num_ver
                i = idx - j * num_ver
                # print('idx =', idx, 'i =', i, 'j =', j)
                result[j * height:j * height + height,
                       i * width:i * width + width, 0] = image.squeeze()
            # result = result.squeeze()
            result = revert(result)

            print('Filling color information...')
            i, j = 0, 0
            for idx, image in enumerate(test_color):
                j = idx // num_ver
                i = idx - j * num_ver
                result[j * height:j * height + height,
                       i * width:i * width + width, 1:3] = image

            result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
            # cv.imshow('super-resolution', result)
            # cv.waitKey(0)
            # save_path = os.path.join('./result', 'test_raw.png')
            save_path = os.path.join(save_dir, os.path.basename(path))
            scipy.misc.imsave(save_path, result)
            print('Finished upscaling image', path)

        elif os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for im_name in files:
                    img_path = os.path.join(path, im_name)
                    print('Upscaling image', img_path, '...')
                    img = cv.imread(img_path)
                    h, w = img.shape[0], img.shape[1]
                    num_hor = math.ceil((h * multiplier - size_input) / 21)
                    num_ver = math.ceil((w * multiplier - size_input) / 21)

                    test_data, test_color = prepare_raw(img_path)

                    # Generate super-resolutioned image.
                    conv_out = model.eval({images: test_data
                                           })  # Result in patch of size 21x21.
                    height, width = conv_out.shape[1], conv_out.shape[2]
                    # print('conv_out has shape:', conv_out.shape)
                    result = np.zeros([height * num_hor, width * num_ver, 3])
                    # original = np.zeros([height * num_hor, width * num_ver, 1])
                    # print('result has shape:', result.shape)
                    # print('num_hor =', num_hor, 'num_ver =', num_ver)
                    i, j = 0, 0
                    for idx, image in enumerate(conv_out):
                        j = idx // num_ver
                        i = idx - j * num_ver
                        # print('idx =', idx, 'i =', i, 'j =', j)
                        result[j * height:j * height + height,
                               i * width:i * width + width,
                               0] = image.squeeze()
                    # result = result.squeeze()
                    result = revert(result)

                    print('Filling color information...')
                    i, j = 0, 0
                    for idx, image in enumerate(test_color):
                        j = idx // num_ver
                        i = idx - j * num_ver
                        result[j * height:j * height + height,
                               i * width:i * width + width, 1:3] = image

                    result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
                    # cv.imshow('super-resolution', result)
                    # cv.waitKey(0)
                    # save_path = os.path.join('./result', 'test_raw.png')
                    save_path = os.path.join(save_dir,
                                             os.path.basename(img_path))
                    scipy.misc.imsave(save_path, result)
                    print('Finished upscaling image', img_path)
            print('Finished upscaling all images.')
        else:
            print(' [*] Invalid input path.')
Ejemplo n.º 3
0
def generate_SR(x, path, save_dir):
    # Initialization.
    model = SRCNN(x)
    l2_loss = tf.reduce_mean(tf.square(labels - model))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        l2_loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print('Generating super-resolutioned image...')

        # Load the saved checkpoint.
        saver = tf.train.Saver()
        if load_ckpt(sess, ckpt_dir, saver):
            print('Successfully loaded checkpoint.')
        else:
            print('Failed to load checkpoint.')

        if os.path.isfile(path):
            img = cv.imread(path)
            h, w = img.shape[0], img.shape[1]
            num_hor = math.ceil((h - size_input) / 21)
            num_ver = math.ceil((w - size_input) / 21)

            test_data, test_label = prepare_data(path)
            print('test_data has shape:', test_data.shape, 'label has shape:',
                  test_label.shape)

            # Generate super-resolutioned image.
            conv_out = model.eval({images: test_data
                                   })  # Result in patch of size 21x21.
            height, width = conv_out.shape[1], conv_out.shape[2]
            # print('conv_out has shape:', conv_out.shape)
            result = np.zeros([height * num_hor, width * num_ver, 1])
            original = np.zeros([height * num_hor, width * num_ver, 1])
            # print('result has shape:', result.shape)
            # print('num_hor =', num_hor, 'num_ver =', num_ver)
            i, j = 0, 0
            for idx, image in enumerate(conv_out):
                j = idx // num_ver
                i = idx - j * num_ver
                print('idx =', idx, 'i =', i, 'j =', j)
                result[j * height:j * height + height,
                       i * width:i * width + width, :] = image
            result = result.squeeze()
            result = revert(result)

            i, j = 0, 0
            for idx, image in enumerate(test_label):
                j = idx // num_ver
                i = idx - j * num_ver
                original[j * height:j * height + height,
                         i * width:i * width + width, :] = image
            original = original.squeeze()

            size_original = original.shape
            bicubic = scipy.misc.imresize(original, 1 / 3, interp='bicubic')
            bicubic = scipy.misc.imresize(bicubic,
                                          size_original,
                                          interp='bicubic')

            # Display and save the image.
            # cv.imshow('original', original)
            # cv.waitKey(0)
            # cv.imshow('bicubic', bicubic)
            # cv.waitKey(0)
            # cv.imshow('super-resolution', result)
            # cv.waitKey(0)
            # save_path = os.path.join('./result', 'test.png')
            save_path = os.path.join(save_dir, os.path.basename(path))
            scipy.misc.imsave(save_path, result)
            # scipy.misc.imsave('./result/original.png', original)
            scipy.misc.imsave('./result/bicubic.png', bicubic)
        elif os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for im_name in files:
                    img_path = os.path.join(path, im_name)
                    print('Testing on image', img_path)
                    img = cv.imread(img_path)
                    h, w = img.shape[0], img.shape[1]
                    num_hor = math.ceil((h - size_input) / 21)
                    num_ver = math.ceil((w - size_input) / 21)

                    test_data, test_label = prepare_data(img_path)

                    # Generate super-resolutioned image.
                    conv_out = model.eval({images: test_data
                                           })  # Result in patch of size 21x21.
                    height, width = conv_out.shape[1], conv_out.shape[2]
                    # print('conv_out has shape:', conv_out.shape)
                    result = np.zeros([height * num_hor, width * num_ver, 1])
                    original = np.zeros([height * num_hor, width * num_ver, 1])
                    # print('result has shape:', result.shape)
                    # print('num_hor =', num_hor, 'num_ver =', num_ver)
                    i, j = 0, 0
                    for idx, image in enumerate(conv_out):
                        j = idx // num_ver
                        i = idx - j * num_ver
                        # print('idx =', idx, 'i =', i, 'j =', j)
                        result[j * height:j * height + height,
                               i * width:i * width + width, :] = image
                    result = result.squeeze()
                    result = revert(result)

                    i, j = 0, 0
                    for idx, image in enumerate(test_label):
                        j = idx // num_ver
                        i = idx - j * num_ver
                        original[j * height:j * height + height,
                                 i * width:i * width + width, :] = image
                    original = original.squeeze()

                    size_original = original.shape
                    bicubic = scipy.misc.imresize(original,
                                                  1 / 3,
                                                  interp='bicubic')
                    bicubic = scipy.misc.imresize(bicubic,
                                                  size_original,
                                                  interp='bicubic')

                    save_path = os.path.join(save_dir,
                                             os.path.basename(img_path))
                    scipy.misc.imsave(save_path, result)
                    save_path = save_dir + 'bicubic_' + os.path.basename(
                        img_path)
                    scipy.misc.imsave(save_path, bicubic)

                    print('Finished writing', os.path.basename(img_path))

        else:
            print(' [*] Invalid input path.')
Ejemplo n.º 4
0
def generate_SR(path, save_dir):
	# Initialization.
	images = tf.placeholder(tf.float32, [None, None, None, 1], name='images')
	model = SRCNN(images)

	with tf.Session() as sess:
		sess.run(tf.global_variables_initializer())
		print('Generating super-resolutioned image...')

		# Load the saved checkpoint.
		saver = tf.train.Saver()
		if load_ckpt(sess, ckpt_dir, saver):
			print('Successfully loaded checkpoint.')
		else:
			print('Failed to load checkpoint.')

		if os.path.isfile(path):
			test_data, test_label, color = prepare_data(path)
			print('test_data has shape:', test_data.shape, 'label has shape:', test_label.shape)
			print('color has shape:', color.shape)

			# Generate super-resolutioned image.
			conv_out = model.eval({images: test_data})	# Result in patch of size 21x21.
			conv_out = conv_out.squeeze()
			result_bw = revert(conv_out)

			result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
			result[:, :, 0] = result_bw
			result[:, :, 1:3] = color
			result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)

			bicubic = scipy.misc.imresize(test_label, 1 / multiplier, interp='bicubic')
			bicubic = scipy.misc.imresize(bicubic, multiplier * 1.0, interp='bicubic')
			bicubic = cv.cvtColor(bicubic, cv.COLOR_BGR2RGB)

			# Save the image.
			save_path = os.path.join(save_dir, os.path.basename(path))
			scipy.misc.imsave(save_path, result)
			bicubic_path = os.path.join(save_dir, 'bicubic_' + os.path.basename(path))
			scipy.misc.imsave(bicubic_path, bicubic)
			print('Finished testing', path)
		elif os.path.isdir(path):
			for root, dirs, files in os.walk(path):
				for im_name in files:
					img_path = os.path.join(path, im_name)
					print('Testing on image', img_path)

					test_data, test_label, color = prepare_data(img_path)
					print('test_data has shape:', test_data.shape, 'label has shape:', test_label.shape)
					print('color has shape:', color.shape)

					# Generate super-resolutioned image.
					conv_out = model.eval({images: test_data})	# Result in patch of size 21x21.
					conv_out = conv_out.squeeze()
					result_bw = revert(conv_out)

					result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
					result[:, :, 0] = result_bw
					result[:, :, 1:3] = color
					result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)

					bicubic = scipy.misc.imresize(test_label, 1 / multiplier, interp='bicubic')
					bicubic = scipy.misc.imresize(bicubic, multiplier * 1.0, interp='bicubic')
					bicubic = cv.cvtColor(bicubic, cv.COLOR_BGR2RGB)

					# img = cv.imread(img_path)
					# h, w = img.shape[0], img.shape[1]
					# num_hor = math.ceil((h - size_input) / 21)
					# num_ver = math.ceil((w - size_input) / 21)

					# test_data, test_label = prepare_data(img_path)

					# # Generate super-resolutioned image.
					# conv_out = model.eval({images: test_data})	# Result in patch of size 21x21.
					# height, width = conv_out.shape[1], conv_out.shape[2]
					# # print('conv_out has shape:', conv_out.shape)
					# result = np.zeros([height * num_hor, width * num_ver, 1])
					# original = np.zeros([height * num_hor, width * num_ver, 1])
					# # print('result has shape:', result.shape)
					# # print('num_hor =', num_hor, 'num_ver =', num_ver)
					# i, j = 0, 0
					# for idx, image in enumerate(conv_out):
					# 	j = idx // num_ver
					# 	i = idx - j * num_ver
					# 	# print('idx =', idx, 'i =', i, 'j =', j)
					# 	result[j * height : j * height + height, i * width : i * width + width, :] = image
					# result = result.squeeze()
					# result = revert(result)

					# i, j = 0, 0
					# for idx, image in enumerate(test_label):
					# 	j = idx // num_ver
					# 	i = idx - j * num_ver
					# 	original[j * height : j * height + height, i * width : i * width + width, :] = image
					# original = original.squeeze()

					# size_original = original.shape
					# bicubic = scipy.misc.imresize(original, 1/3, interp='bicubic')
					# bicubic = scipy.misc.imresize(bicubic, size_original, interp='bicubic')

					# Save the image.
					save_path = os.path.join(save_dir, os.path.basename(img_path))
					scipy.misc.imsave(save_path, result)
					bicubic_path = os.path.join(save_dir, 'bicubic_' + os.path.basename(img_path))
					scipy.misc.imsave(bicubic_path, bicubic)
					print('Finished testing', os.path.basename(img_path))
			print('Finished testing all images.')
		else:
			print(' [*] Invalid input path.')