Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ from sam2.build_sam import build_sam2_video_predictor
|
|
| 31 |
|
| 32 |
from moviepy.editor import ImageSequenceClip
|
| 33 |
|
| 34 |
-
def
|
| 35 |
# Open the video file
|
| 36 |
cap = cv2.VideoCapture(video_path)
|
| 37 |
|
|
@@ -41,8 +41,15 @@ def get_video_fps(video_path):
|
|
| 41 |
|
| 42 |
# Get the FPS of the video
|
| 43 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
return fps
|
| 46 |
|
| 47 |
def clear_points(image):
|
| 48 |
# we clean all
|
|
@@ -395,7 +402,7 @@ def propagate_to_all(video_in, checkpoint, stored_inference_state, stored_frame_
|
|
| 395 |
return gr.update(value=jpeg_images), gr.update(value=None), gr.update(choices=available_frames_to_check, value=working_frame, visible=True), available_frames_to_check, gr.update(visible=True), None
|
| 396 |
elif vis_frame_type == "render":
|
| 397 |
# Create a video clip from the image sequence
|
| 398 |
-
original_fps =
|
| 399 |
fps = original_fps # Frames per second
|
| 400 |
total_frames = len(jpeg_images)
|
| 401 |
clip = ImageSequenceClip(jpeg_images, fps=fps)
|
|
@@ -413,20 +420,22 @@ def propagate_to_all(video_in, checkpoint, stored_inference_state, stored_frame_
|
|
| 413 |
# Create a video from the masks_images array
|
| 414 |
mask_video_filename = "final_masks_video.mp4"
|
| 415 |
|
| 416 |
-
#
|
| 417 |
-
frame = cv2.imread(masks_images[0])
|
| 418 |
-
height, width, _ = frame.shape
|
| 419 |
-
|
| 420 |
-
# Define the video writer
|
| 421 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 422 |
-
|
| 423 |
-
video_writer = cv2.VideoWriter(mask_video_filename, fourcc, fps, (width, height))
|
| 424 |
|
| 425 |
# Write each mask image to the video
|
| 426 |
for mask_path in masks_images:
|
|
|
|
| 427 |
frame = cv2.imread(mask_path)
|
| 428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
|
|
|
| 430 |
video_writer.release()
|
| 431 |
print(f"Mask Video saved at {mask_video_filename}")
|
| 432 |
|
|
|
|
| 31 |
|
| 32 |
from moviepy.editor import ImageSequenceClip
|
| 33 |
|
| 34 |
+
def get_video_properties(video_path):
|
| 35 |
# Open the video file
|
| 36 |
cap = cv2.VideoCapture(video_path)
|
| 37 |
|
|
|
|
| 41 |
|
| 42 |
# Get the FPS of the video
|
| 43 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 44 |
+
|
| 45 |
+
# Get the width and height of the video
|
| 46 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 47 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 48 |
+
|
| 49 |
+
# Release the video capture object
|
| 50 |
+
cap.release()
|
| 51 |
|
| 52 |
+
return fps, width, height
|
| 53 |
|
| 54 |
def clear_points(image):
|
| 55 |
# we clean all
|
|
|
|
| 402 |
return gr.update(value=jpeg_images), gr.update(value=None), gr.update(choices=available_frames_to_check, value=working_frame, visible=True), available_frames_to_check, gr.update(visible=True), None
|
| 403 |
elif vis_frame_type == "render":
|
| 404 |
# Create a video clip from the image sequence
|
| 405 |
+
original_fps, o_width, o_height = get_video_properties(video_in)
|
| 406 |
fps = original_fps # Frames per second
|
| 407 |
total_frames = len(jpeg_images)
|
| 408 |
clip = ImageSequenceClip(jpeg_images, fps=fps)
|
|
|
|
| 420 |
# Create a video from the masks_images array
|
| 421 |
mask_video_filename = "final_masks_video.mp4"
|
| 422 |
|
| 423 |
+
# Define the video writer with the original video's dimensions
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 425 |
+
video_writer = cv2.VideoWriter(mask_video_filename, fourcc, fps, (o_width, o_height))
|
|
|
|
| 426 |
|
| 427 |
# Write each mask image to the video
|
| 428 |
for mask_path in masks_images:
|
| 429 |
+
# Read the mask frame
|
| 430 |
frame = cv2.imread(mask_path)
|
| 431 |
+
|
| 432 |
+
# Resize the mask frame to the original video's dimensions
|
| 433 |
+
frame_resized = cv2.resize(frame, (o_width, o_height), interpolation=cv2.INTER_NEAREST)
|
| 434 |
+
|
| 435 |
+
# Write the resized frame to the video
|
| 436 |
+
video_writer.write(frame_resized)
|
| 437 |
|
| 438 |
+
# Release the video writer
|
| 439 |
video_writer.release()
|
| 440 |
print(f"Mask Video saved at {mask_video_filename}")
|
| 441 |
|