Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,7 +38,7 @@ class Generator(nn.Module):
|
|
| 38 |
|
| 39 |
self.exit = nn.Sequential(
|
| 40 |
nn.Conv2d(64, 3, 3, 1, 1),
|
| 41 |
-
nn.Sigmoid()
|
| 42 |
)
|
| 43 |
|
| 44 |
def forward(self, x):
|
|
@@ -87,7 +87,7 @@ def enhance_image(input_image):
|
|
| 87 |
|
| 88 |
output_img = (output * 255).astype(np.uint8)
|
| 89 |
|
| 90 |
-
# Resize back
|
| 91 |
output_img = Image.fromarray(output_img)
|
| 92 |
output_img = output_img.resize(original_size, Image.BICUBIC)
|
| 93 |
output_img = np.array(output_img)
|
|
@@ -96,21 +96,31 @@ def enhance_image(input_image):
|
|
| 96 |
# FINAL SAFE POST-PROCESSING
|
| 97 |
# ---------------------------
|
| 98 |
|
| 99 |
-
# Light smoothing
|
| 100 |
output_img = cv2.GaussianBlur(output_img, (3, 3), 0)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
[0, -1, 0],
|
| 105 |
[-1, 5, -1],
|
| 106 |
[0, -1, 0]
|
| 107 |
])
|
| 108 |
-
output_img = cv2.filter2D(output_img, -1,
|
| 109 |
|
| 110 |
-
# Blend with original (VERY IMPORTANT)
|
| 111 |
original_np = np.array(img.resize(original_size))
|
| 112 |
output_img = cv2.addWeighted(original_np, 0.7, output_img, 0.3, 0)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
output_img = np.clip(output_img, 0, 255)
|
| 115 |
|
| 116 |
# Save for download
|
|
|
|
| 38 |
|
| 39 |
self.exit = nn.Sequential(
|
| 40 |
nn.Conv2d(64, 3, 3, 1, 1),
|
| 41 |
+
nn.Sigmoid()
|
| 42 |
)
|
| 43 |
|
| 44 |
def forward(self, x):
|
|
|
|
| 87 |
|
| 88 |
output_img = (output * 255).astype(np.uint8)
|
| 89 |
|
| 90 |
+
# Resize back
|
| 91 |
output_img = Image.fromarray(output_img)
|
| 92 |
output_img = output_img.resize(original_size, Image.BICUBIC)
|
| 93 |
output_img = np.array(output_img)
|
|
|
|
| 96 |
# FINAL SAFE POST-PROCESSING
|
| 97 |
# ---------------------------
|
| 98 |
|
| 99 |
+
# 1. Light smoothing (remove artifacts)
|
| 100 |
output_img = cv2.GaussianBlur(output_img, (3, 3), 0)
|
| 101 |
|
| 102 |
+
# 2. Mild sharpening (safe)
|
| 103 |
+
sharpen_kernel = np.array([
|
| 104 |
[0, -1, 0],
|
| 105 |
[-1, 5, -1],
|
| 106 |
[0, -1, 0]
|
| 107 |
])
|
| 108 |
+
output_img = cv2.filter2D(output_img, -1, sharpen_kernel)
|
| 109 |
|
| 110 |
+
# 3. Blend with original (VERY IMPORTANT)
|
| 111 |
original_np = np.array(img.resize(original_size))
|
| 112 |
output_img = cv2.addWeighted(original_np, 0.7, output_img, 0.3, 0)
|
| 113 |
|
| 114 |
+
# 4. Adaptive contrast (CLAHE - BEST FINAL TOUCH)
|
| 115 |
+
lab = cv2.cvtColor(output_img, cv2.COLOR_RGB2LAB)
|
| 116 |
+
l, a, b = cv2.split(lab)
|
| 117 |
+
|
| 118 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 119 |
+
l = clahe.apply(l)
|
| 120 |
+
|
| 121 |
+
lab = cv2.merge((l, a, b))
|
| 122 |
+
output_img = cv2.cvtColor(lab, cv2.COLOR_LAB2RGB)
|
| 123 |
+
|
| 124 |
output_img = np.clip(output_img, 0, 255)
|
| 125 |
|
| 126 |
# Save for download
|