new function remove_reflection
Browse files- DSRNet/test_sirs.py +55 -33
DSRNet/test_sirs.py
CHANGED
|
@@ -1,33 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
opt
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DSRNet/utils.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from os.path import join
|
| 5 |
+
|
| 6 |
+
import torch.backends.cudnn as cudnn
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
import data.sirs_dataset as datasets
|
| 11 |
+
from data.image_folder import read_fns
|
| 12 |
+
from engine import Engine
|
| 13 |
+
from options.net_options.train_options import TrainOptions
|
| 14 |
+
from tools import mutils
|
| 15 |
+
|
| 16 |
+
def remove_reflection(image_path):
|
| 17 |
+
# Initialize options and engine
|
| 18 |
+
opt = TrainOptions().parse()
|
| 19 |
+
|
| 20 |
+
opt.isTrain = False
|
| 21 |
+
cudnn.benchmark = True
|
| 22 |
+
opt.no_log = True
|
| 23 |
+
opt.display_id = 0
|
| 24 |
+
opt.verbose = False
|
| 25 |
+
|
| 26 |
+
# Create a temporary directory to store the image and load it as a dataset
|
| 27 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 28 |
+
temp_image_path = os.path.join(temp_dir.name, 'temp_image.png')
|
| 29 |
+
|
| 30 |
+
# Save the image to the temporary directory
|
| 31 |
+
with open(image_path, 'rb') as img_file:
|
| 32 |
+
img = Image.open(img_file)
|
| 33 |
+
img.save(temp_image_path)
|
| 34 |
+
|
| 35 |
+
datadir = temp_dir.name
|
| 36 |
+
test_dataset_real = datasets.DSRTestDataset(join(datadir, 'real20_420'),
|
| 37 |
+
fns=[temp_image_path],
|
| 38 |
+
if_align=opt.if_align)
|
| 39 |
+
test_dataloader_real = datasets.DataLoader(test_dataset_real, batch_size=1, shuffle=False, num_workers=opt.nThreads,
|
| 40 |
+
pin_memory=True)
|
| 41 |
+
|
| 42 |
+
engine = Engine(opt)
|
| 43 |
+
|
| 44 |
+
# Test the model and get the result
|
| 45 |
+
result_dir = os.path.join('./checkpoints', opt.name, mutils.get_formatted_time())
|
| 46 |
+
res = engine.test(test_dataloader_real, savedir=join(result_dir, 'test'))
|
| 47 |
+
|
| 48 |
+
# Load the processed image from the result directory
|
| 49 |
+
processed_image_path = join(result_dir, 'test', 'temp_image.png')
|
| 50 |
+
processed_image = Image.open(processed_image_path)
|
| 51 |
+
|
| 52 |
+
temp_dir.cleanup() # Clean up the temporary directory
|
| 53 |
+
|
| 54 |
+
return processed_image
|
| 55 |
+
|