Skip to content

Fix frame extraction failure with pathlib video selections - #3449

Merged
deruyter92 merged 13 commits into
devfrom
cy/fix-frame-extraction-path-matching
Sep 4, 2026
Merged

deruyter92 merged 13 commits into
devfrom
cy/fix-frame-extraction-path-matching

Conversation

@C-Achard

Copy link
Copy Markdown
Member

Motivation

Frame extraction could process zero videos when videos_list contained Path objects and configuration keys were strings.
This caused valid videos to be incorrectly reported as corrupted.

Closes #3448.

Fix

  • Paths are now normalized for comparison while preserving the original config keys
  • Improved the empty selection error handling
  • Added regression/test coverage

Improve frame extraction and GUI cropping to match selected videos against config entries using normalized `Path` values instead of raw string equality. This preserves original config keys for updates, prevents silent no-op runs by raising explicit errors when no selected videos match or none are processed, and ensures GUI-selected files are passed as strings while still resolving path-format differences.
Adds a new test module for `generate_training_dataset.frame_extraction` to cover video path normalization behavior. The tests verify that `extract_frames` accepts `Path` objects in `videos_list`, that `_filter_config_videos` correctly matches `str` and `Path` values while preserving original config keys/types, and that edge cases like `None`, non-matching selections, and Windows case-insensitive matching behave as expected.
@C-Achard
C-Achard requested review from deruyter92 and a lite review from Copilot August 14, 2026 11:33
@C-Achard C-Achard added GUI issues relating to GUI bug fix config Related to config.yaml, ruamel, YAML parsing, ... labels Aug 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where frame extraction could silently process zero videos when videos_list contained pathlib.Path objects while config video-set keys were strings (leading to incorrect “corrupted video” reporting), and adds regression coverage.

Changes:

  • Add path normalization + _filter_config_videos() to compare selected videos against config keys while preserving original config keys for lookups.
  • Improve error handling when no videos are processed / when a selection matches none of the configured videos.
  • Update GUI cropping + extraction to match videos via normalized paths and to pass videos_list as strings; add new tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
deeplabcut/generate_training_dataset/frame_extraction.py Adds normalization/filtering helpers and updates extraction logic + empty-selection handling (but currently introduces a mode=="match" filtering bug).
deeplabcut/gui/tabs/extract_frames.py Uses normalized path matching when updating crop entries and ensures videos_list passed to extraction is stringified.
tests/generate_training_dataset/test_frame_extraction.py Adds regression tests for Path-vs-string filtering (but currently contains import/monkeypatch issues).
Suppressed comments (1)

tests/generate_training_dataset/test_frame_extraction.py:63

  • This monkeypatch targets deeplabcut.utils.io.imsave, but extract_frames() writes via skimage.io.imsave (imported inside the function). As written, the test will still write PNGs to disk and won’t isolate I/O as intended.
    # Avoid writing an actual PNG.
    monkeypatch.setattr(io, "imsave", lambda *args, **kwargs: None)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

C-Achard and others added 3 commits August 14, 2026 13:41
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Update `test_frame_extraction.py` to import `io` from `skimage` and remove the incorrect `deeplabcut.utils` `io` import. This aligns the test with the intended image I/O dependency and avoids using the wrong module.
@deruyter92

Copy link
Copy Markdown
Collaborator

Thanks for addressing this! And great that you added tests 💪

@deruyter92

Copy link
Copy Markdown
Collaborator

Side note: when do we think would be a good moment to update the config schema a bit? I think we all agree that having the file paths as mapping keys in the configs has been the cause of a lot of issues.

@C-Achard

Copy link
Copy Markdown
Member Author

@deruyter92 Thanks for the review !

As discussed, I think we're better off implementing additional validation for e.g project layout and keypoint data.
Then we could progressively migrate things, but maybe best if we start by first having a properly versioned baseline.

@C-Achard C-Achard mentioned this pull request Aug 26, 2026
2 tasks done
Refactors video filtering to use explicit `Iterable`/`Collection` and `Path`-aware type hints, and removes the redundant `normalize_video_path` helper. `extract_frames` now normalizes `videos_list` to `Path` objects up front, making selected-video matching more consistent and type-safe.
@C-Achard
C-Achard requested a review from deruyter92 August 26, 2026 11:24
@C-Achard

