-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
158 lines (135 loc) · 5.24 KB
/
server.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
import hashlib
import os
import time
from flask import (Flask, abort, jsonify, render_template,
request, send_from_directory, url_for)
from PIL import Image
from werkzeug.utils import secure_filename
from haze_removal import HazeRemovel
from utils import threshold_color_array
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 20 * 1024 * 1024
@app.route('/basic')
def index():
return render_template('index.html')
@app.route('/')
def advanced():
return render_template('advanced.html')
@app.route('/upload', methods=['POST'])
def upload():
f = request.files['file_data']
image_key = hashlib.md5(f.read()).hexdigest()
f.seek(0)
image_ext = f.filename.split('.')[1]
image_name = secure_filename('{}.{}'.format(image_key, image_ext))
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_name)
if not os.path.isfile(image_path):
f.save(image_path)
print('image saved')
return jsonify({
'url': url_for('result', image=image_name),
'name': image_name
})
@app.route('/result/<image>')
def result(image):
try:
start_time = time.time()
image_path = os.path.join('./', app.config['UPLOAD_FOLDER'], image)
haze_removal = HazeRemovel(image=image_path)
t1 = time.time()
dark_channel = haze_removal.get_dark_channel(haze_removal.I)
t2 = time.time()
A = haze_removal.get_atmosphere(dark_channel)
t3 = time.time()
t = haze_removal.get_transmission(dark_channel, A)
t4 = time.time()
recover_image = haze_removal.get_recover_image(A, t)
t5 = time.time()
_save_image(dark_channel, image, 'dark')
_save_image(t * 255, image, 't', True)
_save_image(recover_image, image, 'res', True)
end_time = time.time()
ctx = {
'image_org': _image_url(image, 'org'),
'image_dark': _image_url(image, 'dark'),
'image_t': _image_url(image, 't'),
'image_recover': _image_url(image, 'res'),
'atmosphere': A,
'total_time': end_time - start_time,
'real_time': t5 - t1,
'dark_time': t2 - t1,
'a_time': t3 - t2,
't_time': t4 - t3,
'res_time': t5 - t4,
}
return render_template('result.html', **ctx)
except FileNotFoundError as e:
abort(403)
@app.route('/<image>', methods=['POST'])
def advanced_result(image):
try:
config = request.json
start_time = time.time()
image_path = os.path.join('./', app.config['UPLOAD_FOLDER'], image)
refine = config['refine']
window_size = int(config['window_size'])
omega = int(config['omega'])
haze_removal = HazeRemovel(image=image_path, refine=refine, local_patch_size=window_size, omega=omega / 100)
t1 = time.time()
dark_channel = haze_removal.get_dark_channel(haze_removal.I)
t2 = time.time()
A = haze_removal.get_atmosphere(dark_channel)
t3 = time.time()
t = haze_removal.get_transmission(dark_channel, A)
t4 = time.time()
recover_image = haze_removal.get_recover_image(A, t)
t5 = time.time()
_save_image(dark_channel, image, 'dark_{}_{}_{}'.format(refine, window_size, omega))
_save_image(t * 255, image, 't_{}_{}_{}'.format(refine, window_size, omega), True)
_save_image(recover_image, image, 'res_{}_{}_{}'.format(refine, window_size, omega), True)
end_time = time.time()
res = {
'image_org': _image_url(image, 'org'),
'image_recover': _image_url(image, 'res_{}_{}_{}'.format(refine, window_size, omega)),
'config': config
}
if config['show_a']:
res.update({'atmosphere': A.tolist()})
if config['show_dark']:
res.update({'image_dark': _image_url(image, 'dark_{}_{}_{}'.format(refine, window_size, omega))})
if config['show_t']:
res.update({'image_t': _image_url(image, 't_{}_{}_{}'.format(refine, window_size, omega))})
if config['show_time']:
res.update({
'total_time': round(end_time - start_time, 3),
'real_time': round(t5 - t1, 3),
'dark_time': round(t2 - t1, 3),
'a_time': round(t3 - t2, 3),
't_time': round(t4 - t3, 3),
'res_time': round(t5 - t4, 3),
})
return jsonify(res)
except FileNotFoundError as e:
abort(403)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route('/dist/<filename>')
def dist_file(filename):
return send_from_directory('dist', filename)
def _image_url(image, image_type):
if image_type != 'org':
image = '{}_{}'.format(image_type, image)
return '/{}/{}'.format(app.config['UPLOAD_FOLDER'], image)
def _save_image(image, image_name, image_type, threshold=False):
if threshold:
image = threshold_color_array(image)
image = Image.fromarray(image)
image.save('{}/{}_{}'.format(
app.config['UPLOAD_FOLDER'],
image_type,
image_name
))
if __name__ == '__main__':
app.run(host='0.0.0.0')