# 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 import streamlit as st 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 = opt.base_dir test_dataset_real = datasets.RealDataset(datadir) st.write(datadir) 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