|
|
import gradio as gr |
|
|
import time |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_character(source_image, target_video, progress=gr.Progress(track_tqdm=True)): |
|
|
""" |
|
|
Simulates the character replacement process in a video. |
|
|
|
|
|
Args: |
|
|
source_image (str): File path to the source character image. |
|
|
target_video (str): File path to the target video. |
|
|
progress (gr.Progress): Gradio progress tracker to show processing status. |
|
|
|
|
|
Returns: |
|
|
str: File path of the processed video. For this demo, it's the original video. |
|
|
""" |
|
|
if source_image is None: |
|
|
raise gr.Error("Please upload a character reference image.") |
|
|
if target_video is None: |
|
|
raise gr.Error("Please upload a target video.") |
|
|
|
|
|
|
|
|
stages = [ |
|
|
"Initializing model...", |
|
|
"Detecting faces and bodies in video...", |
|
|
"Tracking primary character...", |
|
|
"Generating character mask...", |
|
|
"Swapping frames with reference image...", |
|
|
"Rendering final video..." |
|
|
] |
|
|
|
|
|
for stage in progress.tqdm(stages, desc="Starting process"): |
|
|
|
|
|
time.sleep(2.5) |
|
|
|
|
|
|
|
|
return target_video |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
|
|
|
gr.HTML( |
|
|
""" |
|
|
<div style="text-align: center; max-width: 800px; margin: 0 auto;"> |
|
|
<h1 style="font-weight: 900; font-size: 2.5rem;">🎭 AI Video Character Replacement</h1> |
|
|
<p style="margin-top: 1rem; font-size: 1.1rem; color: #4B5563;"> |
|
|
Provide a reference image of a character and a target video. The AI will replace a character in the video with the one from your image. |
|
|
<br> |
|
|
<em>(Note: This is a UI demonstration. The backend simulates the process and returns the original video.)</em> |
|
|
</p> |
|
|
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #3B82F6; font-weight: 500;"> |
|
|
Built with anycoder |
|
|
</a> |
|
|
</div> |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(variant="panel", equal_height=True): |
|
|
|
|
|
with gr.Column(scale=1, min_width=300): |
|
|
gr.Markdown("### 1. Inputs") |
|
|
source_image = gr.Image( |
|
|
type="filepath", |
|
|
label="Character Reference Image", |
|
|
info="Upload a clear image of the character you want to insert." |
|
|
) |
|
|
target_video = gr.Video( |
|
|
label="Target Video", |
|
|
info="Upload the video where you want to replace a character." |
|
|
) |
|
|
submit_btn = gr.Button("✨ Replace Character", variant="primary") |
|
|
|
|
|
|
|
|
with gr.Column(scale=1, min_width=300): |
|
|
gr.Markdown("### 2. Result") |
|
|
output_video = gr.Video( |
|
|
label="Result Video", |
|
|
interactive=False, |
|
|
info="The processed video will appear here." |
|
|
) |
|
|
|
|
|
|
|
|
submit_btn.click( |
|
|
fn=replace_character, |
|
|
inputs=[source_image, target_video], |
|
|
outputs=output_video |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
``` |