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