Spaces:
Running
Running
Upload run_api.py
Browse files- run_api.py +41 -0
run_api.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
from shutil import copy2
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
|
| 6 |
+
# 定义输入视频目录和输出目录
|
| 7 |
+
input_dir = "../Bakemonogatari_Videos_Splited"
|
| 8 |
+
output_dir = "../Bakemonogatari_Videos_Splited_Depth"
|
| 9 |
+
|
| 10 |
+
# 如果输出目录不存在,则创建它
|
| 11 |
+
if not os.path.exists(output_dir):
|
| 12 |
+
os.makedirs(output_dir)
|
| 13 |
+
|
| 14 |
+
# 遍历输入目录中的所有 .mp4 文件
|
| 15 |
+
for filename in tqdm(os.listdir(input_dir)):
|
| 16 |
+
print(filename)
|
| 17 |
+
#break
|
| 18 |
+
if filename.endswith(".mp4"):
|
| 19 |
+
video_path = os.path.join(input_dir, filename)
|
| 20 |
+
|
| 21 |
+
# 初始化 Gradio 客户端
|
| 22 |
+
client = Client("http://127.0.0.1:7860")
|
| 23 |
+
|
| 24 |
+
# 调用 API
|
| 25 |
+
result = client.predict(
|
| 26 |
+
input_video={"video": handle_file(video_path)},
|
| 27 |
+
max_len=500,
|
| 28 |
+
target_fps=15,
|
| 29 |
+
max_res=1280,
|
| 30 |
+
grayscale=False,
|
| 31 |
+
api_name="/infer_video_depth"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# 获取处理后的视频路径
|
| 35 |
+
processed_video_path = result[1]["video"]
|
| 36 |
+
|
| 37 |
+
# 将处理后的视频拷贝到输出目录
|
| 38 |
+
output_video_path = os.path.join(output_dir, os.path.basename(processed_video_path))
|
| 39 |
+
copy2(processed_video_path, output_video_path)
|
| 40 |
+
|
| 41 |
+
print(f"Processed and copied {filename} to {output_video_path}")
|