fix temp file

This commit is contained in:
2025-07-26 09:01:38 -07:00
parent c4af7baf3d
commit 99c4da83af
2 changed files with 73 additions and 49 deletions

View File

@@ -150,52 +150,75 @@ class VR180Processor(VideoProcessor):
if not eye_frames:
return []
# Initialize SAM2 with eye frames
self.sam2_model.init_video_state(eye_frames)
# Create a unique temporary video for this eye processing
import uuid
temp_video_name = f"temp_sam2_{eye_name}_chunk{chunk_idx}_{uuid.uuid4().hex[:8]}.mp4"
temp_video_path = Path.cwd() / temp_video_name
# Detect persons in first frame
first_frame = eye_frames[0]
detections = self.detector.detect_persons(first_frame)
if not detections:
warnings.warn(f"No persons detected in {eye_name} eye, chunk {chunk_idx}")
return self._create_empty_masks(eye_frames)
print(f"Detected {len(detections)} persons in {eye_name} eye first frame")
# Convert to SAM2 prompts
box_prompts, labels = self.detector.convert_to_sam_prompts(detections)
# Add prompts
object_ids = self.sam2_model.add_person_prompts(0, box_prompts, labels)
# Propagate masks
video_segments = self.sam2_model.propagate_masks(
start_frame=0,
max_frames=len(eye_frames)
)
# Apply masks
matted_frames = []
for frame_idx, frame in enumerate(eye_frames):
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)
try:
# Write frames to temporary video
height, width = eye_frames[0].shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(str(temp_video_path), fourcc, 30.0, (width, height))
matted_frames.append(matted_frame)
# Cleanup
self.sam2_model.cleanup()
return matted_frames
for frame in eye_frames:
writer.write(frame)
writer.release()
# Initialize SAM2 with video path
self.sam2_model.init_video_state(video_path=str(temp_video_path))
# Detect persons in first frame
first_frame = eye_frames[0]
detections = self.detector.detect_persons(first_frame)
if not detections:
warnings.warn(f"No persons detected in {eye_name} eye, chunk {chunk_idx}")
return self._create_empty_masks(eye_frames)
print(f"Detected {len(detections)} persons in {eye_name} eye first frame")
# Convert to SAM2 prompts
box_prompts, labels = self.detector.convert_to_sam_prompts(detections)
# Add prompts
object_ids = self.sam2_model.add_person_prompts(0, box_prompts, labels)
# Propagate masks
video_segments = self.sam2_model.propagate_masks(
start_frame=0,
max_frames=len(eye_frames)
)
# Apply masks
matted_frames = []
for frame_idx, frame in enumerate(eye_frames):
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)
return matted_frames
finally:
# Always cleanup
self.sam2_model.cleanup()
# Remove temporary video file
try:
if temp_video_path.exists():
temp_video_path.unlink()
except Exception as e:
warnings.warn(f"Failed to cleanup temp video {temp_video_path}: {e}")
def _process_eye_sequence_with_validation(self,
right_eye_frames: List[np.ndarray],