Spaces:
Paused
Paused
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,132 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import numpy as np
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from insightface.app import FaceAnalysis
|
| 7 |
-
from insightface.model_zoo import get_model
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
import tempfile
|
| 10 |
-
|
| 11 |
-
# --- 1. إعداد المسارات وتحميل الموديلات (الطريقة الرسمية) ---
|
| 12 |
-
print("--- جاري تجهيز الموديلات ---")
|
| 13 |
-
|
| 14 |
-
try:
|
| 15 |
-
# تحميل موديل التبديل باستخدام huggingface_hub لضمان الاستقرار
|
| 16 |
-
# سيتم تحميله مرة واحدة فقط وتخزينه في الكاش
|
| 17 |
-
model_path = hf_hub_download(
|
| 18 |
-
repo_id="ezioruan/inswapper_128.onnx",
|
| 19 |
-
filename="inswapper_128.onnx",
|
| 20 |
-
local_dir=os.path.expanduser("~/.insightface/models"),
|
| 21 |
-
local_dir_use_symlinks=False
|
| 22 |
-
)
|
| 23 |
-
print(f"✅ الموديل جاهز في المسار: {model_path}")
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"❌ فشل تحميل الموديل: {str(e)}")
|
| 26 |
-
model_path = None
|
| 27 |
-
|
| 28 |
-
# إعداد محرك تحليل الوجوه (Buffalo_L)
|
| 29 |
-
# ملاحظة: سيقوم تلقائياً بتحميل ملفاته الصغيرة عند أول تشغيل
|
| 30 |
-
print("--- جاري تهيئة محرك الوجوه (CPU Mode) ---")
|
| 31 |
-
face_app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
|
| 32 |
-
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 33 |
-
|
| 34 |
-
# تحميل المبدل (Swapper)
|
| 35 |
-
if model_path:
|
| 36 |
-
swapper = get_model(model_path, download=False)
|
| 37 |
-
else:
|
| 38 |
-
swapper = None
|
| 39 |
-
|
| 40 |
-
# --- 2. منطق المعالجة ---
|
| 41 |
-
|
| 42 |
-
def process_video(source_img, target_video, progress=gr.Progress()):
|
| 43 |
-
if swapper is None:
|
| 44 |
-
return None, "❌ الموديل غير جاهز، تأكد من اتصال السيرفر."
|
| 45 |
-
|
| 46 |
-
if source_img is None or target_video is None:
|
| 47 |
-
return None, "⚠️ يرجى رفع الصورة والفيديو أولاً."
|
| 48 |
-
|
| 49 |
-
# استخراج وجه المصدر
|
| 50 |
-
face_img = cv2.cvtColor(np.array(source_image_input), cv2.COLOR_RGB2BGR) # تعديل بسيط للمدخل
|
| 51 |
-
face_img = cv2.cvtColor(np.array(source_img), cv2.COLOR_RGB2BGR)
|
| 52 |
-
source_faces = face_app.get(face_img)
|
| 53 |
-
|
| 54 |
-
if not source_faces:
|
| 55 |
-
return None, "❌ لم يتم العثور على وجه في الصورة المرفوعة."
|
| 56 |
-
|
| 57 |
-
source_face = source_faces[0]
|
| 58 |
-
|
| 59 |
-
# إعداد الفيديو
|
| 60 |
-
cap = cv2.VideoCapture(target_video)
|
| 61 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 62 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 63 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 64 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 65 |
-
|
| 66 |
-
output_path = tempfile.mktemp(suffix='.mp4')
|
| 67 |
-
# استخدام 'avc1' لضمان عمل الفيديو على المتصفحات مباشرة
|
| 68 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 69 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 70 |
-
|
| 71 |
-
frame_count = 0
|
| 72 |
-
try:
|
| 73 |
-
while cap.isOpened():
|
| 74 |
-
ret, frame = cap.read()
|
| 75 |
-
if not ret:
|
| 76 |
-
break
|
| 77 |
-
|
| 78 |
-
# تحليل الوجوه في الفريم الحالي
|
| 79 |
-
target_faces = face_app.get(frame)
|
| 80 |
-
if target_faces:
|
| 81 |
-
for target_face in target_faces:
|
| 82 |
-
frame = swapper.get(frame, target_face, source_face, paste_back=True)
|
| 83 |
-
|
| 84 |
-
out.write(frame)
|
| 85 |
-
frame_count += 1
|
| 86 |
-
|
| 87 |
-
if frame_count % 10 == 0:
|
| 88 |
-
progress(frame_count / total_frames, desc=f"🎬 معالجة: {int((frame_count/total_frames)*100)}%")
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
return None, f"🔥 خطأ: {str(e)}"
|
| 92 |
-
finally:
|
| 93 |
-
cap.release()
|
| 94 |
-
out.release()
|
| 95 |
-
|
| 96 |
-
return output_path, "✅ تم التبديل بنجاح!"
|
| 97 |
-
|
| 98 |
-
# --- 3. واجهة المستخدم (التصميم الجمالي) ---
|
| 99 |
-
css = """
|
| 100 |
-
.container { max-width: 900px; margin: auto; }
|
| 101 |
-
.upload-box { border: 2px dashed #4f46e5; border-radius: 15px; padding: 15px; background: #fdfdfd; }
|
| 102 |
-
#mix-btn { background: linear-gradient(90deg, #4f46e5, #7c3aed); color: white; border-radius: 8px; font-size: 1.1em; }
|
| 103 |
-
"""
|
| 104 |
-
|
| 105 |
-
with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 106 |
-
gr.HTML("""
|
| 107 |
-
<div style="text-align: center; padding: 10px;">
|
| 108 |
-
<h1 style="color: #4f46e5; margin-bottom: 5px;">🎭 AI Face Mix Pro</h1>
|
| 109 |
-
<p style="color: #666;">نسخة 2026 المستقرة - تبديل وجوه سينمائي</p>
|
| 110 |
-
</div>
|
| 111 |
-
""")
|
| 112 |
-
|
| 113 |
-
with gr.Row(elem_classes="container"):
|
| 114 |
-
with gr.Column():
|
| 115 |
-
with gr.Group(elem_classes="upload-box"):
|
| 116 |
-
source_image = gr.Image(label="📸 صورتك", type="pil")
|
| 117 |
-
target_vid = gr.Video(label="🎬 فيديو المشهد")
|
| 118 |
-
|
| 119 |
-
mix_btn = gr.Button("🚀 ابدأ الخلط السحري", elem_id="mix-btn")
|
| 120 |
-
|
| 121 |
-
with gr.Column():
|
| 122 |
-
output_vid = gr.Video(label="✨ النتيجة")
|
| 123 |
-
status = gr.Textbox(label="📡 الحالة", interactive=False)
|
| 124 |
-
|
| 125 |
-
mix_btn.click(
|
| 126 |
-
fn=process_video,
|
| 127 |
-
inputs=[source_image, target_vid],
|
| 128 |
-
outputs=[output_vid, status]
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
if __name__ == "__main__":
|
| 132 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|