Spaces:
Build error
Build error
first commit
Browse files- app.py +52 -0
- models/RealESRGAN_x4plus.pth +3 -0
- real_esrgan_utils.py +22 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 5 |
+
from realesrgan import RealESRGANer
|
| 6 |
+
import cv2
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Fungsi upscaling Anda yang sudah ada
|
| 10 |
+
def upscale_image_gradio(input_image):
|
| 11 |
+
# Pastikan direktori models ada
|
| 12 |
+
model_path = 'models/RealESRGAN_x4plus.pth'
|
| 13 |
+
if not os.path.exists(model_path):
|
| 14 |
+
# Anda perlu memastikan model .pth diunduh ke folder 'models'
|
| 15 |
+
# atau Anda bisa mengunduhnya secara programatik jika tidak ada
|
| 16 |
+
raise FileNotFoundError(f"Model file not found at {model_path}. Please download it.")
|
| 17 |
+
|
| 18 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
|
| 19 |
+
num_block=23, num_grow_ch=32, scale=4)
|
| 20 |
+
upsampler = RealESRGANer(
|
| 21 |
+
scale=4,
|
| 22 |
+
model_path=model_path,
|
| 23 |
+
model=model,
|
| 24 |
+
tile=0, # Atur tile sesuai kebutuhan memori GPU/CPU Anda
|
| 25 |
+
tile_pad=10,
|
| 26 |
+
pre_pad=0,
|
| 27 |
+
half=False # Atur True jika Anda menggunakan GPU yang mendukung half-precision (FP16)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# input_image dari Gradio biasanya adalah numpy array (dari PIL Image yang di-load)
|
| 31 |
+
# Pastikan formatnya sesuai (BGR untuk OpenCV jika model Anda dilatih dengan itu)
|
| 32 |
+
# Gradio mengembalikan RGB, Real-ESRGAN/OpenCV biasanya butuh BGR
|
| 33 |
+
img_bgr = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
| 34 |
+
|
| 35 |
+
output_bgr, _ = upsampler.enhance(img_bgr, outscale=4)
|
| 36 |
+
|
| 37 |
+
# Konversi kembali ke RGB untuk ditampilkan oleh Gradio
|
| 38 |
+
output_rgb = cv2.cvtColor(output_bgr, cv2.COLOR_BGR2RGB)
|
| 39 |
+
|
| 40 |
+
return output_rgb
|
| 41 |
+
|
| 42 |
+
# Definisikan antarmuka Gradio
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=upscale_image_gradio,
|
| 45 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 46 |
+
outputs=gr.Image(type="numpy", label="Upscaled Image"),
|
| 47 |
+
title="Real-ESRGAN Image Upscaler",
|
| 48 |
+
description="Upload an image to upscale it using Real-ESRGAN x4plus model."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
iface.launch() # Untuk menjalankan lokal
|
models/RealESRGAN_x4plus.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4fa0d38905f75ac06eb49a7951b426670021be3018265fd191d2125df9d682f1
|
| 3 |
+
size 67040989
|
real_esrgan_utils.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 3 |
+
from realesrgan import RealESRGANer
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
def upscale_image(input_path, output_path):
|
| 7 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
|
| 8 |
+
num_block=23, num_grow_ch=32, scale=4)
|
| 9 |
+
upsampler = RealESRGANer(
|
| 10 |
+
scale=4,
|
| 11 |
+
model_path='models/RealESRGAN_x4plus.pth',
|
| 12 |
+
model=model,
|
| 13 |
+
tile=0,
|
| 14 |
+
tile_pad=10,
|
| 15 |
+
pre_pad=0,
|
| 16 |
+
half=False
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
img = cv2.imread(input_path, cv2.IMREAD_COLOR)
|
| 20 |
+
output, _ = upsampler.enhance(img, outscale=4)
|
| 21 |
+
cv2.imwrite(output_path, output)
|
| 22 |
+
return output_path
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
realesrgan==0.3.0
|
| 3 |
+
torch==2.1.0
|
| 4 |
+
torchvision==0.16.0
|
| 5 |
+
opencv-python
|
| 6 |
+
numpy<2
|