more memory fixes hopeufly
This commit is contained in:
@@ -398,44 +398,50 @@ class VR180Processor(VideoProcessor):
|
||||
|
||||
self._print_memory_step(f"After SAM2 propagation ({eye_name} eye)")
|
||||
|
||||
# Apply masks - need to reload frames from temp video since we freed the original frames
|
||||
self._print_memory_step(f"Before reloading frames for mask application ({eye_name} eye)")
|
||||
# Apply masks with streaming approach (no frame accumulation)
|
||||
self._print_memory_step(f"Before streaming mask application ({eye_name} eye)")
|
||||
|
||||
# Read frames back from the temp video for mask application
|
||||
# Process frames one at a time without accumulation
|
||||
cap = cv2.VideoCapture(str(temp_video_path))
|
||||
reloaded_frames = []
|
||||
|
||||
for frame_idx in range(num_frames):
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
reloaded_frames.append(frame)
|
||||
cap.release()
|
||||
|
||||
self._print_memory_step(f"Reloaded {len(reloaded_frames)} frames for mask application")
|
||||
|
||||
# Apply masks
|
||||
matted_frames = []
|
||||
for frame_idx, frame in enumerate(reloaded_frames):
|
||||
if frame_idx in video_segments:
|
||||
frame_masks = video_segments[frame_idx]
|
||||
combined_mask = self.sam2_model.get_combined_mask(frame_masks)
|
||||
|
||||
try:
|
||||
for frame_idx in range(num_frames):
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
matted_frame = self.sam2_model.apply_mask_to_frame(
|
||||
frame, combined_mask,
|
||||
output_format=self.config.output.format,
|
||||
background_color=self.config.output.background_color
|
||||
)
|
||||
else:
|
||||
matted_frame = self._create_empty_mask_frame(frame)
|
||||
|
||||
matted_frames.append(matted_frame)
|
||||
# Apply mask to this single frame
|
||||
if frame_idx in video_segments:
|
||||
frame_masks = video_segments[frame_idx]
|
||||
combined_mask = self.sam2_model.get_combined_mask(frame_masks)
|
||||
|
||||
matted_frame = self.sam2_model.apply_mask_to_frame(
|
||||
frame, combined_mask,
|
||||
output_format=self.config.output.format,
|
||||
background_color=self.config.output.background_color
|
||||
)
|
||||
else:
|
||||
matted_frame = self._create_empty_mask_frame(frame)
|
||||
|
||||
matted_frames.append(matted_frame)
|
||||
|
||||
# Free the original frame immediately (no accumulation)
|
||||
del frame
|
||||
|
||||
# Periodic cleanup during processing
|
||||
if frame_idx % 100 == 0 and frame_idx > 0:
|
||||
import gc
|
||||
gc.collect()
|
||||
|
||||
finally:
|
||||
cap.release()
|
||||
|
||||
# Free reloaded frames and video segments completely
|
||||
del reloaded_frames
|
||||
# Free video segments completely
|
||||
del video_segments # This holds processed masks from SAM2
|
||||
self._aggressive_memory_cleanup(f"After mask application ({eye_name} eye)")
|
||||
self._aggressive_memory_cleanup(f"After streaming mask application ({eye_name} eye)")
|
||||
|
||||
self._print_memory_step(f"Completed streaming mask application ({eye_name} eye)")
|
||||
return matted_frames
|
||||
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user