Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
import sys
|
| 3 |
-
import torchvision.transforms.functional as F
|
| 4 |
-
sys.modules['torchvision.transforms.functional_tensor'] = F
|
| 5 |
-
# --------------------------------
|
| 6 |
-
|
| 7 |
from gfpgan import GFPGANer
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
-
|
| 10 |
-
import
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
repo_id="leonelhs/gfpgan",
|
| 15 |
-
filename="GFPGANv1.4.pth"
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def restore_cartoon(image):
|
| 27 |
-
image = image.convert("RGB")
|
| 28 |
-
restored_image, _ = restorer.enhance(image, has_aligned=False)
|
| 29 |
-
return restored_image
|
| 30 |
|
| 31 |
iface = gr.Interface(
|
| 32 |
-
fn=
|
| 33 |
-
inputs=
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from gfpgan import GFPGANer
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# کش مدلها
|
| 8 |
+
loaded_models = {}
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def load_model(version):
|
| 11 |
+
if version == "v1.4 (original)":
|
| 12 |
+
if version not in loaded_models:
|
| 13 |
+
model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.4.pth")
|
| 14 |
+
loaded_models[version] = GFPGANer(
|
| 15 |
+
model_path=model_path,
|
| 16 |
+
upscale=1,
|
| 17 |
+
arch='original',
|
| 18 |
+
channel_multiplier=2,
|
| 19 |
+
bg_upsampler=None
|
| 20 |
+
)
|
| 21 |
+
return loaded_models[version]
|
| 22 |
+
|
| 23 |
+
elif version == "v1.3 (clean)":
|
| 24 |
+
if version not in loaded_models:
|
| 25 |
+
model_path = hf_hub_download("leonelhs/gfpgan", "GFPGANv1.3.pth")
|
| 26 |
+
loaded_models[version] = GFPGANer(
|
| 27 |
+
model_path=model_path,
|
| 28 |
+
upscale=1,
|
| 29 |
+
arch='clean',
|
| 30 |
+
channel_multiplier=2,
|
| 31 |
+
bg_upsampler=None
|
| 32 |
+
)
|
| 33 |
+
return loaded_models[version]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def enhance_face(image, version):
|
| 37 |
+
restorer = load_model(version)
|
| 38 |
+
|
| 39 |
+
# تبدیل تصویر PIL/numpy به BGR
|
| 40 |
+
if isinstance(image, np.ndarray):
|
| 41 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 42 |
+
else:
|
| 43 |
+
raise ValueError("Invalid image format")
|
| 44 |
+
|
| 45 |
+
_, _, restored_img = restorer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 46 |
+
|
| 47 |
+
# تبدیل دوباره به RGB برای نمایش
|
| 48 |
+
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
| 49 |
+
|
| 50 |
+
return restored_img
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
iface = gr.Interface(
|
| 54 |
+
fn=enhance_face,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.Image(type="numpy", label="Upload your Anime/Cartoon image"),
|
| 57 |
+
gr.Radio(["v1.3 (clean)", "v1.4 (original)"], value="v1.4 (original)", label="Choose GFPGAN Version")
|
| 58 |
+
],
|
| 59 |
+
outputs=gr.Image(type="numpy", label="Restored Cartoon Face"),
|
| 60 |
+
title="Cartoon Face Restoration with GFPGAN",
|
| 61 |
+
description="Upload a cartoonized image (e.g. from AnimeGAN) and restore it with GFPGAN."
|
| 62 |
)
|
| 63 |
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|