Spaces:
Runtime error
Runtime error
# @title #设置提示词,获取坐标 | |
import subprocess | |
import pickle | |
import cv2 | |
import os | |
import re | |
import pickle | |
from moviepy.editor import VideoFileClip, ImageSequenceClip | |
import subprocess | |
out_dir = "/workspace/tem" | |
os.makedirs(out_dir, exist_ok=True) | |
def set_prompt_and_get_coordinates(output_video, texts=['men', 'the table']): | |
if isinstance(texts, str): | |
texts = texts.split(',') # Assuming prompts are separated by commas | |
texts = [text.strip() for text in texts] | |
print(texts) | |
# 保存提示词到文件 | |
with open('/workspace/tem/texts.pkl', 'wb') as file: | |
pickle.dump(texts, file) | |
with open('/workspace/tem/output_video.pkl', 'wb') as file: | |
pickle.dump(output_video, file) | |
# 构建命令 | |
command = ['python', '/workspace/florence-sam-tencent/1.py'] | |
# 执行命令并捕获输出 | |
all_ok_bboxes = subprocess.run(command, capture_output=True, text=True) | |
#print(all_ok_bboxes) | |
return all_ok_bboxes | |
# 示例调用 | |
output_video = '/workspace/transvnet/o_videos/2.mp4' | |
texts="men, the table" | |
result = set_prompt_and_get_coordinates(output_video,texts) | |
print(result.stdout) | |
#result | |
# @title #sam2处理 | |
def run_sam2(output_video): | |
# 定义脚本路径 | |
script_path = '/workspace/florence-sam-tencent/2.py' | |
# 构建命令 | |
command = ['python3', script_path] | |
# 执行命令并捕获输出 | |
sam2_output = subprocess.run(command, capture_output=True, text=True) | |
#print(sam2_output) | |
return sam2_output | |
# 示例调用 | |
#output_video = 'path/to/output_video.mp4' | |
result = run_sam2(output_video) | |
#print(result.stdout) | |
#result | |
def create_video_with_audio(image_folder, input_video_path): | |
# 获取图像文件列表 | |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))] | |
# 自然排序图像文件 | |
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')): | |
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, s)] | |
image_files.sort(key=natural_sort_key) | |
''' | |
# 跳过第一张图片 | |
if image_files: | |
image_files = image_files[1:] | |
''' | |
# 读取第一张图像以获取尺寸 | |
if image_files: | |
first_image = cv2.imread(os.path.join(image_folder, image_files[0])) | |
height, width, layers = first_image.shape | |
else: | |
raise ValueError("No valid images found in the folder after skipping the first one.") | |
# 获取输入视频的帧率 | |
cap = cv2.VideoCapture(input_video_path) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
cap.release() | |
# 创建图像序列视频 | |
image_paths = [os.path.join(image_folder, img) for img in image_files] | |
clip = ImageSequenceClip(image_paths, fps=fps) | |
# 从输入视频中提取音频 | |
audio_clip = VideoFileClip(input_video_path).audio | |
# 将音频添加到视频中 | |
final_clip = clip.set_audio(audio_clip) | |
# 生成与输入视频同名的输出文件 | |
#output_video_path = os.path.join('/tmp/gradio/', os.path.basename(input_video_path)) | |
output_video_path = os.path.join('/workspace/tem/sam2_videos/', os.path.basename(input_video_path)) | |
# 确保输出目录存在 | |
os.makedirs(os.path.dirname(output_video_path), exist_ok=True) | |
# 导出最终视频 | |
final_clip.write_videofile(output_video_path, codec='libx264') | |
print(f"Video created successfully: {output_video_path}") | |
return output_video_path | |
# 示例调用 | |
image_folder = '/workspace/tem/output' | |
with open('/workspace/tem/output_video.pkl', 'rb') as file: | |
output_video = pickle.load(file) | |
print(output_video) | |
input_video_path = output_video | |
create_video_with_audio(image_folder, input_video_path) | |
'''''' |