CSB261 commited on
Commit
bf7dd36
ยท
verified ยท
1 Parent(s): 9ddcb40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
app.py CHANGED
@@ -13,35 +13,41 @@ def process_image(
13
  contrast,
14
  saturation
15
  ):
 
 
 
 
 
16
  # Pillow Image -> Numpy array ๋ณ€ํ™˜
17
  img_np = np.array(img)
 
18
  # ================ 1. ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ ================
19
- # ์ฑ„๋„ ์ •๋ณด๊ฐ€ 2D(ํ‘๋ฐฑ) ๋˜๋Š” 3D(RGB)์ธ์ง€ ํ™•์ธ
20
  if len(img_np.shape) == 3:
21
- # RGB ์ด๋ฏธ์ง€์ธ ๊ฒฝ์šฐ
22
- # scikit-image์˜ denoise ํ•จ์ˆ˜๋Š” (H, W, C) ํ˜•ํƒœ๋ฅผ ์ง€์›
23
- sigma_est = np.mean(estimate_sigma(img_np, multichannel=True))
24
- # denoise_strength๋ฅผ h ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ (์ž„์˜ ์„ค์ • ๊ฐ€๋Šฅ)
25
  denoised = denoise_nl_means(
26
  img_np,
27
  h=denoise_strength * sigma_est,
28
  patch_size=5,
29
  patch_distance=3,
30
- multichannel=True,
31
  fast_mode=True
32
  )
33
  else:
34
- # ๊ทธ๋ ˆ์ด์Šค์ผ€์ผ(ํ‘๋ฐฑ)์ธ ๊ฒฝ์šฐ
35
- sigma_est = np.mean(estimate_sigma(img_np, multichannel=False))
36
  denoised = denoise_nl_means(
37
  img_np,
38
  h=denoise_strength * sigma_est,
39
  patch_size=5,
40
  patch_distance=3,
41
- multichannel=False,
42
  fast_mode=True
43
  )
44
- denoised = np.clip(denoised, 0, 1) # [0,1] ๋ฒ”์œ„๋กœ ํด๋ฆฌํ•‘
 
 
45
  denoised = (denoised * 255).astype(np.uint8)
46
  denoised_img = Image.fromarray(denoised)
47
 
@@ -50,6 +56,7 @@ def process_image(
50
  sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
51
 
52
  # ================ 3. ๊ฐ๋งˆ ๋ณด์ • ================
 
53
  gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
54
  gamma_corrected = np.power(gamma_np, 1.0 / gamma)
55
  gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
@@ -81,10 +88,14 @@ def generate_output(
81
  contrast,
82
  saturation
83
  ):
 
 
 
 
84
  if input_image is None:
85
  return None, None
86
 
87
- # ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€
88
  transformed = process_image(
89
  input_image,
90
  denoise_strength,
@@ -95,19 +106,21 @@ def generate_output(
95
  saturation
96
  )
97
 
98
- # ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•œ JPG ํŒŒ์ผ ์ƒ์„ฑ
99
  with io.BytesIO() as output:
100
  transformed.save(output, format="JPEG")
101
  contents = output.getvalue()
102
 
103
- # ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€์™€ ํ•จ๊ป˜, Gradio์˜ File ํ˜•์‹์œผ๋กœ (ํŒŒ์ผ bytes, MIME ํƒ€์ž…, ๋‹ค์šด๋กœ๋“œ ์‹œ ํŒŒ์ผ๋ช…) ๋ฐ˜ํ™˜
104
- return transformed, (contents, "image/jpeg", "transformed.jpg")
 
 
 
105
 
106
  def main():
107
  with gr.Blocks() as demo:
108
  gr.Markdown("## ํ‘๋ฐฑ ๋ถ„์œ„๊ธฐ ์‚ฌ์ง„ ๋ณ€ํ™˜ ๋ฐ๋ชจ")
109
 
110
- # ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
111
  with gr.Row():
112
  input_image = gr.Image(
113
  label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ",
@@ -140,10 +153,15 @@ def main():
140
  label="์ฑ„๋„ (saturation)"
141
  )
142
 
143
- # ๊ฒฐ๊ณผ ์˜์—ญ
144
  with gr.Row():
145
- output_image = gr.Image(label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€", height=300)
146
- download_file = gr.File(label="JPG ๋‹ค์šด๋กœ๋“œ")
 
 
 
 
 
147
 
148
  # ๋ณ€ํ™˜ ์‹คํ–‰ ๋ฒ„ํŠผ
149
  btn = gr.Button("๋ณ€ํ™˜ ์‹คํ–‰")
@@ -161,10 +179,10 @@ def main():
161
  outputs=[output_image, download_file]
162
  )
163
 
164
- # ์›๋ณธ vs ๊ฒฐ๊ณผ ๋น„๊ต ๋ฒ„ํŠผ
165
  compare_output = gr.Image(
166
  type="pil",
167
- label="๋น„๊ต ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€",
168
  height=300
169
  )
170
  compare_btn = gr.Button("์›๋ณธ vs ๋ณ€ํ™˜๋ณธ ๋น„๊ต")
@@ -189,13 +207,14 @@ def main():
189
  contrast,
190
  saturation
191
  )
192
- # ๋‘ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€๋กœ๋กœ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค๊ธฐ
193
  input_w, input_h = input_image.size
194
  transformed_w, transformed_h = transformed.size
195
  new_w = input_w + transformed_w
196
  new_h = max(input_h, transformed_h)
197
 
198
  new_image = Image.new("RGB", (new_w, new_h))
 
199
  new_image.paste(input_image.convert("RGB"), (0, 0))
200
  new_image.paste(transformed.convert("RGB"), (input_w, 0))
201
 
 
13
  contrast,
14
  saturation
15
  ):
