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

Fix sorting of filenames with alphanumeric prefixes. #248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion sam2/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# LICENSE file in the root directory of this source tree.

import os
import re
import warnings
from threading import Thread

Expand Down Expand Up @@ -101,6 +102,13 @@ def _load_img_as_tensor(img_path, image_size):
return img, video_height, video_width


def _extract_numeric(filename):
# Extract the numeric part from the filename using regex
match = re.search(r"\d+", filename)
# Return the numeric part as an integer, default to 0 if not found
return int(match.group()) if match else 0


class AsyncVideoFrameLoader:
"""
A list of video frames to be load asynchronously without blocking session start.
Expand Down Expand Up @@ -204,7 +212,7 @@ def load_video_frames(
for p in os.listdir(jpg_folder)
if os.path.splitext(p)[-1] in [".jpg", ".jpeg", ".JPG", ".JPEG"]
]
frame_names.sort(key=lambda p: int(os.path.splitext(p)[0]))
frame_names.sort(key=lambda p: _extract_numeric(os.path.splitext(p)[0]))
num_frames = len(frame_names)
if num_frames == 0:
raise RuntimeError(f"no images found in {jpg_folder}")
Expand Down