svjack commited on
Commit
4d779f8
·
verified ·
1 Parent(s): c447251

Upload 3 files

Browse files
.gitattributes CHANGED
@@ -36,3 +36,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
36
  build/lib.linux-x86_64-cpython-310/sam2/_C.so filter=lfs diff=lfs merge=lfs -text
37
  build/temp.linux-x86_64-cpython-310/sam2/csrc/connected_components.o filter=lfs diff=lfs merge=lfs -text
38
  sam2/_C.so filter=lfs diff=lfs merge=lfs -text
 
 
36
  build/lib.linux-x86_64-cpython-310/sam2/_C.so filter=lfs diff=lfs merge=lfs -text
37
  build/temp.linux-x86_64-cpython-310/sam2/csrc/connected_components.o filter=lfs diff=lfs merge=lfs -text
38
  sam2/_C.so filter=lfs diff=lfs merge=lfs -text
39
+ 逆风散羽之别.mp4 filter=lfs diff=lfs merge=lfs -text
video_filter.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ python video_filter.py input_video.mp4 mask_area_ratios.json 0.1 filtered_video_01.mp4
3
+
4
+ python video_filter.py input_video.mp4 mask_area_ratios.json 0.0 filtered_video_00.mp4
5
+ '''
6
+
7
+ import argparse
8
+ import cv2
9
+ import json
10
+ import os
11
+ import numpy as np
12
+ from moviepy.editor import ImageSequenceClip
13
+
14
+ def process_video(video_path, json_path, threshold, output_path):
15
+ # 读取视频
16
+ if os.path.exists("input_video.mp4"):
17
+ video_path = "input_video.mp4"
18
+
19
+ cap = cv2.VideoCapture(video_path)
20
+ input_fps = cap.get(cv2.CAP_PROP_FPS) # 获取输入视频的帧率
21
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 获取总帧数
22
+
23
+ # 读取所有帧
24
+ all_frames = []
25
+ while True:
26
+ ret, frame = cap.read()
27
+ if not ret:
28
+ break
29
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转换为RGB
30
+ all_frames.append(frame_rgb)
31
+ cap.release()
32
+
33
+ # 读取JSON文件
34
+ with open(json_path, 'r') as f:
35
+ mask_area_ratios = json.load(f)
36
+
37
+ # 获取mask_area_ratios中的帧索引和对应的ratio
38
+ frame_indices = [int(key.split("_")[1]) for key in mask_area_ratios.keys()]
39
+ ratios = [mask_area_ratios[key] for key in mask_area_ratios.keys()]
40
+
41
+ # 创建一个长度为 total_frames 的数组来存储对应的 ratio
42
+ all_ratios = [None] * len(all_frames)
43
+
44
+ # 将 mask_area_ratios 的帧索引和对应的 ratio 映射到 all_ratios
45
+ for frame_index, ratio in zip(frame_indices, ratios):
46
+ scaled_index = int(frame_index * (len(all_frames) - 1) / max(frame_indices))
47
+ all_ratios[scaled_index] = ratio
48
+
49
+ # 填充缺失的 ratio 值,使用最邻近值
50
+ for i in range(len(all_ratios)):
51
+ if all_ratios[i] is None:
52
+ # 找到最近的非空值
53
+ left_index = i - 1
54
+ right_index = i + 1
55
+ while left_index >= 0 and all_ratios[left_index] is None:
56
+ left_index -= 1
57
+ while right_index < len(all_ratios) and all_ratios[right_index] is None:
58
+ right_index += 1
59
+
60
+ if left_index >= 0 and right_index < len(all_ratios):
61
+ # 使用最近的左边或右边的值
62
+ all_ratios[i] = all_ratios[left_index] if all_ratios[left_index] is not None else all_ratios[right_index]
63
+ elif left_index >= 0:
64
+ all_ratios[i] = all_ratios[left_index]
65
+ elif right_index < len(all_ratios):
66
+ all_ratios[i] = all_ratios[right_index]
67
+
68
+ # 筛选符合条件的帧
69
+ frames = []
70
+ for frame_idx, ratio in enumerate(all_ratios):
71
+ if ratio >= threshold:
72
+ frames.append(all_frames[frame_idx])
73
+
74
+ # 保存符合条件的帧为视频
75
+ if frames:
76
+ clip = ImageSequenceClip(frames, fps=input_fps) # 使用输入视频的帧率
77
+ clip.write_videofile(output_path, codec="libx264")
78
+ print(f"Output video saved to {output_path}")
79
+ else:
80
+ print("No frames meet the threshold condition.")
81
+
82
+ if __name__ == "__main__":
83
+ parser = argparse.ArgumentParser(description="Filter video frames based on mask area ratios.")
84
+ parser.add_argument("video_path", type=str, nargs='?', default="input_video.mp4", help="Path to the input video file. Default: input_video.mp4")
85
+ parser.add_argument("json_path", type=str, nargs='?', default="mask_area_ratios.json", help="Path to the JSON file containing mask area ratios. Default: mask_area_ratios.json")
86
+ parser.add_argument("threshold", type=float, nargs='?', default=0.25, help="Threshold for mask area ratio. Default: 0.25")
87
+ parser.add_argument("output_path", type=str, nargs='?', default="output_seg_video.mp4", help="Path to save the output video. Default: output_seg_video.mp4")
88
+
89
+ args = parser.parse_args()
90
+
91
+ process_video(args.video_path, args.json_path, args.threshold, args.output_path)
video_filter_app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import json
4
+ import os
5
+ import numpy as np
6
+ from moviepy.editor import ImageSequenceClip
7
+
8
+ def process_video(video_path, json_path, threshold):
9
+ # 读取视频
10
+ if os.path.exists("input_video.mp4"):
11
+ video_path = "input_video.mp4"
12
+
13
+ cap = cv2.VideoCapture(video_path)
14
+ input_fps = cap.get(cv2.CAP_PROP_FPS) # 获取输入视频的帧率
15
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 获取总帧数
16
+
17
+ # 读取所有帧
18
+ all_frames = []
19
+ while True:
20
+ ret, frame = cap.read()
21
+ if not ret:
22
+ break
23
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转换为RGB
24
+ all_frames.append(frame_rgb)
25
+ cap.release()
26
+
27
+ # 读取JSON文件
28
+ with open(json_path, 'r') as f:
29
+ mask_area_ratios = json.load(f)
30
+
31
+ # 获取mask_area_ratios中的帧索引和对应的ratio
32
+ frame_indices = [int(key.split("_")[1]) for key in mask_area_ratios.keys()]
33
+ ratios = [mask_area_ratios[key] for key in mask_area_ratios.keys()]
34
+
35
+ # 创建一个长度为 total_frames 的数组来存储对应的 ratio
36
+ all_ratios = [None] * len(all_frames)
37
+
38
+ # 将 mask_area_ratios 的帧索引和对应的 ratio 映射到 all_ratios
39
+ for frame_index, ratio in zip(frame_indices, ratios):
40
+ scaled_index = int(frame_index * (len(all_frames) - 1) / max(frame_indices))
41
+ all_ratios[scaled_index] = ratio
42
+
43
+ # 填充缺失的 ratio 值,使用最邻近值
44
+ for i in range(len(all_ratios)):
45
+ if all_ratios[i] is None:
46
+ # 找到最近的非空值
47
+ left_index = i - 1
48
+ right_index = i + 1
49
+ while left_index >= 0 and all_ratios[left_index] is None:
50
+ left_index -= 1
51
+ while right_index < len(all_ratios) and all_ratios[right_index] is None:
52
+ right_index += 1
53
+
54
+ if left_index >= 0 and right_index < len(all_ratios):
55
+ # 使用最近的左边或右边的值
56
+ all_ratios[i] = all_ratios[left_index] if all_ratios[left_index] is not None else all_ratios[right_index]
57
+ elif left_index >= 0:
58
+ all_ratios[i] = all_ratios[left_index]
59
+ elif right_index < len(all_ratios):
60
+ all_ratios[i] = all_ratios[right_index]
61
+
62
+ # 筛选符合条件的帧
63
+ frames = []
64
+ for frame_idx, ratio in enumerate(all_ratios):
65
+ if ratio >= threshold:
66
+ frames.append(all_frames[frame_idx])
67
+
68
+ # 保存符合条件的帧为视频
69
+ if frames:
70
+ output_path = "output_seg_video.mp4"
71
+ clip = ImageSequenceClip(frames, fps=input_fps) # 使用输入视频的帧率
72
+ clip.write_videofile(output_path, codec="libx264")
73
+ return output_path
74
+ else:
75
+ return "No frames meet the threshold condition."
76
+
77
+ # Gradio界面
78
+ iface = gr.Interface(
79
+ fn=process_video,
80
+ inputs=[
81
+ gr.Video(label="Input Video", value="input_video.mp4"),
82
+ gr.Textbox(label="JSON File Path", value="mask_area_ratios.json"),
83
+ gr.Number(label="Threshold", value=0.25)
84
+ ],
85
+ outputs=gr.Video(label="Output Video"),
86
+ title="Video Frame Filter",
87
+ description="Filter video frames based on mask area ratios and save the result as a new video."
88
+ )
89
+
90
+ iface.launch(share=True)
逆风散羽之别.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e24dc9cbb5b65e0a3a77631a99311fb6e048abee86396f0d69a2ceb95998038
3
+ size 59887885