Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image, ImageFilter
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if editor_data is None:
|
| 10 |
return None
|
| 11 |
|
|
@@ -16,48 +23,39 @@ def inpaint(editor_data, brush_size, feather):
|
|
| 16 |
return None
|
| 17 |
|
| 18 |
image = bg.convert("RGB")
|
|
|
|
| 19 |
|
| 20 |
if not layers or layers[0] is None:
|
| 21 |
return image
|
| 22 |
|
| 23 |
-
# استخراج الماسك
|
| 24 |
layer = layers[0].convert("RGBA")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# تكبير الماسك شوية حتى يغطي حواف النص
|
| 28 |
-
mask_img = Image.fromarray(alpha)
|
| 29 |
|
| 30 |
-
# feather
|
| 31 |
if feather > 0:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# 🧹 SFX Cleaner — LaMa Inpainting
|
| 50 |
-
**كيفية الاستخدام:**
|
| 51 |
-
1. ارفع صورة المانهوا
|
| 52 |
-
2. ارسم بالفرشاة البيضاء على النص / SFX
|
| 53 |
-
3. اضغط **تبييض**
|
| 54 |
-
"""
|
| 55 |
-
)
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column(scale=2):
|
| 59 |
editor = gr.ImageEditor(
|
| 60 |
-
label="📌 ارسم على النص
|
| 61 |
brush=gr.Brush(
|
| 62 |
colors=["#ffffff"],
|
| 63 |
color_mode="fixed",
|
|
@@ -65,42 +63,19 @@ with gr.Blocks(
|
|
| 65 |
),
|
| 66 |
eraser=gr.Eraser(default_size=20),
|
| 67 |
type="pil",
|
| 68 |
-
height=
|
| 69 |
)
|
| 70 |
-
|
| 71 |
with gr.Column(scale=1):
|
| 72 |
output = gr.Image(
|
| 73 |
label="✅ النتيجة",
|
| 74 |
type="pil",
|
| 75 |
-
height=
|
| 76 |
)
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
)
|
| 84 |
-
feather = gr.Slider(
|
| 85 |
-
minimum=0, maximum=10, value=2, step=1,
|
| 86 |
-
label="نعومة الحواف (Feather)"
|
| 87 |
-
)
|
| 88 |
-
with gr.Column():
|
| 89 |
-
btn = gr.Button("🧹 تبييض", variant="primary", size="lg", elem_id="run-btn")
|
| 90 |
-
download = gr.DownloadButton(label="💾 حفظ الصورة", visible=False)
|
| 91 |
-
|
| 92 |
-
btn.click(
|
| 93 |
-
fn=inpaint,
|
| 94 |
-
inputs=[editor, brush_size, feather],
|
| 95 |
-
outputs=[output]
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
# تفعيل زر الحفظ بعد النتيجة
|
| 99 |
-
def enable_download(img):
|
| 100 |
-
if img is None:
|
| 101 |
-
return gr.update(visible=False)
|
| 102 |
-
return gr.update(visible=True)
|
| 103 |
-
|
| 104 |
-
output.change(enable_download, inputs=output, outputs=download)
|
| 105 |
|
| 106 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image, ImageFilter
|
| 4 |
+
import torch
|
| 5 |
+
import cv2
|
| 6 |
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
+
# تحميل lama_large_512px — نفس الموديل اللي في BallonsTranslator
|
| 10 |
+
from iopaint.model.lama import LaMa
|
| 11 |
+
from iopaint.schema import InpaintRequest, HDStrategy
|
| 12 |
+
|
| 13 |
+
model = LaMa(device=device)
|
| 14 |
+
|
| 15 |
+
def inpaint(editor_data, feather):
|
| 16 |
if editor_data is None:
|
| 17 |
return None
|
| 18 |
|
|
|
|
| 23 |
return None
|
| 24 |
|
| 25 |
image = bg.convert("RGB")
|
| 26 |
+
image_np = np.array(image)
|
| 27 |
|
| 28 |
if not layers or layers[0] is None:
|
| 29 |
return image
|
| 30 |
|
| 31 |
+
# استخراج الماسك
|
| 32 |
layer = layers[0].convert("RGBA")
|
| 33 |
+
mask_np = np.array(layer)[:, :, 3]
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# feather اختياري
|
| 36 |
if feather > 0:
|
| 37 |
+
mask_pil = Image.fromarray(mask_np)
|
| 38 |
+
mask_pil = mask_pil.filter(ImageFilter.GaussianBlur(radius=feather))
|
| 39 |
+
mask_np = np.array(mask_pil)
|
| 40 |
+
|
| 41 |
+
mask_np = (mask_np > 127).astype(np.uint8) * 255
|
| 42 |
+
|
| 43 |
+
config = InpaintRequest(hd_strategy=HDStrategy.ORIGINAL)
|
| 44 |
+
result = model(image_np, mask_np, config)
|
| 45 |
+
|
| 46 |
+
return Image.fromarray(result)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
with gr.Blocks(title="SFX Cleaner - LaMa Large") as demo:
|
| 50 |
+
gr.Markdown("""
|
| 51 |
+
# 🧹 SFX Cleaner — lama_large_512px
|
| 52 |
+
**الطريقة:** ارفع الصورة ← ارسم بالفرشاة على النص/SFX ← اضغط **تبييض**
|
| 53 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column(scale=2):
|
| 57 |
editor = gr.ImageEditor(
|
| 58 |
+
label="📌 ارسم على النص",
|
| 59 |
brush=gr.Brush(
|
| 60 |
colors=["#ffffff"],
|
| 61 |
color_mode="fixed",
|
|
|
|
| 63 |
),
|
| 64 |
eraser=gr.Eraser(default_size=20),
|
| 65 |
type="pil",
|
| 66 |
+
height=650,
|
| 67 |
)
|
|
|
|
| 68 |
with gr.Column(scale=1):
|
| 69 |
output = gr.Image(
|
| 70 |
label="✅ النتيجة",
|
| 71 |
type="pil",
|
| 72 |
+
height=650,
|
| 73 |
)
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
+
feather = gr.Slider(0, 8, value=2, step=1, label="نعومة الحواف (Feather)")
|
| 77 |
+
btn = gr.Button("🧹 تبييض", variant="primary", size="lg")
|
| 78 |
+
|
| 79 |
+
btn.click(fn=inpaint, inputs=[editor, feather], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
demo.launch()
|