File size: 1,669 Bytes
a9bfcff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa08c18
 
a9bfcff
 
 
 
 
 
 
 
 
 
 
36deecd
 
a9bfcff
 
173bd8c
 
 
a9bfcff
173bd8c
 
0f270a3
aa08c18
173bd8c
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
56
57
58
59
# 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