16
+ """
17
+ ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ(๋…ธ์ด์ฆˆ ์ œ๊ฑฐ, ์ƒคํ”„๋‹, ๊ฐ๋งˆ ๋ณด์ •, ๋ฐ๊ธฐ, ๋Œ€๋น„, ์ฑ„๋„)์— ๋”ฐ๋ผ
18
+ ์ตœ์ข…์ ์œผ๋กœ ํ‘๋ฐฑ ์‚ฌ์ง„์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
19
+ """
20
+
21
  # Pillow Image -> Numpy array ๋ณ€ํ™˜
22
  img_np = np.array(img)
23
+
24
  # ================ 1. ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ ================
25
+ # ์ด๋ฏธ์ง€๋ฅผ RGB(3์ฐจ์›)์ธ์ง€, ํ‘๋ฐฑ(2์ฐจ์›)์ธ์ง€ ํŒ๋ณ„
26
  if len(img_np.shape) == 3:
27
+ # RGB ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ channel_axis=-1 ์‚ฌ์šฉ
28
+ sigma_est = np.mean(estimate_sigma(img_np, channel_axis=-1))
 
 
29
  denoised = denoise_nl_means(
30
  img_np,
31
  h=denoise_strength * sigma_est,
32
  patch_size=5,
33
  patch_distance=3,
34
+ channel_axis=-1, # multichannel=True ๋Œ€์‹  channel_axis=-1
35
  fast_mode=True
36
  )
37
  else:
38
+ # ํ‘๋ฐฑ(2์ฐจ์›) ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ channel_axis=None
39
+ sigma_est = np.mean(estimate_sigma(img_np, channel_axis=None))
40
  denoised = denoise_nl_means(
41
  img_np,
42
  h=denoise_strength * sigma_est,
43
  patch_size=5,
44
  patch_distance=3,
45
+ channel_axis=None, # multichannel=False ๋Œ€์‹  channel_axis=None
46
  fast_mode=True
47
  )
48
+
49
+ # denoise ๊ฒฐ๊ณผ๋Š” [0,1] ๋ฒ”์œ„์ด๋ฏ€๋กœ, ๋‹ค์‹œ [0,255]๋กœ ๋ณ€ํ™˜
50
+ denoised = np.clip(denoised, 0, 1)
51
  denoised = (denoised * 255).astype(np.uint8)
52
  denoised_img = Image.fromarray(denoised)
53
 
 
56
  sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
57
 
58
  # ================ 3. ๊ฐ๋งˆ ๋ณด์ • ================
