jpjp9292 commited on
Commit
eb901ad
โ€ข
1 Parent(s): 1b9e00e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -16
app.py CHANGED
@@ -1,34 +1,71 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
 
4
 
5
- # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ ํ•จ์ˆ˜
6
  def upscale_image(input_image, upscale_factor):
7
- upscale_factor = int(upscale_factor) # ์—…์Šค์ผ€์ผ ๋น„์œจ์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜
8
- # ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ฃผ์–ด์ง„ ์—…์Šค์ผ€์ผ ๋น„์œจ๋กœ ํฌ๊ธฐ ๋ณ€๊ฒฝ (๋ณด๊ฐ„๋ฒ•: INTER_CUBIC)
9
- output_image = cv2.resize(input_image, None, fx=upscale_factor, fy=upscale_factor, interpolation=cv2.INTER_CUBIC)
10
- # ์ด๋ฏธ์ง€๋ฅผ PNG ํ˜•์‹์œผ๋กœ ์ €์žฅ
11
- output_image_bgr = cv2.cvtColor(output_image, cv2.COLOR_RGB2BGR) # RGB์—์„œ BGR๋กœ ๋ณ€ํ™˜
12
- output_filename = "upscaled_image.png"
13
- cv2.imwrite(output_filename, output_image_bgr)
14
- return output_filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # ์ธํ„ฐํŽ˜์ด์Šค ์„ค๋ช…
17
  DESCRIPTION = """
18
- ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋ณด์„ธ์š”! (you can increase the size and quality of your images)
19
- """
 
20
 
21
- # ์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ ์„ ํƒ ๋ผ๋””์˜ค ๋ฒ„ํŠผ
22
- radio_input = gr.Radio(label="์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ์„ ์„ ํƒํ•ด ์ฃผ์„ธ์š” (Select Upscaling level)", choices=[2, 4, 6, 8, 10], value=2)
 
 
 
23
 
24
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
25
  demo = gr.Interface(
26
  fn=upscale_image,
27
- inputs=[gr.Image(label="์ž…๋ ฅ ์ด๋ฏธ์ง€ (Input Image)", type="numpy"), radio_input],
 
 
 
 
 
 
 
 
28
  outputs=gr.File(label="์—…์Šค์ผ€์ผ๋œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ (Download Upscaled Image)"),
29
  title="Image Upscaler",
30
- description=DESCRIPTION
 
 
 
 
31
  )
32
 
33
  # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
34
- demo.launch(show_api=False)
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ import os
5
+ from PIL import Image
6
 
 
7
  def upscale_image(input_image, upscale_factor):
8
+ try:
9
+ # PIL Image๋กœ ๋ณ€ํ™˜
10
+ if isinstance(input_image, np.ndarray):
11
+ input_image = Image.fromarray(input_image)
12
+
13
+ # ํ˜„์žฌ ์ด๋ฏธ์ง€ ํฌ๊ธฐ
14
+ current_width, current_height = input_image.size
15
+
16
+ # ์ƒˆ๋กœ์šด ํฌ๊ธฐ ๊ณ„์‚ฐ
17
+ new_width = int(current_width * upscale_factor)
18
+ new_height = int(current_height * upscale_factor)
19
+
20
+ # ์ด๋ฏธ์ง€ ๋ฆฌ์‚ฌ์ด์ฆˆ
21
+ output_image = input_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
22
+
23
+ # ์ž„์‹œ ํŒŒ์ผ ์ €์žฅ์„ ์œ„ํ•œ ๋””๋ ‰ํ† ๋ฆฌ ํ™•์ธ ๋ฐ ์ƒ์„ฑ
24
+ if not os.path.exists("temp"):
25
+ os.makedirs("temp")
26
+
27
+ # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ์ €์žฅ
28
+ output_path = os.path.join("temp", "upscaled_image.png")
29
+ output_image.save(output_path, "PNG", quality=100)
30
+
31
+ return output_path
32
+
33
+ except Exception as e:
34
+ print(f"Error in upscale_image: {str(e)}")
35
+ raise gr.Error("์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.")
36
 
37
  # ์ธํ„ฐํŽ˜์ด์Šค ์„ค๋ช…
38
  DESCRIPTION = """
39
+ # ์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ๋Ÿฌ (Image Upscaler)
40
+ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋ณด์„ธ์š”!
41
+ You can increase the size and quality of your images!
42
 
43
+ ## ์‚ฌ์šฉ ๋ฐฉ๋ฒ• (How to use):
44
+ 1. ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š” (Upload an image)
45
+ 2. ์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ์„ ์„ ํƒํ•˜์„ธ์š” (Select upscale level)
46
+ 3. Submit ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์„ธ์š” (Click submit)
47
+ """
48
 
49
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
50
  demo = gr.Interface(
51
  fn=upscale_image,
52
+ inputs=[
53
+ gr.Image(label="์ž…๋ ฅ ์ด๋ฏธ์ง€ (Input Image)", type="pil"),
54
+ gr.Radio(
55
+ label="์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ (Upscale Level)",
56
+ choices=[2, 4, 6, 8],
57
+ value=2,
58
+ type="number"
59
+ )
60
+ ],
61
  outputs=gr.File(label="์—…์Šค์ผ€์ผ๋œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ (Download Upscaled Image)"),
62
  title="Image Upscaler",
63
+ description=DESCRIPTION,
64
+ allow_flagging="never",
65
+ examples=[
66
+ ["example.jpg", 2] # examples ํด๋”์— example.jpg ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
67
+ ]
68
  )
69
 
70
  # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
71
+ demo.launch()