| 289 | |
| 290 | |
| 291 | def align_frames(first_frame, last_frame): |
| 292 | h1, w1 = first_frame.shape[:2] |
| 293 | h2, w2 = last_frame.shape[:2] |
| 294 | if (h1, w1) == (h2, w2): |
| 295 | return last_frame |
| 296 | ratio = min(w1 / w2, h1 / h2) |
| 297 | new_w = int(w2 * ratio) |
| 298 | new_h = int(h2 * ratio) |
| 299 | resized = cv2.resize(last_frame, (new_w, new_h), interpolation=cv2.INTER_AREA) |
| 300 | aligned = np.ones((h1, w1, 3), dtype=np.uint8) * 255 |
| 301 | x_offset = (w1 - new_w) // 2 |
| 302 | y_offset = (h1 - new_h) // 2 |
| 303 | aligned[y_offset:y_offset + new_h, x_offset:x_offset + new_w] = resized |
| 304 | return aligned |
| 305 | |
| 306 | |
| 307 | def save_sam2_video(video_path, video_segments, output_video_path): |