Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Buckler89 opened this issue Oct 30, 2024 · 0 comments
Open

Comments

@Buckler89
Copy link

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant