Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,34 +79,38 @@ def enhance_image(input_image):
|
|
| 79 |
with torch.no_grad():
|
| 80 |
output = model(input_tensor)
|
| 81 |
|
|
|
|
| 82 |
output_img = output.squeeze().permute(1,2,0).cpu().numpy()
|
| 83 |
output_img = (output_img * 255).astype(np.uint8)
|
| 84 |
|
| 85 |
-
# Resize back
|
| 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 |
-
#
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
output_img = cv2.GaussianBlur(output_img, (3,3), 0)
|
| 93 |
|
| 94 |
-
# 🔥 STEP 2:
|
|
|
|
|
|
|
|
|
|
| 95 |
sharpen_kernel = np.array([[0,-1,0],
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
output_img = np.clip(output_img, 0, 255)
|
| 103 |
|
| 104 |
# Save for download
|
| 105 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 106 |
-
Image.fromarray(
|
| 107 |
-
|
| 108 |
-
return output_img, temp_file.name
|
| 109 |
|
|
|
|
| 110 |
# ---------------------------
|
| 111 |
# GRADIO UI
|
| 112 |
# ---------------------------
|
|
|
|
| 79 |
with torch.no_grad():
|
| 80 |
output = model(input_tensor)
|
| 81 |
|
| 82 |
+
# Model output
|
| 83 |
output_img = output.squeeze().permute(1,2,0).cpu().numpy()
|
| 84 |
output_img = (output_img * 255).astype(np.uint8)
|
| 85 |
|
| 86 |
+
# Resize back
|
| 87 |
output_img = Image.fromarray(output_img)
|
| 88 |
output_img = output_img.resize(original_size, Image.BICUBIC)
|
| 89 |
output_img = np.array(output_img)
|
| 90 |
|
| 91 |
+
# 🔥 ORIGINAL IMAGE (for blending)
|
| 92 |
+
original_np = np.array(img)
|
| 93 |
+
|
| 94 |
+
# 🔥 STEP 1: LIGHT SMOOTHING (remove artifacts)
|
| 95 |
output_img = cv2.GaussianBlur(output_img, (3,3), 0)
|
| 96 |
|
| 97 |
+
# 🔥 STEP 2: BLEND WITH ORIGINAL (VERY IMPORTANT)
|
| 98 |
+
final_img = cv2.addWeighted(original_np, 0.6, output_img, 0.4, 0)
|
| 99 |
+
|
| 100 |
+
# 🔥 STEP 3: VERY LIGHT SHARPEN (safe)
|
| 101 |
sharpen_kernel = np.array([[0,-1,0],
|
| 102 |
+
[-1,5,-1],
|
| 103 |
+
[0,-1,0]])
|
| 104 |
|
| 105 |
+
final_img = cv2.filter2D(final_img, -1, sharpen_kernel)
|
| 106 |
|
| 107 |
+
final_img = np.clip(final_img, 0, 255)
|
|
|
|
| 108 |
|
| 109 |
# Save for download
|
| 110 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 111 |
+
Image.fromarray(final_img).save(temp_file.name)
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
return final_img, temp_file.name
|
| 114 |
# ---------------------------
|
| 115 |
# GRADIO UI
|
| 116 |
# ---------------------------
|