-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
359 lines (256 loc) · 14.2 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
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
"""
PR IMAGE PROCESSING
1. Bisa memilih area yang mana ingin diapply
2. Object Detection
"""
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
import os
import cv2
import numpy as np
from PIL import Image, ImageEnhance
import subprocess
# LIBRARY FILE SENDIRI
from facial_landmarks import FaceLandmarks
from blur_function import lensblur
app = Flask(__name__, static_folder="./static", template_folder="./templates")
UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def adjust_hue_pil(image_path, hue_factor):
img = Image.open(image_path)
hsv = img.convert('HSV')
hue = hsv.split()[0]
hue = hue.point(lambda p: (p + hue_factor * 255) % 256)
new_img = Image.merge('HSV', (hue, hsv.split()[1], hsv.split()[2]))
return new_img.convert('RGB')
def motion_blur_effect(img, blur_length, angle):
angle = float(angle)
psf = np.zeros((50, 50, 3))
psf = cv2.ellipse(psf, (25, 25), (blur_length, 0), angle, 0, 360, (1, 1, 1), thickness=-1)
psf /= psf[:,:,0].sum()
imfilt = cv2.filter2D(img, -1, psf)
return imfilt
def get_face_mask(image, landmarks):
height, width, _ = image.shape
mask = np.zeros((height, width), np.uint8)
if len(landmarks) == 0:
return None
convexhull = cv2.convexHull(landmarks)
cv2.fillConvexPoly(mask, convexhull, 255)
return mask
@app.route("/")
def home():
return render_template("index.html")
@app.route("/blur", methods=["GET", "POST"])
def blurring():
if request.method == "POST":
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
# Mode blur
gaussian_blur = request.form.get("gaussian")
motion_blur_option = request.form.get("motion")
lens_blur = request.form.get("lensblur")
# Mode fokus muka
face_detect_on = request.form.get("face-true")
face_invert = request.form.get("invert")
face_only = request.form.get("blurface")
# KHUSUS MOTION BLUR
angle = request.form.get("angle")
radius = request.form.get("radius")
fl = FaceLandmarks()
image = cv2.imread(file_path)
if gaussian_blur:
print("You've choosen Gaussian Blur!")
print("Creating gaussian blur...")
if face_detect_on:
landmarks = fl.get_facial_landmarks(image)
mask = get_face_mask(image, landmarks)
if mask is None:
return render_template("blur.html", result="Tidak terdeteksi wajah!")
if face_only:
image_blur = cv2.GaussianBlur(image, (27, 27), 0)
face_extracted = cv2.bitwise_and(image_blur, image_blur, mask=mask)
background_mask = cv2.bitwise_not(mask)
background = cv2.bitwise_and(image, image, mask=background_mask)
result = cv2.add(background, face_extracted)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
elif face_invert:
image_blur = cv2.GaussianBlur(image, (27, 27), 0)
face_without_blur = cv2.bitwise_and(image, image, mask=mask)
background_mask = cv2.bitwise_not(mask)
background_blur = cv2.bitwise_and(image_blur, image_blur, mask=background_mask)
result = cv2.add(background_blur, face_without_blur)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
else:
image_blur = cv2.GaussianBlur(image, (27, 27), 0)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), image_blur)
return render_template("blur.html", result="Success!", image=filename, image_output="blurred_" + filename)
if motion_blur_option:
print("You've choosen Motion Blur!")
print("Creating motion blur...")
if face_detect_on:
landmarks = fl.get_facial_landmarks(image)
mask = get_face_mask(image, landmarks)
if mask is None:
return render_template("blur.html", result="Tidak terdeteksi wajah!")
if face_only:
image_blur = motion_blur_effect(image, 100, angle)
face_extracted = cv2.bitwise_and(image_blur, image_blur, mask=mask)
background_mask = cv2.bitwise_not(mask)
background = cv2.bitwise_and(image, image, mask=background_mask)
result = cv2.add(background, face_extracted)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
elif face_invert:
image_blur = motion_blur_effect(image, 100, angle)
face_without_blur = cv2.bitwise_and(image, image, mask=mask)
background_mask = cv2.bitwise_not(mask)
background_blur = cv2.bitwise_and(image_blur, image_blur, mask=background_mask)
result = cv2.add(background_blur, face_without_blur)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
else:
image_blur = motion_blur_effect(image, 10000, angle)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), image_blur)
return render_template("blur.html", result="Success!", image=filename, image_output="blurred_" + filename)
if lens_blur:
print("You've choosen Lens Blur!")
print("Creating lens blur...")
if face_detect_on:
landmarks = fl.get_facial_landmarks(image)
mask = get_face_mask(image, landmarks)
if mask is None:
return render_template("blur.html", result="Tidak terdeteksi wajah!")
if face_only:
image_blur = lensblur.apply_lens_blur(file_path, int(radius)) # (image, radius)
face_extracted = cv2.bitwise_and(image_blur, image_blur, mask=mask)
background_mask = cv2.bitwise_not(mask)
background = cv2.bitwise_and(image, image, mask=background_mask)
result = cv2.add(background, face_extracted)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
elif face_invert:
image_blur = lensblur.apply_lens_blur(file_path, int(radius)) # (image, radius)
face_without_blur = cv2.bitwise_and(image, image, mask=mask)
background_mask = cv2.bitwise_not(mask)
background_blur = cv2.bitwise_and(image_blur, image_blur, mask=background_mask)
result = cv2.add(background_blur, face_without_blur)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), result)
else:
image_blur = lensblur.apply_lens_blur(file_path, int(radius)) # (image, radius)
cv2.imwrite(os.path.join(app.config["UPLOAD_FOLDER"], "blurred_" + filename), image_blur)
return render_template("blur.html", result="Success!", image=filename, image_output="blurred_" + filename)
return render_template("blur.html")
@app.route("/rgb", methods=["GET", "POST"])
def rgb_img():
if request.method == "POST":
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
red = int(request.form.get("red"))
green = int(request.form.get("green"))
blue = int(request.form.get("blue"))
image = Image.open(file_path)
image = image.convert("RGB")
width, height = image.size
for y in range(height):
for x in range(width):
r, g, b = image.getpixel((x, y))
r_new = min(255, r + red)
g_new = min(255, g + green)
b_new = min(255, b + blue)
image.putpixel((x, y), (r_new, g_new, b_new))
cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], 'rgb_' + filename), cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR))
return render_template("rgb.html", image=filename, image_output='rgb_' + filename)
return render_template("rgb.html")
@app.route("/adjustment", methods=["GET", "POST"])
def adjustment():
if request.method == "POST":
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
#######################################################
# USER ENTRIES #
#######################################################
new_image = Image.open(file_path)
temp_image_path = f"temp_{filename}"
brightness = request.form.get("brightness")
contrast = request.form.get("contrast")
hue = request.form.get("hue")
saturation = request.form.get("saturation")
brightnessFac = float(brightness)
contrastFac = float(contrast)
hueFac = float(hue)
saturationFac = float(saturation)
# MENGECEK APAKAH FILE BERHASIL DIUPLOAD
print(file_path, ", Temp image:", temp_image_path)
################ BRIGHTNESS ################
# Min 0, default 1, max 10
brightnessFilter = ImageEnhance.Brightness(new_image)
temp_edited_image = brightnessFilter.enhance(brightnessFac)
temp_edited_image.save(temp_image_path)
################ CONTRAST ################
# Min 0, default 1, max 10
temp_new_image_contrast = Image.open(temp_image_path)
contrastFilter = ImageEnhance.Contrast(temp_new_image_contrast)
temp_edited_image = contrastFilter.enhance(contrastFac)
temp_edited_image.save(temp_image_path)
# ################ SATURATION ################
# Min 0, default 1, max 10
temp_new_image_saturation = Image.open(temp_image_path)
saturationFilter = ImageEnhance.Color(temp_new_image_saturation)
temp_edited_image = saturationFilter.enhance(saturationFac)
temp_edited_image.save(temp_image_path)
# ############### HUE COLOR ################
# Min 0, default 1, max 10
temp_edited_image = adjust_hue_pil(temp_image_path, hueFac)
cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], 'adjust_' + filename), cv2.cvtColor(np.array(temp_edited_image), cv2.COLOR_RGB2BGR))
os.remove(temp_image_path)
return render_template("adjustment.html", image=filename, image_output="adjust_" + filename)
return render_template("adjustment.html")
@app.route("/sharp", methods=["GET", "POST"])
def sharp():
if request.method == "POST":
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
########################################################
# USER ENTRIES #
########################################################
input_path = file_path
output_path = f"temp_upscaled_{filename}"
model_name = "realesrgan-x4plus-anime"
command = ["realesrgan-ncnn-vulkan.exe", "-i", input_path, "-o", output_path, "-n", model_name]
subprocess.run(command)
upscaled_image = cv2.imread(output_path)
cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], 'upscaled_' + filename), upscaled_image)
os.remove(output_path)
return render_template("sharp.html", image=filename, image_output="upscaled_" + filename)
return render_template("sharp.html")
if __name__ == "__main__":
app.run(debug=True)