SunlitDeadline commited on
Commit
a9bfcff
·
verified ·
1 Parent(s): 1d7ca0c

new function remove_reflection

Browse files
Files changed (1) hide show
  1. DSRNet/test_sirs.py +55 -33
DSRNet/test_sirs.py CHANGED
@@ -1,33 +1,55 @@
1
- import os
2
- from os.path import join
3
-
4
- import torch.backends.cudnn as cudnn
5
-
6
- import data.sirs_dataset as datasets
7
- from data.image_folder import read_fns
8
- from engine import Engine
9
- from options.net_options.train_options import TrainOptions
10
- from tools import mutils
11
-
12
- opt = TrainOptions().parse()
13
-
14
- opt.isTrain = False
15
- cudnn.benchmark = True
16
- opt.no_log = True
17
- opt.display_id = 0
18
- opt.verbose = False
19
-
20
- datadir = os.path.join(opt.base_dir, 'test')
21
- test_dataset_real = datasets.DSRTestDataset(join(datadir, f'real20_420'),
22
- fns=read_fns('data/real_test.txt'),
23
- if_align=opt.if_align)
24
- #test_dataset_real = datasets.RealDataset('./data')
25
- test_dataloader_real = datasets.DataLoader(test_dataset_real, batch_size=1, shuffle=True, num_workers=opt.nThreads,
26
- pin_memory=True)
27
-
28
- engine = Engine(opt)
29
-
30
- """Main Loop"""
31
- result_dir = os.path.join('./checkpoints', opt.name, mutils.get_formatted_time())
32
-
33
- res = engine.test(test_dataloader_real, savedir=join(result_dir, 'test'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+