Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,634 Bytes
d2d949b 2e2a632 ff46f61 d2d949b 2e2a632 fd6068a d2d949b fd6068a ff46f61 0421974 2e2a632 ff46f61 2e2a632 ff46f61 2e2a632 ff46f61 2e2a632 fd6068a d2d949b fd6068a 4fc32cf d2d949b ff46f61 fd6068a d2d949b 0421974 ff46f61 d2d949b 4fc32cf d2d949b 2e2a632 d2d949b ff46f61 2e2a632 ff46f61 d2d949b ff46f61 d2d949b fd6068a cecbfad d2d949b cecbfad d2d949b 2e2a632 d2d949b cecbfad d2d949b fd6068a d2d949b fd6068a d2d949b fd6068a cecbfad 2e2a632 d2d949b ff46f61 d2d949b ff46f61 2e2a632 d2d949b ff46f61 cecbfad d2d949b ff46f61 cecbfad d2d949b cecbfad ff46f61 d2d949b ff46f61 fd6068a d2d949b 2e2a632 fd6068a d2d949b fd6068a d2d949b fd6068a d2d949b fd6068a d2d949b fd6068a ff46f61 fd6068a d2d949b fd6068a ff46f61 d2d949b fd6068a 2e2a632 fd6068a |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import spaces
import gradio as gr
import cv2
import numpy as np
from PIL import Image
import os
import shutil
import tempfile
import datetime
import ffmpeg
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
# استيراد مكتبة السلايدر الجديدة
from gradio_imageslider import ImageSlider
# ==========================================
# 1. إعداد نموذج DDColor
# ==========================================
print("⏳ Loading DDColor Professional Model...")
try:
ddcolor_pipeline = pipeline(
Tasks.image_colorization,
model='damo/cv_ddcolor_image-colorization',
device='gpu'
)
print("✅ DDColor Model loaded successfully.")
except Exception as e:
print(f"❌ Error loading DDColor model: {e}")
ddcolor_pipeline = None
# ==========================================
# 2. دالة المعالجة
# ==========================================
@spaces.GPU(duration=180)
def colorize_video_professional(video_file):
if not video_file:
return None, None
if ddcolor_pipeline is None:
raise gr.Error("فشل تحميل النموذج.")
print("🚀 Starting processing...")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
temp_frames_dir = os.path.join(tempfile.gettempdir(), f"frames_{timestamp}")
os.makedirs(temp_frames_dir, exist_ok=True)
final_output_name = f"colored_ddcolor_{timestamp}.mp4"
audio_path = os.path.join(tempfile.gettempdir(), f"audio_{timestamp}.aac")
# مسارات صور المقارنة
comp_original_path = os.path.join(tempfile.gettempdir(), f"comp_orig_{timestamp}.png")
comp_colored_path = os.path.join(tempfile.gettempdir(), f"comp_color_{timestamp}.png")
comparison_result = None
# --- استخراج الصوت ---
audio_exists = False
try:
ffmpeg.input(video_file).output(audio_path, acodec='copy').run(overwrite_output=True, quiet=True)
audio_exists = True
except ffmpeg.Error:
pass
# --- المعالجة ---
cap = cv2.VideoCapture(video_file)
fps = cap.get(cv2.CAP_PROP_FPS) or 25
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# نختار إطاراً في المنتصف ليكون صورة المقارنة
comparison_frame_index = max(0, total_frames // 2)
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
# حفظ الإطار الأصلي للمقارنة إذا وصلنا للمنتصف
if frame_count == comparison_frame_index:
cv2.imwrite(comp_original_path, frame)
# التلوين
result = ddcolor_pipeline(frame)
colorized_frame_bgr = result['output_img']
# حفظ الإطار الملون للمقارنة
if frame_count == comparison_frame_index:
cv2.imwrite(comp_colored_path, colorized_frame_bgr)
comparison_result = (comp_original_path, comp_colored_path)
# حفظ الإطار للفيديو
frame_filename = os.path.join(temp_frames_dir, f"frame_{frame_count:05d}.png")
cv2.imwrite(frame_filename, colorized_frame_bgr)
frame_count += 1
cap.release()
# --- تجميع الفيديو ---
input_frames = ffmpeg.input(os.path.join(temp_frames_dir, 'frame_%05d.png'), framerate=fps)
if audio_exists:
stream = ffmpeg.output(input_frames, ffmpeg.input(audio_path), final_output_name, vcodec='libx264', pix_fmt='yuv420p', acodec='aac', shortest=None)
else:
stream = ffmpeg.output(input_frames, final_output_name, vcodec='libx264', pix_fmt='yuv420p')
try:
stream.run(overwrite_output=True, quiet=True)
except ffmpeg.Error:
# محاولة بديلة
ffmpeg.input(os.path.join(temp_frames_dir, 'frame_%05d.png'), framerate=fps).output(final_output_name, vcodec='libx264', pix_fmt='yuv420p').run(overwrite_output=True)
shutil.rmtree(temp_frames_dir, ignore_errors=True)
# نرجع الفيديو + صور المقارنة (Tuple)
return final_output_name, comparison_result
# ==========================================
# 3. الواجهة الجديدة
# ==========================================
custom_css = """
#col-container {max-width: 800px; margin-left: auto; margin-right: auto;}
"""
with gr.Blocks(css=custom_css, title="Pro Video Colorizer") as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("# 🎞️ Professional Video Colorizer (DDColor)")
gr.Markdown("قم برفع فيديو أبيض وأسود وسيقوم الذكاء الاصطناعي بتلوينه.")
with gr.Row():
video_input = gr.Video(label="فيديو أبيض وأسود (Input)")
submit_btn = gr.Button("✨ تلوين ومعاينة (Colorize)", variant="primary", size="lg")
# عنصر المقارنة الجديد
gr.Markdown("### 🔍 معاينة قبل وبعد (Before / After)")
slider_output = ImageSlider(label="مقارنة النتيجة", type="filepath", position=0.5)
gr.Markdown("### 🎥 الفيديو النهائي الملون")
video_output = gr.Video(label="النتيجة النهائية")
submit_btn.click(
fn=colorize_video_professional,
inputs=[video_input],
outputs=[video_output, slider_output]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |