-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapp.py
171 lines (148 loc) · 5.95 KB
/
app.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
import io
import os
import sys
import glob
import time
import argparse
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
from PIL import Image
from src.model import DCE_x
from tensorflow import keras
from tensorflow.keras import Model, Input
from tensorflow.keras.layers import Concatenate, Conv2D
# for server
from starlette.applications import Starlette
from starlette.responses import JSONResponse, HTMLResponse, RedirectResponse
import uvicorn
import aiohttp
import asyncio
import base64
tf.compat.v1.enable_eager_execution()
async def get_bytes(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.read()
app = Starlette()
#path = Path('')
@app.route("/upload", methods = ["POST"])
async def upload(request):
data = await request.form()
bytes = await (data["file"].read())
#img_file = io.BytesIO(bytes)
return test(bytes)
@app.route("/classify-url", methods = ["GET"])
async def classify_url(request):
bytes = await get_bytes(request.query_params["url"])
#img_file = io.BytesIO(bytes)
return test(bytes)
def test(bytes):
input_img = Input(shape=(512, 512, 3))
conv1 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(input_img)
conv2 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv1)
conv3 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv2)
conv4 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(conv3)
int_con1 = Concatenate(axis=-1)([conv4, conv3])
conv5 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(int_con1)
int_con2 = Concatenate(axis=-1)([conv5, conv2])
conv6 = Conv2D(32, (3, 3), strides=(1,1), activation='relu', padding='same')(int_con2)
int_con3 = Concatenate(axis=-1)([conv6, conv1])
x_r = Conv2D(24, (3,3), strides=(1,1), activation='tanh', padding='same')(int_con3)
model = Model(inputs=input_img, outputs = x_r)
model.load_weights("weights/best.h5")
### load image ###
# for test_file in glob.glob(lowlight_test_images_path + "*.bmp"):
img_file = io.BytesIO(bytes)
data_lowlight_path = img_file
original_img = Image.open(data_lowlight_path)
original_img.save("img_in.jpg", format="JPEG")
original_size = (np.array(original_img).shape[1], np.array(original_img).shape[0])
required_size = (np.array(original_img).shape[1]//4 * 4, np.array(original_img).shape[0]//4 *4)
original_img = original_img.resize((required_size), Image.LANCZOS)
original_img = (np.asarray(original_img)/255.0)
img_lowlight = Image.open(data_lowlight_path)
img_lowlight = img_lowlight.resize((required_size), Image.LANCZOS)
img_lowlight = (np.asarray(img_lowlight)/255.0)
img_lowlight = np.expand_dims(img_lowlight, 0)
# img_lowlight = K.constant(img_lowlight)
### process image ###
A = model.predict(img_lowlight)
r1, r2, r3, r4, r5, r6, r7, r8 = A[:,:,:,:3], A[:,:,:,3:6], A[:,:,:,6:9], A[:,:,:,9:12], A[:,:,:,12:15], A[:,:,:,15:18], A[:,:,:,18:21], A[:,:,:,21:24]
x = original_img + r1 * (K.pow(original_img,2)-original_img)
x = x + r2 * (K.pow(x,2)-x)
x = x + r3 * (K.pow(x,2)-x)
enhanced_image_1 = x + r4*(K.pow(x,2)-x)
x = enhanced_image_1 + r5*(K.pow(enhanced_image_1,2)-enhanced_image_1)
x = x + r6*(K.pow(x,2)-x)
x = x + r7*(K.pow(x,2)-x)
enhance_image = x + r8*(K.pow(x,2)-x)
enhance_image = tf.cast((enhance_image[0,:,:,:] * 255), dtype=np.uint8)
enhance_image = Image.fromarray(enhance_image.numpy())
enhance_image = enhance_image.resize(original_size, Image.LANCZOS)
enhance_image.save("img_out.jpg", format="JPEG")
img_in = base64.b64encode(open("img_in.jpg", 'rb').read()).decode('utf-8')
img_out = base64.b64encode(open("img_out.jpg", 'rb').read()).decode('utf-8')
return HTMLResponse(
"""
<html>
<head>
<title>Enhanced Image</title>
<style>
.center {
text-align: center;
display: block;
}
</style>
</head>
<body>
<p style="text-align:center"> Hello!!! %s</p>
<div class="center">
<img src="data:image/png;base64, %s" tile="input"> <br>
<a href="data:image/png;base64, img_in" download>
<img src="data:image/png;base64, %s" title="output">
</a>
</div>
</body>
</html>
""" %('', img_in, img_out))
@app.route("/")
def form(request):
return HTMLResponse(
"""
<html>
<head>
<title>Image Enhancer</title>
<style>
.center {
text-align: center;
display: block;
}
</style>
</head>
<body class="center">
<h1> Lowlight Enhancer </h1>
<p> Wanna see better? </p>
<form action="/upload" method = "post" enctype = "multipart/form-data">
<u> Select picture to upload: </u> <br> <p>
1. <input type="file" name="file"><br><p>
2. <input type="submit" value="Upload">
</form>
<br>
<br>
<form action = "/classify-url" method="get">
<u> Submit picture URL </u>
1. <input type="url" name="url" size="60"><br><p>
2. <input type="submit" value="Upload">
</form>
</body>
</html>
""")
@app.route("/form")
def redirect_to_homepage(request):
return RedirectResponse("/")
if __name__ == "__main__":
if "serve" in sys.argv:
port = int(os.environ.get("PORT", 8008))
uvicorn.run(app, host = "0.0.0.0", port = port)
#test(config.lowlight_test_images_path)