Copy link
Copy Markdown
Member Author

All comments addressed, thank you!

@C-Achard
C-Achard marked this pull request as ready for review August 26, 2026 11:25
@deruyter92 deruyter92 added the 3.1 label Aug 28, 2026
Replace normalized string-based video path matching in `select_cropping_area` with direct `Path` object comparison, and remove the now-unused `normalize_video_path` import. This simplifies lookup logic for finding the project config key associated with a selected video.

@deruyter92 deruyter92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a good state but still a few small fixes required I think. Let me know if you agree.

Comment thread deeplabcut/gui/tabs/extract_frames.py Resolved
@@ -229,15 +248,23 @@ def extract_frames(
from skimage.util import img_as_ubyte

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring of extract_frames should be updated to reflect Path support I think.

"""
        ...
        videos_list (list[str | Path], optional): Full paths to the videos to extract
            frames for. If left as ``None``, all videos specified in the config file
            will have frames extracted. Defaults to None.
"""

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated most of it to make it clearer and more consistent e.g. with defaults args, let me know what you think

@deruyter92
deruyter92 changed the base branch from main to dev August 31, 2026 11:49
@deruyter92 deruyter92 added this to the Bug fixes v3.1.0 milestone Aug 31, 2026
Update frame extraction typing to import `Collection`/`Iterable` from `collections.abc` and allow `selected_videos` to contain either `str` or `Path`, matching how config keys are used. Also replace the runtime error when nothing is processed with a clearer message that distinguishes missing/unopenable configured videos from per-video extraction failures.
Use the video selection widget values directly when calling frame extraction, preserving `Path` objects instead of coercing to strings. The related test now checks matching when both configured and selected videos use `Path`, aligning test coverage with the updated GUI behavior.
Reworked the `extract_frames` docstring to provide clearer, structured API documentation. It now better explains behavior across `automatic`, `manual`, and `match` modes, documents argument semantics and defaults in more detail, adds return/raise/warning sections, and refreshes examples (including `videos_list`, GUI cropping, and 3D matching usage).
Move selected-video validation into `_filter_config_videos` so it now raises `ValueError` when a non-empty selection has no config path matches, while still preserving original config keys. This removes duplicated error checks in both `extract_frames` code paths and updates the frame extraction test to expect the new exception behavior.
@C-Achard
C-Achard requested a review from deruyter92 August 31, 2026 13:49

@deruyter92 deruyter92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good PR and I think it can be merged in the current state! It already improves the state a lot!

Not relevant to the current scope, but by design Path-matching for the video set keys stays a bit of a fragile point. We could address that in the future by making them Pydantic models or so.

Also there is still some existing dead code here and there that we should refactor in the future. Just commented here but no need to address now.


cfg = auxiliaryfunctions.read_config(config)
if videos is None:
videos = list(cfg.get("video_sets_original") or cfg["video_sets"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectively dead code now, since ProjectConfig has no field video_sets_original

Suggested change
videos = list(cfg.video_sets)

Comment on lines +79 to 80
video_sets[matching_keys[0]] = temp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still risky right? If different Path representations of the same video differ, instead of changing the value for an existing key, this would write a new key.

Not an issue for now, since you made the callers always pass a key directly from the config. But the whole design stays a bit fragile.

@deruyter92
deruyter92 merged commit 4a82207 into dev Sep 4, 2026
31 checks passed
@deruyter92
deruyter92 deleted the cy/fix-frame-extraction-path-matching branch September 4, 2026 15:05
@deruyter92 deruyter92 added 3.0.2 and removed 3.1 labels Sep 7, 2026
@C-Achard C-Achard linked an issue Sep 16, 2026 that may be closed by this pull request
2 tasks done
@C-Achard C-Achard linked an issue Oct 9, 2026 that may be closed by this pull request
2 tasks done
@C-Achard C-Achard mentioned this pull request Oct 9, 2026
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.0.2 bug fix config Related to config.yaml, ruamel, YAML parsing, ... GUI issues relating to GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Video corrupted when not on config.yaml Extract frames failing on new and old previously successful projects Extracting frames from videos

3 participants