Skip to content

Infinite Loop in split_frame Function of SAM2 Video Tracking Example #656

Closed as not planned
@Buckler89

Description

@Buckler89

I'm using the SAM2 video tracking example, and I encountered an issue where the program goes into an infinite loop due to an incorrect condition in the split_frame function. Specifically, the issue was that the counter was never incremented under certain conditions, which caused the loop to continue indefinitely without reaching an exit point. This bug made the video tracking example unusable in its current form.

I have identified and fixed the issue by correcting the loop condition, ensuring that the function properly iterates through the frames and exits as expected. With this fix, the code is now working properly, and the video tracking example can proceed as intended without any infinite looping behavior.

I've prepared a pull request with the fix, but since I'm not a collaborator, I'm unable to submit it directly. I would greatly appreciate it if a collaborator could review the changes I've made and apply the fix to the main repository. This would help other users who may encounter the same issue and improve the overall stability of the example.

Here is the corrected code snippet for the split_frames function:

   def split_frames(self, video_path, temp_dir, start_frame=0, end_frame=100):
       logger.debug(f'Opening video file: {video_path}')
       video = cv2.VideoCapture(video_path)

       if not video.isOpened():
           raise ValueError(f"Could not open video file: {video_path}")

       logger.debug(f'Number of frames: {int(video.get(cv2.CAP_PROP_FRAME_COUNT))}')

       frame_count = 0
       while True:
           success, frame = video.read()

           if not success:
               logger.error(f'Failed to read frame {frame_count}')
               break

           if frame_count < start_frame:
               frame_count += 1
               continue

           if frame_count >= end_frame:
               break

           frame_filename = os.path.join(temp_dir, f'{frame_count:05d}.jpg')

           if not os.path.exists(frame_filename):
               cv2.imwrite(frame_filename, frame)

           logger.debug(f'Frame {frame_count}: {frame_filename}')
           yield frame_filename, frame
           frame_count += 1

       video.release()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions