Repository navigation
Fix frame extraction failure with pathlib video selections - #3449
Conversation
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.
There was a problem hiding this comment.
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_listas 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, butextract_frames()writes viaskimage.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.
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.
…b.com/DeepLabCut/DeepLabCut into cy/fix-frame-extraction-path-matching
|
Thanks for addressing this! And great that you added tests 💪 |
|
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. |
|
@deruyter92 Thanks for the review ! As discussed, I think we're better off implementing additional validation for e.g project layout and keypoint data. |
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.
|
All comments addressed, thank you! |
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
left a comment
There was a problem hiding this comment.
In a good state but still a few small fixes required I think. Let me know if you agree.
| @@ -229,15 +248,23 @@ def extract_frames( | |||
| from skimage.util import img_as_ubyte | |||
|
|
|||
There was a problem hiding this comment.
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.
"""There was a problem hiding this comment.
I updated most of it to make it clearer and more consistent e.g. with defaults args, let me know what you think
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.
deruyter92
left a comment
There was a problem hiding this comment.
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"]) |
There was a problem hiding this comment.
Effectively dead code now, since ProjectConfig has no field video_sets_original
| videos = list(cfg.video_sets) |
| video_sets[matching_keys[0]] = temp | ||
|
|
There was a problem hiding this comment.
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.
Motivation
Frame extraction could process zero videos when
videos_listcontainedPathobjects and configuration keys were strings.This caused valid videos to be incorrectly reported as corrupted.
Closes #3448.
Fix