59
+ # Pillow์— ๊ฐ๋งˆ ๋ณด์ •์ด ์—†์œผ๋ฏ€๋กœ, NumPy๋กœ ์ฒ˜๋ฆฌ
60
  gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
61
  gamma_corrected = np.power(gamma_np, 1.0 / gamma)
62
  gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
 
88
  contrast,
89
  saturation
90
  ):
91
+ """
92
+ Gradio์—์„œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋กœ, ๋ณ€ํ™˜๋œ ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋ฅผ
93
+ ๋ฐ”๋กœ ํ‘œ์‹œํ•  ์ด๋ฏธ์ง€์™€ ๋‹ค์šด๋กœ๋“œ์šฉ ํŒŒ์ผ๋กœ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
94
+ """
95
  if input_image is None:
96
  return None, None
97
 
98
+ # 1) ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€ (PIL)
99
  transformed = process_image(
100
  input_image,
101
  denoise_strength,
 
106
  saturation
107
  )
108
 
109
+ # 2) ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•œ JPG ํŒŒ์ผ ์ƒ์„ฑ
110
  with io.BytesIO() as output:
111
  transformed.save(output, format="JPEG")
112
  contents = output.getvalue()
113
 
114
+ # Gradio์˜ File ํ˜•์‹: (ํŒŒ์ผ ๋ฐ”์ดํŠธ, MIME ํƒ€์ž…, ๋‹ค์šด๋กœ๋“œ ์‹œ ํ‘œ์‹œ๋  ํŒŒ์ผ๋ช…)
115
+ file_data = (contents, "image/jpeg", "transformed.jpg")
116
+
117
+ # ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€๋ฅผ Gradio์˜ Image๋กœ ๋ณด์—ฌ์ฃผ๊ณ , ๋‹ค์šด๋กœ๋“œ๋„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ๋‘ ๊ฐ€์ง€๋ฅผ ๋ฐ˜ํ™˜
118
+ return transformed, file_data
119
 
120
  def main():
121
  with gr.Blocks() as demo:
122
  gr.Markdown("## ํ‘๋ฐฑ ๋ถ„์œ„๊ธฐ ์‚ฌ์ง„ ๋ณ€ํ™˜ ๋ฐ๋ชจ")
123
 
 
124
  with gr.Row():
125
  input_image = gr.Image(
126
  label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ",
 
153
  label="์ฑ„๋„ (saturation)"
154
  )
155
 
156
+ # ๊ฒฐ๊ณผ(๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€, ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ)
157
  with gr.Row():
158
+ output_image = gr.Image(
159
+ label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€",
160
+ height=300
161
+ )
162
+ download_file = gr.File(
163
+ label="JPG ๋‹ค์šด๋กœ๋“œ"
164
+ )
165
 
166
  # ๋ณ€ํ™˜ ์‹คํ–‰ ๋ฒ„ํŠผ
167
  btn = gr.Button("๋ณ€ํ™˜ ์‹คํ–‰")
 
179
  outputs=[output_image, download_file]
180
  )
181
 
182
+ # ์›๋ณธ vs ๊ฒฐ๊ณผ ๋น„๊ต
183
  compare_output = gr.Image(
184
  type="pil",
185
+ label="์›๋ณธ vs ๋ณ€ํ™˜๋ณธ ๋น„๊ต ๊ฒฐ๊ณผ",
186
  height=300
187
  )
188
  compare_btn = gr.Button("์›๋ณธ vs ๋ณ€ํ™˜๋ณธ ๋น„๊ต")
 
207
  contrast,
208
  saturation
209
  )
210
+ # ๋‘ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€๋กœ๋กœ ํ•ฉ์ณ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ ๋‹ค
211
  input_w, input_h = input_image.size
212
  transformed_w, transformed_h = transformed.size
213
  new_w = input_w + transformed_w
214
  new_h = max(input_h, transformed_h)
215
 
216
  new_image = Image.new("RGB", (new_w, new_h))
217
+ # ์›๋ณธ(RGB), ๋ณ€ํ™˜๋ณธ(RGB)
218
  new_image.paste(input_image.convert("RGB"), (0, 0))
219
  new_image.paste(transformed.convert("RGB"), (input_w, 0))
220