File size: 3,308 Bytes
d346aa9 f7f7335 88eaf9c f7f7335 3af33be f7f7335 d346aa9 f7f7335 d346aa9 f7f7335 d346aa9 f7f7335 d346aa9 f7f7335 d346aa9 f7f7335 d346aa9 f7f7335 d346aa9 3af33be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import gradio as gr
import numpy as np
import cv2
import os
import requests
import sys
from PIL import Image
# ─── বাগ ফিক্স ────────────────────────────────────────────────────────────────
import torchvision.transforms.functional
sys.modules['torchvision.transforms.functional_tensor'] = torchvision.transforms.functional
# ─── মডেল সেটআপ ──────────────────────────────────────────────────────────────
MODEL_PATH = "RealESRGAN_x4plus.pth"
if not os.path.exists(MODEL_PATH):
print("মডেল ডাউনলোড হচ্ছে...")
r = requests.get("https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth", stream=True)
with open(MODEL_PATH, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
upsampler = RealESRGANer(scale=4, model_path=MODEL_PATH, model=model, tile=128, tile_pad=10, pre_pad=0, half=False)
# ─── মূল কাজ (কোনো ফ্যান্সি ডিজাইন ছাড়া) ──────────────────────────────────────
def process_image(img):
if img is None:
return None, None
# RGB থেকে BGR
img_np = np.array(img.convert("RGB"))
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
# 4K আপস্কেল
output_bgr, _ = upsampler.enhance(img_bgr, outscale=4)
# BGR থেকে RGB
output_rgb = cv2.cvtColor(output_bgr, cv2.COLOR_BGR2RGB)
out_img = Image.fromarray(output_rgb)
# 4K সাইজ নিশ্চিত করা
if out_img.size != (3840, 2160):
out_img = out_img.resize((3840, 2160), Image.LANCZOS)
# সেভ করা
out_path = "/tmp/output.png"
out_img.save(out_path, "PNG")
return out_img, out_path
# ─── সিম্পল ইউজার ইন্টারফেস ──────────────────────────────────────────────────
with gr.Blocks() as demo:
gr.Markdown("# 🚀 4K AI Upscaler (Basic Version)")
gr.Markdown("সব ডিজাইন বাদ দেওয়া হয়েছে। শুধু ছবি আপলোড করুন এবং রেজাল্ট নিন।")
with gr.Row():
inp = gr.Image(type="pil", label="ছবি আপলোড করুন")
out = gr.Image(type="pil", label="4K আউটপুট")
btn = gr.Button("✨ 4K তে রূপান্তর করুন", variant="primary")
file_out = gr.File(label="4K ছবি ডাউনলোড করুন")
btn.click(fn=process_image, inputs=inp, outputs=[out, file_out])
if __name__ == "__main__":
demo.queue().launch()
|