Update inference_realesrgan.py
Browse files3 unmodified lines45678910111 unmodified line1314151617181920212223242526272829303132339 unmodified lines434445464748499 unmodified lines596061623 unmodified linesimport cv2import numpy as npfrom PIL import Image# ๐ Load Real-ESRGAN model manually from weight fileMODEL_PATH = "weight/RealESRGAN_x4plus.pth"# Ensure the model exists1 unmodified lineprint(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")exit(1)# ๐ Fix: Manually load the ESRGAN model without requiring 'realesrgan' packagefrom torch.hub import load_state_dict_from_urlmodel_url = "https://huggingface.co/va1/CViT-Deepfake-Model/resolve/main/RealESRGAN_x4plus.pth"# Load weights if missingif not os.path.exists(MODEL_PATH):torch.hub.download_url_to_file(model_url, MODEL_PATH)# Initialize ESRGAN modeldevice = "cuda" if torch.cuda.is_available() else "cpu"# ๐ Fix: Directly use ESRGAN weight without requiring 'realesrgan'from model.esrgan import RealESRGANerupsampler = RealESRGANer(scale=4, model_path=MODEL_PATH, model=None, tile=0, tile_pad=10, pre_pad=0,half=True if torch.cuda.is_available() else False)def upscale_image(input_path, output_path):"""Upscales an image using Real-ESRGAN."""9 unmodified lines# Apply super-resolutionprint(f"[INFO] Upscaling image: {input_path}")output, _ = upsampler.enhance(np.array(img_pil), outscale=2)output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)# Save upscaled image9 unmodified linesparser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")args = parser.parse_args()upscale_image(args.input, args.output)3 unmodified lines4567891011121 unmodified line141516171819202122232425269 unmodified lines363738394041429 unmodified lines525354553 unmodified linesimport cv2import numpy as npfrom PIL import Imagefrom realesrgan import RealESRGANer# Load Real-ESRGAN model manually from weight fileMODEL_PATH = "weight/RealESRGAN_x4plus.pth"# Ensure the model exists1 unmodified lineprint(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")exit(1)# Initialize the RealESRGANer instancerealesrgan = RealESRGANer(scale=4, # Assuming a scale of 4xmodel_path=MODEL_PATH,device='cuda' if torch.cuda.is_available() else 'cpu', # Use GPU if availablehalf=False # Set to True if you want to use half precision)def upscale_image(input_path, output_path):"""Upscales an image using Real-ESRGAN."""9 unmodified lines# Apply super-resolutionprint(f"[INFO] Upscaling image: {input_path}")output, _ = realesrgan.enhance(np.array(img_pil), outscale=2)output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)# Save upscaled image9 unmodified linesparser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")args = parser.parse_args()upscale_image(args.input, args.output)
inference_realesrgan.py+11-18