Spaces:
Runtime error
Runtime error
Create all_in_one.py
Browse files- all_in_one.py +114 -0
all_in_one.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# @title #设置提示词,获取坐标
|
2 |
+
import subprocess
|
3 |
+
import pickle
|
4 |
+
import cv2
|
5 |
+
import os
|
6 |
+
import re
|
7 |
+
import pickle
|
8 |
+
from moviepy.editor import VideoFileClip, ImageSequenceClip
|
9 |
+
import subprocess
|
10 |
+
|
11 |
+
def set_prompt_and_get_coordinates(output_video, texts=['men', 'the table']):
|
12 |
+
if isinstance(texts, str):
|
13 |
+
texts = texts.split(',') # Assuming prompts are separated by commas
|
14 |
+
texts = [text.strip() for text in texts]
|
15 |
+
print(texts)
|
16 |
+
# 保存提示词到文件
|
17 |
+
with open('/workspace/florence-sam/texts.pkl', 'wb') as file:
|
18 |
+
pickle.dump(texts, file)
|
19 |
+
with open('/workspace/florence-sam/output_video.pkl', 'wb') as file:
|
20 |
+
pickle.dump(output_video, file)
|
21 |
+
# 构建命令
|
22 |
+
command = ['python', '/workspace/florence-sam/1.py']
|
23 |
+
|
24 |
+
# 执行命令并捕获输出
|
25 |
+
all_ok_bboxes = subprocess.run(command, capture_output=True, text=True)
|
26 |
+
#print(all_ok_bboxes)
|
27 |
+
return all_ok_bboxes
|
28 |
+
|
29 |
+
# 示例调用
|
30 |
+
output_video = '/workspace/transvnet/o_videos/2.mp4'
|
31 |
+
texts="men, the table"
|
32 |
+
result = set_prompt_and_get_coordinates(output_video,texts)
|
33 |
+
print(result.stdout)
|
34 |
+
#result
|
35 |
+
# @title #sam2处理
|
36 |
+
|
37 |
+
|
38 |
+
def run_sam2(output_video):
|
39 |
+
|
40 |
+
# 定义脚本路径
|
41 |
+
script_path = '/workspace/florence-sam/2.py'
|
42 |
+
|
43 |
+
# 构建命令
|
44 |
+
command = ['python3', script_path]
|
45 |
+
|
46 |
+
# 执行命令并捕获输出
|
47 |
+
sam2_output = subprocess.run(command, capture_output=True, text=True)
|
48 |
+
#print(sam2_output)
|
49 |
+
return sam2_output
|
50 |
+
|
51 |
+
# 示例调用
|
52 |
+
#output_video = 'path/to/output_video.mp4'
|
53 |
+
result = run_sam2(output_video)
|
54 |
+
#print(result.stdout)
|
55 |
+
#result
|
56 |
+
|
57 |
+
|
58 |
+
def create_video_with_audio(image_folder, input_video_path):
|
59 |
+
# 获取图像文件列表
|
60 |
+
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
61 |
+
|
62 |
+
# 自然排序图像文件
|
63 |
+
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
|
64 |
+
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, s)]
|
65 |
+
|
66 |
+
image_files.sort(key=natural_sort_key)
|
67 |
+
'''
|
68 |
+
# 跳过第一张图片
|
69 |
+
if image_files:
|
70 |
+
image_files = image_files[1:]
|
71 |
+
'''
|
72 |
+
# 读取第一张图像以获取尺寸
|
73 |
+
if image_files:
|
74 |
+
first_image = cv2.imread(os.path.join(image_folder, image_files[0]))
|
75 |
+
height, width, layers = first_image.shape
|
76 |
+
else:
|
77 |
+
raise ValueError("No valid images found in the folder after skipping the first one.")
|
78 |
+
|
79 |
+
# 获取输入视频的帧率
|
80 |
+
cap = cv2.VideoCapture(input_video_path)
|
81 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
82 |
+
cap.release()
|
83 |
+
|
84 |
+
# 创建图像序列视频
|
85 |
+
image_paths = [os.path.join(image_folder, img) for img in image_files]
|
86 |
+
clip = ImageSequenceClip(image_paths, fps=fps)
|
87 |
+
|
88 |
+
# 从输入视频中提取音频
|
89 |
+
audio_clip = VideoFileClip(input_video_path).audio
|
90 |
+
|
91 |
+
# 将音频添加到视频中
|
92 |
+
final_clip = clip.set_audio(audio_clip)
|
93 |
+
|
94 |
+
# 生成与输入视频同名的输出文件
|
95 |
+
#output_video_path = os.path.join('/tmp/gradio/', os.path.basename(input_video_path))
|
96 |
+
output_video_path = os.path.join('/workspace/florence-sam/sam2_videos/', os.path.basename(input_video_path))
|
97 |
+
# 确保输出目录存在
|
98 |
+
os.makedirs(os.path.dirname(output_video_path), exist_ok=True)
|
99 |
+
|
100 |
+
# 导出最终视频
|
101 |
+
final_clip.write_videofile(output_video_path, codec='libx264')
|
102 |
+
|
103 |
+
print(f"Video created successfully: {output_video_path}")
|
104 |
+
return output_video_path
|
105 |
+
|
106 |
+
# 示例调用
|
107 |
+
image_folder = '/workspace/florence-sam/output'
|
108 |
+
with open('/workspace/florence-sam/output_video.pkl', 'rb') as file:
|
109 |
+
output_video = pickle.load(file)
|
110 |
+
print(output_video)
|
111 |
+
input_video_path = output_video
|
112 |
+
|
113 |
+
create_video_with_audio(image_folder, input_video_path)
|
114 |
+
''''''
|