Update app.py
Browse files
app.py
CHANGED
@@ -7,60 +7,51 @@ def apply_filter(image, filter_type, intensity):
|
|
7 |
normalized_intensity = intensity / 100.0
|
8 |
|
9 |
if filter_type == "Grayscale":
|
10 |
-
# ํ๋ฐฑ ๋ณํ
|
11 |
return convert_to_grayscale(image)
|
12 |
elif filter_type == "Soft Glow":
|
13 |
-
# Soft Glow ํจ๊ณผ
|
14 |
gaussian = cv2.GaussianBlur(image, (15, 15), 0)
|
15 |
soft_glow = cv2.addWeighted(image, 1 - normalized_intensity, gaussian, normalized_intensity, 0)
|
16 |
return soft_glow
|
17 |
elif filter_type == "Portrait Enhancer":
|
18 |
# ๊ฐ๋จํ ํฌํธ๋ ์ดํธ ํฅ์ (๋ถ๋๋ฝ๊ฒ ์กฐ์ ๋ ๋๋น ๋ฐ ๋ฐ๊ธฐ)
|
19 |
-
enhanced = cv2.detailEnhance(image, sigma_s=5, sigma_r=0.
|
20 |
-
|
|
|
|
|
21 |
elif filter_type == "Warm Tone":
|
22 |
-
# ๋ฐ๋ปํ ํค ํจ๊ณผ (๋นจ๊ฐ ๋ฐ ๋
ธ๋ ํค์ ๊ฐ์กฐ)
|
23 |
increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
|
24 |
[0.0, 1.0, 0.0],
|
25 |
[0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
|
26 |
warm_image = cv2.transform(image, increase_red)
|
27 |
return warm_image
|
28 |
elif filter_type == "Cold Tone":
|
29 |
-
# ์ฐจ๊ฐ์ด ํค ํจ๊ณผ (ํ๋ ํค์ ๊ฐ์กฐ)
|
30 |
increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
|
31 |
[0.0, 1.0, 0.0],
|
32 |
[0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
|
33 |
cold_image = cv2.transform(image, increase_blue)
|
34 |
return cold_image
|
35 |
elif filter_type == "High-Key":
|
36 |
-
# High-Key ํจ๊ณผ (๋ฐ๊ธฐ ์ฆ๊ฐ)
|
37 |
high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
|
38 |
return high_key
|
39 |
elif filter_type == "Low-Key":
|
40 |
-
# Low-Key ํจ๊ณผ (๋ฐ๊ธฐ ๊ฐ์)
|
41 |
low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
|
42 |
return low_key
|
43 |
elif filter_type == "Haze":
|
44 |
-
# Haze ํจ๊ณผ
|
45 |
haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
|
46 |
return haze
|
47 |
else:
|
48 |
return image
|
49 |
|
50 |
def convert_to_grayscale(image):
|
51 |
-
# ์ด๋ฏธ์ง๋ฅผ ํ๋ฐฑ์ผ๋ก ๋ณํ
|
52 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
53 |
return gray_image
|
54 |
|
55 |
def convert_and_save(image, filter_type, intensity):
|
56 |
-
# ์ ํํ ํํฐ์ ๊ฐ๋๋ฅผ ์ด๋ฏธ์ง์ ์ ์ฉ
|
57 |
filtered_image = apply_filter(image, filter_type, intensity)
|
58 |
-
# ์ด๋ฏธ์ง๋ฅผ ์ ์ฅ
|
59 |
output_path = "output.jpg"
|
60 |
cv2.imwrite(output_path, filtered_image)
|
61 |
return filtered_image, output_path
|
62 |
|
63 |
-
# Gradio ์ธํฐํ์ด์ค ์ ์
|
64 |
iface = gr.Interface(
|
65 |
fn=convert_and_save,
|
66 |
inputs=[
|
|
|
7 |
normalized_intensity = intensity / 100.0
|
8 |
|
9 |
if filter_type == "Grayscale":
|
|
|
10 |
return convert_to_grayscale(image)
|
11 |
elif filter_type == "Soft Glow":
|
|
|
12 |
gaussian = cv2.GaussianBlur(image, (15, 15), 0)
|
13 |
soft_glow = cv2.addWeighted(image, 1 - normalized_intensity, gaussian, normalized_intensity, 0)
|
14 |
return soft_glow
|
15 |
elif filter_type == "Portrait Enhancer":
|
16 |
# ๊ฐ๋จํ ํฌํธ๋ ์ดํธ ํฅ์ (๋ถ๋๋ฝ๊ฒ ์กฐ์ ๋ ๋๋น ๋ฐ ๋ฐ๊ธฐ)
|
17 |
+
enhanced = cv2.detailEnhance(image, sigma_s=5, sigma_r=0.05 + 0.1 * normalized_intensity)
|
18 |
+
# ํผ๋ถ ๋ถ๋๋ฝ๊ฒ ํ๊ธฐ ์ํด ์ถ๊ฐ์ ์ธ ๋ธ๋ฌ ์ ์ฉ
|
19 |
+
smoothed = cv2.bilateralFilter(enhanced, d=9, sigmaColor=75, sigmaSpace=75)
|
20 |
+
return smoothed
|
21 |
elif filter_type == "Warm Tone":
|
|
|
22 |
increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
|
23 |
[0.0, 1.0, 0.0],
|
24 |
[0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
|
25 |
warm_image = cv2.transform(image, increase_red)
|
26 |
return warm_image
|
27 |
elif filter_type == "Cold Tone":
|
|
|
28 |
increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
|
29 |
[0.0, 1.0, 0.0],
|
30 |
[0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
|
31 |
cold_image = cv2.transform(image, increase_blue)
|
32 |
return cold_image
|
33 |
elif filter_type == "High-Key":
|
|
|
34 |
high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
|
35 |
return high_key
|
36 |
elif filter_type == "Low-Key":
|
|
|
37 |
low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
|
38 |
return low_key
|
39 |
elif filter_type == "Haze":
|
|
|
40 |
haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
|
41 |
return haze
|
42 |
else:
|
43 |
return image
|
44 |
|
45 |
def convert_to_grayscale(image):
|
|
|
46 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
47 |
return gray_image
|
48 |
|
49 |
def convert_and_save(image, filter_type, intensity):
|
|
|
50 |
filtered_image = apply_filter(image, filter_type, intensity)
|
|
|
51 |
output_path = "output.jpg"
|
52 |
cv2.imwrite(output_path, filtered_image)
|
53 |
return filtered_image, output_path
|
54 |
|
|
|
55 |
iface = gr.Interface(
|
56 |
fn=convert_and_save,
|
57 |
inputs=[
|