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):
|
|
@@ -58,7 +58,7 @@ model.load_state_dict(checkpoint['generator'])
|
|
| 58 |
model.eval()
|
| 59 |
|
| 60 |
# ---------------------------
|
| 61 |
-
# TRANSFORM
|
| 62 |
# ---------------------------
|
| 63 |
|
| 64 |
transform = transforms.ToTensor()
|
|
@@ -79,41 +79,41 @@ def enhance_image(input_image):
|
|
| 79 |
# Convert tensor → numpy
|
| 80 |
output = output.squeeze().permute(1, 2, 0).cpu().numpy()
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
# ---------------------------
|
| 85 |
-
if output.min() < 0:
|
| 86 |
-
# Model output is [-1,1]
|
| 87 |
output = (output + 1) / 2
|
| 88 |
|
| 89 |
-
# Clamp safely
|
| 90 |
output = np.clip(output, 0, 1)
|
| 91 |
|
| 92 |
-
# Convert to image
|
| 93 |
output_img = (output * 255).astype(np.uint8)
|
| 94 |
|
| 95 |
-
# Resize back
|
| 96 |
-
output_img = Image.fromarray(output_img)
|
|
|
|
| 97 |
output_img = np.array(output_img)
|
| 98 |
|
| 99 |
# ---------------------------
|
| 100 |
-
#
|
| 101 |
# ---------------------------
|
| 102 |
|
| 103 |
-
#
|
| 104 |
output_img = cv2.GaussianBlur(output_img, (3, 3), 0)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
kernel = np.array([
|
| 108 |
[0, -1, 0],
|
| 109 |
-
[-1,
|
| 110 |
[0, -1, 0]
|
| 111 |
])
|
| 112 |
output_img = cv2.filter2D(output_img, -1, kernel)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
output_img = np.clip(output_img, 0, 255)
|
| 115 |
|
| 116 |
-
# Save
|
| 117 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 118 |
Image.fromarray(output_img).save(temp_file.name)
|
| 119 |
|
|
|
|
| 38 |
|
| 39 |
self.exit = nn.Sequential(
|
| 40 |
nn.Conv2d(64, 3, 3, 1, 1),
|
| 41 |
+
nn.Sigmoid() # output [0,1]
|
| 42 |
)
|
| 43 |
|
| 44 |
def forward(self, x):
|
|
|
|
| 58 |
model.eval()
|
| 59 |
|
| 60 |
# ---------------------------
|
| 61 |
+
# TRANSFORM
|
| 62 |
# ---------------------------
|
| 63 |
|
| 64 |
transform = transforms.ToTensor()
|
|
|
|
| 79 |
# Convert tensor → numpy
|
| 80 |
output = output.squeeze().permute(1, 2, 0).cpu().numpy()
|
| 81 |
|
| 82 |
+
# Handle range
|
| 83 |
+
if output.min() < 0:
|
|
|
|
|
|
|
|
|
|
| 84 |
output = (output + 1) / 2
|
| 85 |
|
|
|
|
| 86 |
output = np.clip(output, 0, 1)
|
| 87 |
|
|
|
|
| 88 |
output_img = (output * 255).astype(np.uint8)
|
| 89 |
|
| 90 |
+
# Resize back to original size
|
| 91 |
+
output_img = Image.fromarray(output_img)
|
| 92 |
+
output_img = output_img.resize(original_size, Image.BICUBIC)
|
| 93 |
output_img = np.array(output_img)
|
| 94 |
|
| 95 |
# ---------------------------
|
| 96 |
+
# FINAL SAFE POST-PROCESSING
|
| 97 |
# ---------------------------
|
| 98 |
|
| 99 |
+
# Light smoothing
|
| 100 |
output_img = cv2.GaussianBlur(output_img, (3, 3), 0)
|
| 101 |
|
| 102 |
+
# Proper sharpening
|
| 103 |
kernel = np.array([
|
| 104 |
[0, -1, 0],
|
| 105 |
+
[-1, 5, -1],
|
| 106 |
[0, -1, 0]
|
| 107 |
])
|
| 108 |
output_img = cv2.filter2D(output_img, -1, kernel)
|
| 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
|
| 117 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 118 |
Image.fromarray(output_img).save(temp_file.name)
|
| 119 |
|