-
Notifications
You must be signed in to change notification settings - Fork 10
/
ImageRestorer.py
391 lines (271 loc) · 13.2 KB
/
ImageRestorer.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import time
from PIL import Image
from glob import glob
import numpy as np
import os, sys
from contextlib import contextmanager
from numba import cuda as ncuda
from PIL import ImageFilter
import cv2
fast_denoising = False
import time
import ImagePipeline_utils as IP
from ImagePipeline_utils import Quiet
import os, shutil
import warnings
from os import path as osp
import subprocess
from contextlib import contextmanager
import time, sys
class ImageRestorer:
def __init__(self, resetgpu = True):
self._history = []
self._resetgpu = resetgpu
#record current interpreter path (to ensure calling the right interpreter when calling another process)
self._python_dir = sys.executable
self.denoise = self.remove_gaussian_noise
self.Q = Quiet()
def preprocess(self, inputdir = None, outputdir = None, **kwargs):
"""
preprocess images: convert them to RGB format, resize them if too large (optional, True by default), convert them to grayscale (optional, False by default)
Defaults options (you can override any option with a keyword argument):
options = {'gray':False, 'resize':True, 'size':(1000,1000), 'quiet':True, 'raising':True}
"""
#default parameters
options = {'gray':False, 'resize':True, 'size':(1000,1000), 'quiet':True, 'raising':True}
#default parameters are overriden by keywords arguments (e.g. gray = True) (passed by **kwargs) and are unpacked as class attributes (self.gray = True, ...)
inputdir, outputdir = self._init_process(inputdir, outputdir, "preprocess", options, **kwargs)
with self.Q.quiet_and_timeit("Image preprocessing", raising = self.raising, quiet = self.quiet):
imname = '*'
orignames = glob(os.path.join(inputdir, imname))
for orig in orignames:
try:
im = Image.open(orig)
#print(orig)
#remove alpha component
#print("torgb")
im = IP.to_RGB(im)
#convert to grayscale
if self.gray:
#print("togray")
im = IP.to_grayscale(im)
#resize
if self.resize:
width, height = im.size
#resize only if larger than limit
if width > self.size[0] or height > self.size[1]:
im.thumbnail(self.size,Image.ANTIALIAS)
#save as png (and remove previous version if inputdir = outputdir)
path, file = os.path.split(orig)
f, e = os.path.splitext(file)
if inputdir == outputdir:
os.remove(orig)
output_name = os.path.join(outputdir, f+".png")
im.save(output_name)
print(output_name)
except Exception as e:
self.Q.force_print(e)
def filter(self, inputdir = None, outputdir = None, **kwargs):
"""
Perform basic filtering (median, gaussian and/or mean filtering) on images
Defaults options (you can override any option with a keyword argument):
options = {'median':True, 'median_winsize':5, 'gaussian':True, 'gaussian_x':5, 'gaussian_y':5, 'gaussian_std':1, 'mean':True, 'mean_winsize':3, 'raising':True, 'quiet':True}
"""
options = {'median':True, 'median_winsize':5, 'gaussian':True, 'gaussian_x':5, 'gaussian_y':5, 'gaussian_std':1, 'mean':True, 'mean_winsize':3, 'raising':True, 'quiet':True}
inputdir, outputdir = self._init_process(inputdir, outputdir, "filter", options, **kwargs)
with self.Q.quiet_and_timeit("Image filtering", self.raising, self.quiet):
imname = '*'
orignames = glob(os.path.join(inputdir, imname))
for orig in orignames:
print(orig)
try:
im = cv2.imread(orig, cv2.IMREAD_COLOR)
#median blur
if self.median:
im = cv2.medianBlur(im,self.median_winsize)
if self.gaussian:
im = cv2.GaussianBlur(im,(self.gaussian_x,self.gaussian_y),self.gaussian_std)
#mean blur
if self.mean:
im = cv2.blur(im,(self.mean_winsize,self.mean_winsize))
#save as png (and remove previous version if inputdir = outputdir)
path, file = os.path.split(orig)
f, e = os.path.splitext(file)
if inputdir == outputdir:
os.remove(orig)
output_name = os.path.join(outputdir, f+".png")
cv2.imwrite(output_name, im)
print(output_name)
except Exception as e:
self.Q.force_print(e)
def remove_stripes(self, inputdir = None, outputdir = None, **kwargs):
"""
Remove vertical and horizontal stripes from images
Defaults options (you can override any option with a keyword argument):
options = {'working_dir':'./WDNN', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
"""
options = {'working_dir':'./WDNN', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
inputdir, outputdir = self._init_process(inputdir, outputdir, "remove_stripes", options, **kwargs)
command = "%s -W ignore -u striperemover.py -i %s -o %s %s %s" % (self.python_dir, inputdir, outputdir, self.process_args, self.command_suffix)
#Remove vertical stripes
with self.Q.quiet_and_timeit("Removing stripes", self.raising, self.quiet):
IP.createdir_ifnotexists(outputdir)
subprocess.run(command, shell = True)
if self.raising:
self.logerr()
if not self.quiet:
for l in self.log():
print(l)
def remove_gaussian_noise(self, inputdir = None, outputdir = None, **kwargs):
"""
Remove gaussian noise using NLRN (or DNCNN if fast is True).
Defaults options (you can override any option with a keyword argument):
options = {'fast':False, 'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
Note that we use a command suffix to export console outputs to a log file. If you remove this, you could have bugs in jupyter notebooks. It should work in standard python.
You can monitor log.out output by using the command "tail -f log.out" in a terminal.
You can launch the process with a specific python environment by providing its path with the keyword argument "python_dir".
"""
#defaults attributes to instance for method
options = {'fast':False, 'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
#Note that we use a command suffix to export console outputs to a log file. If you remove this, you could have bugs in jupyter notebooks. It should work in standard python
inputdir, outputdir = self._init_process(inputdir, outputdir, "remove_gaussian_noise", options, **kwargs)
command = "%s -W ignore -u denoiser.py -i %s -o %s %s %s" % (self.python_dir, inputdir, outputdir, self.process_args, self.command_suffix)
if self.fast:
#IP.reset_gpu(0)
command = '%s -W ignore -u denoiser_NLRN_DNCNN.py -i %s -o %s --method DNCNN %s %s' % (self.python_dir, inputdir, outputdir, self.process_args, self.command_suffix)
#print("raising", self.raising)
with self.Q.quiet_and_timeit("Removing gaussian noise", self.raising, self.quiet):
IP.createdir_ifnotexists(outputdir)
subprocess.run(command, shell = True)
if self.raising:
self.logerr()
if not self.quiet:
for l in self.log():
print(l)
#if self.fast:
#IP.reset_gpu(0)
def colorize(self, inputdir = None, outputdir = None, **kwargs):
"""
Colorize images using deoldify.
Defaults options (you can override any option with a keyword argument):
options = {'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
Note that we use a command suffix to export console outputs to a log file. If you remove this, you could have bugs in jupyter notebooks. It should work in standard python.
You can monitor log.out output by using the command "tail -f log.out" in a terminal.
You can launch the process with a specific python environment by providing its path with the keyword argument "python_dir".
"""
#defaults attributes to instance for method
options = {'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
inputdir, outputdir = self._init_process(inputdir, outputdir, "colorize", options, **kwargs)
command = "%s -W ignore -u colorizer.py -i %s -o %s %s %s" % (self.python_dir, inputdir, outputdir, self.process_args, self.command_suffix)
with self.Q.quiet_and_timeit("Colorizing", self.raising, self.quiet):
IP.createdir_ifnotexists(outputdir)
subprocess.run(command, shell = True)
if self.raising:
self.logerr()
if not self.quiet:
for l in self.log():
print(l)
def super_resolution(self, inputdir = None, outputdir = None, **kwargs):
"""
Upsample images using ESRGAN.
Defaults options (you can override any option with a keyword argument):
options = {'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
Note that we use a command suffix to export console outputs to a log file. If you remove this, you could have bugs in jupyter notebooks. It should work in standard python.
You can monitor log.out output by using the command "tail -f log.out" in a terminal.
You can launch the process with a specific python environment by providing its path with the keyword argument "python_dir".
"""
#defaults attributes to instance for method
options = {'working_dir':'./', 'raising':True, 'quiet':True, 'python_dir':sys.executable, 'process_args':'', 'command_suffix':" 2> log.err 1>> log.out"}
inputdir, outputdir = self._init_process(inputdir, outputdir, "super_resolution", options, **kwargs)
command = "%s -W ignore -u superresolution.py -i %s -o %s %s %s" % (self.python_dir, inputdir, outputdir, self.process_args, self.command_suffix)
with self.Q.quiet_and_timeit("Super-resolving", self.raising, self.quiet):
IP.createdir_ifnotexists(outputdir)
subprocess.run(command, shell = True)
if self.raising:
self.logerr()
if not self.quiet:
for l in self.log():
print(l)
def merge(self, inputdirs, outputdir, **kwargs):
"""
merge (compute average) of folders parwise images
inputdirs: list of input directories
Defaults options (you can override any option with a keyword argument):
options = {'weights':[1/len(inputdirs)]*len(inputdirs), 'raising':True, 'quiet':True}
"""
options = {'weights':[1/len(inputdirs)]*len(inputdirs), 'raising':True, 'quiet':True}
inputdirs, outputdir = self._init_process(inputdirs, outputdir, "merge", options, **kwargs)
with self.Q.quiet_and_timeit("Image merging", self.raising, self.quiet):
names = IP.get_filenames(inputdirs[0])
for n in names:
try:
files = [os.path.join(i,n) for i in inputdirs]
merged = IP.image_average(files,self.weights)
merged.save(os.path.join(outputdir,n))
except Exception as e:
self.Q.force_print(e)
def _init_process(self, inputdir, outputdir, process, default_options, **kwargs):
"""
This method can (should) be used at the beginning of any other method, to manage process options and history log
"""
if inputdir == None:
if len(self._history) == 0:
raise("Please set the inputdir at least for first processing step")
else:
#If no inputdir is provided, take last outputdir as new inputdir
inputdir = self._history[-1]['output']
#if outputdir is not provided, override inputdir with process result
if outputdir == None:
outputdir = inputdir
elif outputdir != inputdir:
if not ( (type(inputdir) is list) and (outputdir in inputdir) ):
#initialize only if diffrent from inputdir
IP.initdir(outputdir)
options = default_options
#override default parameters for the process, and unpack all arguments as class attributes
options.update(kwargs)
#create/update (unpack) class attributes with options
for key in options:
self.__setattr__(key, options[key])
self._history.append({"input":inputdir,"output":outputdir,"process":process,"options" : options})
IP.createdir_ifnotexists(outputdir)
if self._resetgpu:
IP.reset_gpu(0)
return inputdir, outputdir
def display(self, **kwargs):
if len(self._history) == 0:
print("You did not perform any process yet. No image folder to display.")
return
last_folder = self._history[-1]["output"]
IP.display_folder(last_folder, **kwargs)
def log(self, lines = 10):
logdata = []
if os.path.exists('log.out'):
with open('log.out', 'r') as myfile:
logdata = myfile.readlines()
if os.path.exists('log.err'):
with open('log.err', 'r') as myfile:
logdataerr = myfile.readlines()
logdata = logdata+logdataerr
if len(logdata) > 0:
if len(logdata) < lines:
return logdata
return logdata[-lines:]
else:
logdata = 'No log.out file. Using restorer history instead.\n\n %s' % (self._history)
return logdata
def history(self):
return self._history
def logerr(self, raising = False):
if os.path.exists('log.err'):
with open('log.err') as f:
logdata = f.readlines()
if len(logdata) > 0:
if any(['error' in l.lower() for l in logdata]):
print('Error or warning occured during process. Please check output below.')
for l in logdata:
if raising:
raise Exception(l)
else:
print(l)
return logdata