dev1461 commited on
Commit
5acf400
·
verified ·
1 Parent(s): 3c9c540

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -4,6 +4,8 @@ import torch.nn as nn
4
  from torchvision import transforms
5
  from PIL import Image
6
  import numpy as np
 
 
7
 
8
  # ---------------------------
9
  # MODEL ARCHITECTURE
@@ -60,7 +62,7 @@ model.eval()
60
  # ---------------------------
61
 
62
  transform = transforms.Compose([
63
- transforms.Resize((128,128)),
64
  transforms.ToTensor()
65
  ])
66
 
@@ -68,13 +70,9 @@ transform = transforms.Compose([
68
  # INFERENCE FUNCTION
69
  # ---------------------------
70
 
71
- import gradio as gr
72
- import numpy as np
73
- from PIL import Image
74
- import tempfile
75
-
76
  def enhance_image(input_image):
77
  img = input_image.convert("RGB")
 
78
 
79
  input_tensor = transform(img).unsqueeze(0).to(device)
80
 
@@ -84,19 +82,37 @@ def enhance_image(input_image):
84
  output_img = output.squeeze().permute(1,2,0).cpu().numpy()
85
  output_img = (output_img * 255).astype(np.uint8)
86
 
87
- # 🔥 SAVE TEMP FILE FOR DOWNLOAD
 
 
 
 
 
 
 
 
 
 
 
 
88
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
89
  Image.fromarray(output_img).save(temp_file.name)
90
 
91
  return output_img, temp_file.name
92
 
 
 
 
93
 
94
  with gr.Blocks() as demo:
95
- gr.Markdown("# 🔍 Image Super Resolution")
 
 
 
 
 
96
 
97
- input_img = gr.Image(type="pil", label="Upload Image")
98
- output_img = gr.Image(label="Enhanced Image")
99
- download_file = gr.File(label="Download Image")
100
 
101
  btn = gr.Button("Enhance Image")
102
 
@@ -106,19 +122,4 @@ with gr.Blocks() as demo:
106
  outputs=[output_img, download_file]
107
  )
108
 
109
- demo.launch()
110
-
111
- # ---------------------------
112
- # GRADIO UI
113
- # ---------------------------
114
-
115
- interface = gr.Interface(
116
- fn=enhance_image,
117
- inputs=gr.Image(type="pil", label="Upload Image"),
118
- outputs=gr.Image(type="numpy", label="Enhanced Image"),
119
- title="🔍 Super Resolution App",
120
- description="Upload a low-quality image and enhance it using deep learning",
121
- allow_flagging="never"
122
- )
123
-
124
- interface.launch()
 
4
  from torchvision import transforms
5
  from PIL import Image
6
  import numpy as np
7
+ import cv2
8
+ import tempfile
9
 
10
  # ---------------------------
11
  # MODEL ARCHITECTURE
 
62
  # ---------------------------
63
 
64
  transform = transforms.Compose([
65
+ transforms.Resize((128, 128)),
66
  transforms.ToTensor()
67
  ])
68
 
 
70
  # INFERENCE FUNCTION
71
  # ---------------------------
72
 
 
 
 
 
 
73
  def enhance_image(input_image):
74
  img = input_image.convert("RGB")
75
+ original_size = img.size
76
 
77
  input_tensor = transform(img).unsqueeze(0).to(device)
78
 
 
82
  output_img = output.squeeze().permute(1,2,0).cpu().numpy()
83
  output_img = (output_img * 255).astype(np.uint8)
84
 
85
+ # Resize back to original size
86
+ output_img = Image.fromarray(output_img)
87
+ output_img = output_img.resize(original_size, Image.BICUBIC)
88
+ output_img = np.array(output_img)
89
+
90
+ # Sharpen image (balanced)
91
+ blurred = cv2.GaussianBlur(output_img, (0,0), 1)
92
+ output_img = cv2.addWeighted(output_img, 1.3, blurred, -0.3, 0)
93
+
94
+ # Clip values
95
+ output_img = np.clip(output_img, 0, 255)
96
+
97
+ # Save for download
98
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
99
  Image.fromarray(output_img).save(temp_file.name)
100
 
101
  return output_img, temp_file.name
102
 
103
+ # ---------------------------
104
+ # GRADIO UI
105
+ # ---------------------------
106
 
107
  with gr.Blocks() as demo:
108
+ gr.Markdown("# 🔍 AI Image Enhancer")
109
+ gr.Markdown("Upload a low-quality image and enhance it using deep learning")
110
+
111
+ with gr.Row():
112
+ input_img = gr.Image(type="pil", label="Upload Image")
113
+ output_img = gr.Image(label="Enhanced Image")
114
 
115
+ download_file = gr.File(label="Download Enhanced Image")
 
 
116
 
117
  btn = gr.Button("Enhance Image")
118
 
 
122
  outputs=[output_img, download_file]
123
  )
124
 
125
+ demo.launch()