-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
261 lines (211 loc) · 10.3 KB
/
server.py
File metadata and controls
261 lines (211 loc) · 10.3 KB
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
from os import removexattr
from flask import Flask, request, redirect
from flask.helpers import url_for
from flask.templating import render_template
import cv2
import numpy as np
import shutil
import datetime
import os
app = Flask(__name__)
class Image():
filename = None
brightness = "0"
contrast = "1"
denoise = "0"
enhance = "0"
brightnessBackup = "0"
contrastBackup = "0"
denoiseBackup = "0"
enhanceBackup = "0"
revert = "0"
undo = False
stack = False
@app.route("/")
def index():
return render_template("upload.html")
@app.route("/stack", methods=["GET", "POST"])
def stack():
if request.method == "POST":
imageList = request.files.getlist("image")
now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y-%H%M%S")
os.system(f"mkdir static/uploads/stack{timestamp}")
for image in imageList:
image.save(f"static/uploads/stack{timestamp}/{image.filename}")
imageDirectory = f"static/uploads/stack{timestamp}"
files = os.listdir(imageDirectory)
files = [os.path.join(imageDirectory, x) for x in files if x.endswith(
('.jpg')) or x.endswith(('.jpeg'))]
def stackImagesECC(files):
M = np.eye(3, 3, dtype=np.float32)
first_image = None
stacked_image = None
for file in files:
image = cv2.imread(file, 1).astype(np.float32) / 255
print(file)
if first_image is None:
# convert to gray scale floating point image
first_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
stacked_image = image
else:
# Estimate perspective transform
criteria = (cv2.TERM_CRITERIA_EPS |
cv2.TERM_CRITERIA_COUNT, 3000, 1e-5)
s, M = cv2.findTransformECC(cv2.cvtColor(
image, cv2.COLOR_BGR2GRAY), first_image, M, cv2.MOTION_HOMOGRAPHY, criteria, None, 5)
w, h, _ = image.shape
# Align image to first image
image = cv2.warpPerspective(image, M, (h, w))
stacked_image += image
stacked_image /= len(files)
stacked_image = (stacked_image*255).astype(np.uint8)
return stacked_image
stackedImaged = stackImagesECC(files)
cv2.imwrite(imageDirectory + "/stacked.jpg", stackedImaged)
Image.filename = f"stack{timestamp}/stacked.jpg"
Image.stack = True
return redirect(url_for("upload"))
@app.route("/edit", methods=["GET", "POST"])
def upload():
if request.method == "POST":
Image.brightness = "0"
Image.contrast = "1"
Image.denoise = "0"
Image.enhance = "0"
image = request.files["image"]
Image.filename = image.filename
image.save(f"static/uploads/{Image.filename}")
shutil.copyfile(
f"static/uploads/{Image.filename}", f"static/uploads/backup-{Image.filename}")
Image.stack = False
return render_template("index.html", revert=Image.revert, image=Image.filename, brightness=Image.brightness, contrast=Image.contrast, denoise=Image.denoise, enhance=Image.enhance)
else:
now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y-%H%M%S")
return render_template("index.html", dateTime=timestamp, revert=Image.revert, image=Image.filename, brightness=Image.brightness, contrast=Image.contrast, denoise=Image.denoise, enhance=Image.enhance)
@app.route("/process", methods=["GET", "POST"])
def process():
if request.method == "POST":
Image.brightnessBackup = Image.brightness
Image.contrastBackup = Image.contrast
Image.denoiseBackup = Image.denoise
Image.enhanceBackup = Image.enhance
if Image.stack == True:
shutil.copyfile(
f"static/uploads/{Image.filename}", f"static/uploads/undo/{Image.filename}")
shutil.copyfile(
f"static/uploads/backup-{Image.filename}", f"static/uploads/original/{Image.filename}")
else:
shutil.copyfile(
f"static/uploads/{Image.filename}", f"static/uploads/undo-{Image.filename}")
shutil.copyfile(
f"static/uploads/backup-{Image.filename}", f"static/uploads/original-{Image.filename}")
dateTime = datetime.datetime.now()
# Brightness
# if request.form["sliderBrightness"] != Image.brightness:
Image.brightness = request.form["sliderBrightness"]
img = cv2.imread(f"static/uploads/original-{Image.filename}")
img = cv2.add(img, np.array([float(Image.brightness)]))
# img = cv2.convertScaleAbs(img, beta=int(Image.brightness))
cv2.imwrite(f"static/uploads/{Image.filename}", img)
# Contrast
# if request.form["sliderContrast"] != Image.contrast:
Image.contrast = request.form["sliderContrast"]
img = cv2.imread(f"static/uploads/{Image.filename}")
img = cv2.multiply(img, np.array([float(Image.contrast)]))
cv2.imwrite(f"static/uploads/{Image.filename}", img)
# Denoising
if request.form["sliderDenoise"] == "0":
pass
elif request.form["sliderDenoise"] != Image.denoise:
Image.denoise = int(request.form["sliderDenoise"])
output = None # Do not output image
strength = Image.denoise # Denoising filter strength
strengthColour = strength # "Same as h, but for color images only"
templateWindowSize = 7 # "should be odd. (recommended 7)"
searchWindowSize = 21 # "should be odd. (recommended 21)"
img = cv2.imread(f"static/uploads/{Image.filename}")
img = cv2.fastNlMeansDenoisingColored(img,
output,
strength,
strengthColour,
templateWindowSize,
searchWindowSize)
cv2.imwrite(f"static/uploads/{Image.filename}", img)
# if request.form["sliderStar"] != Image.enhance:
# Image.enhance = request.form["sliderStar"]
# kernelSize = int(Image.enhance)
# kernel = np.ones((kernelSize, kernelSize), np.uint8)
# img = cv2.dilate(img, kernel, iterations=1)
# cv2.imwrite(f"static/uploads/{Image.filename}", img)
# Star Enhancement
if request.form["sliderStar"] == "0":
pass
elif request.form["sliderStar"] != Image.enhance:
Image.enhance = request.form["sliderStar"]
n = int(Image.enhance)/5
imgThreshold = cv2.imread(f"static/uploads/{Image.filename}", 0)
imgThreshold = cv2.GaussianBlur(imgThreshold, (5, 5), 0)
cv2.imwrite(f"static/uploads/BLUR.jpg", imgThreshold)
# input, set value, thresholding algorithm, thresholding type, search radius, subtract value
threshold = cv2.adaptiveThreshold(
imgThreshold, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -2)
cv2.imwrite(f"static/uploads/THRESHOLD.jpg", threshold)
threshold = cv2.multiply(threshold, np.array(n))
cv2.imwrite(f"static/uploads/THRESHOLDmult.jpg", threshold)
threshold = cv2.cvtColor(threshold, cv2.COLOR_GRAY2RGB)
cv2.imwrite(f"static/uploads/THRESHOLDrgb.jpg", threshold)
img = cv2.imread(f"static/uploads/{Image.filename}")
img = cv2.add(img, threshold)
cv2.imwrite(f"static/uploads/ADDED.jpg", img)
# Gaussian blur kernel
# kernelGaussian = np.array([[ 1, 4, 6, 4, 1],
# [ 4,16,24,16, 4],
# [ 6,24,36,24, 6],
# [ 4,16,24,16, 4],
# [ 1, 4, 6, 4, 1]])
# kernelGaussian = kernelGaussian * (1/256)
# img = cv2.filter2D(img, -1, kernelGaussian)
# Unsharp mask kernel
kernelUnsharp = np.array([[1, 4, 6, 4, 1],
[4, 16, 24, 16, 4],
[6, 24, -476, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])
kernelUnsharp = kernelUnsharp * (-1/256)
img = cv2.filter2D(img, -1, kernelUnsharp)
# Sharpening kernel
# kernelSharpen = np.array([[ 0,-1, 0],
# [-1, 5,-1],
# [ 0,-1, 0]])
# img = cv2.filter2D(img, -1, kernelSharpen)
cv2.imwrite(f"static/uploads/{Image.filename}", img)
return render_template("index.html", dateTime=dateTime, image=Image.filename, brightness=Image.brightness, contrast=Image.contrast, denoise=Image.denoise, enhance=Image.enhance)
if request.method == "GET":
if Image.stack == False:
shutil.copyfile(
f"static/uploads/backup-{Image.filename}", f"static/uploads/{Image.filename}")
else:
shutil.copyfile(
f"static/uploads/backup-{Image.filename}", f"static/uploads/original/{Image.filename}")
print("Reverted")
Image.brightness = "0"
Image.contrast = "1"
Image.denoise = "0"
Image.enhance = "0"
return "Sucess"
@app.route("/undo")
def undo():
now = datetime.datetime.now()
timestamp = now.strftime("%d-%m-%Y-%H%M%S")
Image.brightness = Image.brightnessBackup
Image.contrast = Image.contrastBackup
Image.denoise = Image.denoiseBackup
Image.enhance = Image.enhanceBackup
shutil.copyfile(
f"static/uploads/undo-{Image.filename}", f"static/uploads/{Image.filename}")
print("Undid")
return render_template("index.html", dateTime=timestamp, revert=Image.revert, image=Image.filename, brightness=Image.brightness, contrast=Image.contrast, denoise=Image.denoise, enhance=Image.enhance)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)