Update inference_realesrgan.py
Browse files- inference_realesrgan.py +11 -18
inference_realesrgan.py
CHANGED
|
@@ -4,8 +4,9 @@ import torch
|
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
MODEL_PATH = "weight/RealESRGAN_x4plus.pth"
|
| 10 |
|
| 11 |
# Ensure the model exists
|
|
@@ -13,21 +14,13 @@ if not os.path.exists(MODEL_PATH):
|
|
| 13 |
print(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")
|
| 14 |
exit(1)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
if
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Initialize ESRGAN model
|
| 25 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
-
|
| 27 |
-
# 🚀 Fix: Directly use ESRGAN weight without requiring 'realesrgan'
|
| 28 |
-
from model.esrgan import RealESRGANer
|
| 29 |
-
upsampler = RealESRGANer(scale=4, model_path=MODEL_PATH, model=None, tile=0, tile_pad=10, pre_pad=0,
|
| 30 |
-
half=True if torch.cuda.is_available() else False)
|
| 31 |
|
| 32 |
def upscale_image(input_path, output_path):
|
| 33 |
"""Upscales an image using Real-ESRGAN."""
|
|
@@ -43,7 +36,7 @@ def upscale_image(input_path, output_path):
|
|
| 43 |
|
| 44 |
# Apply super-resolution
|
| 45 |
print(f"[INFO] Upscaling image: {input_path}")
|
| 46 |
-
output, _ =
|
| 47 |
output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
|
| 48 |
|
| 49 |
# Save upscaled image
|
|
@@ -59,4 +52,4 @@ if __name__ == "__main__":
|
|
| 59 |
parser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")
|
| 60 |
args = parser.parse_args()
|
| 61 |
|
| 62 |
-
upscale_image(args.input, args.output)
|
|
|
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
+
from realesrgan import RealESRGANer
|
| 8 |
|
| 9 |
+
# Load Real-ESRGAN model manually from weight file
|
| 10 |
MODEL_PATH = "weight/RealESRGAN_x4plus.pth"
|
| 11 |
|
| 12 |
# Ensure the model exists
|
|
|
|
| 14 |
print(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")
|
| 15 |
exit(1)
|
| 16 |
|
| 17 |
+
# Initialize the RealESRGANer instance
|
| 18 |
+
realesrgan = RealESRGANer(
|
| 19 |
+
scale=4, # Assuming a scale of 4x
|
| 20 |
+
model_path=MODEL_PATH,
|
| 21 |
+
device='cuda' if torch.cuda.is_available() else 'cpu', # Use GPU if available
|
| 22 |
+
half=False # Set to True if you want to use half precision
|
| 23 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def upscale_image(input_path, output_path):
|
| 26 |
"""Upscales an image using Real-ESRGAN."""
|
|
|
|
| 36 |
|
| 37 |
# Apply super-resolution
|
| 38 |
print(f"[INFO] Upscaling image: {input_path}")
|
| 39 |
+
output, _ = realesrgan.enhance(np.array(img_pil), outscale=2)
|
| 40 |
output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
|
| 41 |
|
| 42 |
# Save upscaled image
|
|
|
|
| 52 |
parser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")
|
| 53 |
args = parser.parse_args()
|
| 54 |
|
| 55 |
+
upscale_image(args.input, args.output)
|