Spaces:
Running
Running
File size: 5,112 Bytes
4c1e242 44925e5 4c1e242 44925e5 4c1e242 44925e5 |
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 |
import gradio as gr
import moviepy.editor as mpy
import numpy as np
import os
from omegaconf import OmegaConf
from tqdm import tqdm
import shutil
import time
from avatar_generator import Avatar
# # 指定保存文件的目录
# SAVE_DIR = "./uploaded_files"
# os.makedirs(SAVE_DIR, exist_ok=True) # 创建目录(如果不存在)
# 全局变量,用于控制任务是否应当终止
should_stop = False
# 定义逐帧处理的函数
def process_files(file1, file2):
global should_stop
should_stop = False # 重置停止标志
yield None, None, None, "Starting Process!"
file_path1 = file1.name
file_path2 = file2.name
pose_data = np.load(file_path1)
exp_data = np.load(file_path2)
# save
pose_path = './test_data/AMASS/online_test_pose_data.npz'
exp_path = './test_data/face_exp/online_test_exp_data.npy'
np.savez(pose_path, **pose_data)
np.save(exp_path, exp_data)
# with open(file1.name, 'rb') as fsrc:
# with open(file_path1, 'wb') as fdst:
# shutil.copyfileobj(fsrc, fdst)
# with open(file2.name, 'rb') as fsrc:
# with open(file_path2, 'wb') as fdst:
# shutil.copyfileobj(fsrc, fdst)
conf = OmegaConf.load('configs/example.yaml')
avatar = Avatar(conf)
avatar.build_dataset(pose_path, exp_path)
lenth = min(len(avatar.body_dataset), len(avatar.head_dataloader),20)
output_frames = []
start_time = time.time()
for idx in tqdm(range(lenth)):
if should_stop:
yield None, None, None, None
break # 任务应当终止时跳出循环
frame = avatar.reder_frame(idx)
# rgb2bgr
frame = frame[..., ::-1]
output_frames.append(frame)
elapsed_time = time.time() - start_time
estimated_total_time = (elapsed_time / (idx + 1)) * lenth
remaining_time = estimated_total_time - elapsed_time
yield frame, None, (idx + 1) / lenth * 100, f"{elapsed_time:.2f} sec/{estimated_total_time:.2f} sec"
if not should_stop:
output_path = "./output/output_video.mp4"
final_video = mpy.ImageSequenceClip(output_frames, fps=25)
final_video.write_videofile(output_path, codec='libx264')
yield output_frames[-1], output_path, 100.0, "Processing completed!"
# 清除操作
def clear_files():
global should_stop
should_stop = True # 设置停止标志
# 返回空值以清空界面元素
return None, None, None, None, None, None
# 创建 Gradio 接口
with gr.Blocks(css="""
.equal-height {
height: 425px; /* 设置为你希望的高度 */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.equal-height input {
height: 100%; /* 输入框占满整个容器高度 */
}
.output-container {
height: 400px; /* 输出框的高度 */
}
.custom-text {
height: 80px; /* 输出框的高度 */
}
""") as demo:
with gr.Row():
# 左侧列,用于放置文件输入
with gr.Column(scale=1):
with gr.Row(elem_classes="equal-height"):
file_input1 = gr.File(label="Upload File (Body Pose)")
file_input2 = gr.File(label="Upload File (Face EXP)")
with gr.Column(scale=2):
with gr.Row():
# 中间列,用于放置帧输出
with gr.Column(scale=1):
frame_output = gr.Image(label="Current Frame Output", elem_classes="output-container") # 输出当前帧图像
# 右侧列,用于放置视频输出
with gr.Column(scale=1):
video_output = gr.Video(label="Processed Video Output", elem_classes="output-container") # 输出视频
# progress_bar = gr.Label(label="Progress")
with gr.Row():
with gr.Column(scale=2):
progress_bar = gr.Slider(visible=True, minimum=0, maximum=100, step=1, label="Progress %",elem_classes="custom-text") # 使用Slider模拟进度条
with gr.Column(scale=1):
output_time = gr.Textbox(label='Processing Time/Estimate Time', elem_classes="custom-text")
# time_label = gr.Label(value="", label="Estimated Time Remaining", elem_classes="custom-label")
# with gr.Row():
# progress_bar = gr.Progress() # 添加进度条
with gr.Row():
process_button = gr.Button("Start Processing Files")
clear_button = gr.Button("Clear or Stop Processing")
# 定义按钮的功能
process_button.click(
fn=process_files,
inputs=[file_input1, file_input2],
outputs=[frame_output, video_output, progress_bar, output_time],
show_progress=False
)
clear_button.click(
fn= clear_files,
inputs=[],
outputs=[file_input1, file_input2, frame_output, video_output, progress_bar, output_time]
)
# 启动应用
demo.launch() |