MCPcopy Create free account
hub / github.com/cvg/megaflow / process_sequence

Function process_sequence

demo_flow.py:99–185  ·  view source on GitHub ↗
(args, model, device)

Source from the content-addressed store, hash-verified

97
98
99def process_sequence(args, model, device):
100 print(f"Loading frames from {args.input}...")
101
102 native_size = None
103 input_video_fps = None
104 input_image = []
105
106 for frame, orig_shape, extracted_fps in get_frames(args.input, args.fix_width):
107 if native_size is None:
108 native_size = orig_shape
109
110 # Capture the FPS from the first yielded frame
111 if input_video_fps is None and extracted_fps is not None:
112 input_video_fps = extracted_fps
113
114 img_t = torch.from_numpy(frame).permute(2, 0, 1).float()
115 input_image.append(img_t)
116
117 if len(input_image) < 2:
118 print("Sequence too short (requires at least 2 frames).")
119 return
120
121 input_scene = torch.stack(input_image, dim=0)[None]
122 B, T, C, H, W = input_scene.shape
123
124 is_video_out = False
125 video_writer = None
126 out_dir = None
127
128 if args.output.lower().endswith(('.mp4', '.avi', '.mkv', '.webm')):
129 is_video_out = True
130 os.makedirs(os.path.dirname(os.path.abspath(args.output)) or ".", exist_ok=True)
131
132 # Prioritize native video FPS, fallback to args.fps for image folders
133 final_fps = input_video_fps if input_video_fps is not None else args.fps
134 print(f"Encoding output video at {final_fps:.2f} FPS")
135
136 video_writer = imageio.get_writer(args.output, fps=final_fps, codec='libx264', macro_block_size=None)
137 else:
138 out_dir = args.output
139 os.makedirs(out_dir, exist_ok=True)
140
141 print(f"Processing sequence of {T} frames via window size {args.window_size}...")
142 infer_window = args.window_size
143
144 for start in tqdm(range(0, T - 1, infer_window - 1)):
145 end = min(start + infer_window, T)
146 chunk = input_scene[:, start:end].to(device)
147
148 compute_dtype = torch.bfloat16 if device == "cuda" and torch.cuda.is_bf16_supported() else torch.float16
149 with torch.autocast(device_type=device, dtype=compute_dtype, enabled=(device == "cuda")):
150 results_dict = model(chunk, num_reg_refine=args.iters)
151
152 flow_pr = results_dict['flow_preds'][-1]
153
154 if native_size is not None and getattr(args, 'restore_size', False):
155 scaled_flow = F.interpolate(flow_pr.view(-1, 2, H, W), size=native_size, mode='bilinear', align_corners=True)
156

Callers 1

mainFunction · 0.70

Calls 2

flow_to_imageFunction · 0.90
get_framesFunction · 0.70

Tested by

no test coverage detected