Repository navigation
[feature] Add StatsManager.to_dict() for dict and pandas conversion (#507) - #636
leninworld wants to merge 2 commits into
Conversation
| A ``FrameTimecode`` hashes and compares equal to its 0-based ``frame_num``, so | ||
| ``result[5]`` (and ``DataFrame.loc[5]``) finds CFR frame 5. The CSV ``Frame Number`` |
There was a problem hiding this comment.
Could we remove the result[5] / DataFrame.loc[5] references, and say instead that frame numbers are estimates on VFR video (#569), so frames should be looked up by these keys?
| assert stats.get_metrics(frame_2, ["score"]) == [2.0] | ||
|
|
||
|
|
||
| def test_to_dict_pandas(): |
There was a problem hiding this comment.
Can we drop this test? pandas isn't a dev dependency, so it always skips in CI. It's also not really a useful test for this library since the dict is what matters here.
| - [general] The root `pyproject.toml` is now `scenedetect` again (it had a development-only `scenedetect-core` name since 0.7.1). `pip install -e .` now installs the same distribution as PyPI. The `[opencv]` and `[opencv-headless]` extras of the development config were removed because `opencv-python` is now a declared dependency [#558](https://github.com/Breakthrough/PySceneDetect/issues/558) | ||
| - [feature] Add `save-chapters` command to save scenes as chapters in FFmetadata format, which can be added to a video with ffmpeg without re-encoding. Also available as `write_scene_list_ffmetadata()` in the `scenedetect.output` module [#323](https://github.com/Breakthrough/PySceneDetect/issues/323) | ||
| - [feature] Add `--stdin` to read the input video from a pipe, e.g. `ffmpeg -i input.mp4 -c copy -f matroska - | scenedetect --stdin detect-content list-scenes`. Requires the PyAV backend. `save-html` omits images, and commands that need to seek or refer to a source file (`split-video`, `save-images`, `save-fcp`, `save-otio`, `load-scenes`, `time -s`) are not supported [#189](https://github.com/Breakthrough/PySceneDetect/issues/189) [#434](https://github.com/Breakthrough/PySceneDetect/issues/434) | ||
| - [feature] Add `StatsManager.to_dict()` to get all stored metrics as a dict of shallow copies keyed by frame (`FrameTimecode`, which compares equal to its 0-based frame number, or `int`), e.g. for `pandas.DataFrame.from_dict(stats_manager.to_dict(), orient="index")` [#507](https://github.com/Breakthrough/PySceneDetect/issues/507) |
There was a problem hiding this comment.
Could we shorten this to "Add StatsManager.to_dict() to get all frame metrics as a dict, e.g. for pandas.DataFrame.from_dict(stats_manager.to_dict(), orient="index")"?
|
Thanks for the review. All three are done in 6f8d3ef:
Locally, |
This PR addresses part of issue #507.
Who benefits: API users who analyse per-frame detector metrics (for example to pick a representative frame per scene, as in the issue) and want them in a dict or a pandas DataFrame without writing a stats CSV and reading it back.
This adds
StatsManager.to_dict(), which returns every stored frame's metrics as a plain dict:FrameTimecodefor metrics written during detection, orint0-based frame numbers (fromset_metrics(5, ...)or the deprecatedload_from_csv). AFrameTimecodehashes and compares equal to its 0-based frame number, soresult[5]andDataFrame.loc[5]find frame 5 of a constant frame rate video, and the DataFrame index prints as timecodes. The stats CSVFrame Numbercolumn stays 1-based.StatsManager(nested mutable values, if any, stay shared).frame_numints: on VFR video two PTS-based frames can round to the sameframe_num, and converting would merge them and drop metrics.save_to_csv()writes both rows today, so this keeps the two in line. It also keeps int-keyed entries, whichsave_to_csv()skips.I went with a conversion method rather than the full dict protocol: adding
__len__would make an emptyStatsManagerfalsy, and that felt like a decision for the 0.8 API work (#591 also reworks this class), so the TODO aboveget_metricsis left as is. Happy to change the key type or move the changelog entry to 0.8 if you prefer.Changelog entry added under 0.7.4 (TBD) in the Development section.
Tests:
test_to_dict(empty manager, ordering of out-of-order keys,intandFrameTimecodekeys, two VFR frames with the sameframe_numkept apart, int lookup, shallow copies) andtest_to_dict_pandas(DataFrame index, columns and.loc[4]; skipped when pandas isn't installed, since pandas isn't a test dependency). Both fail onmainwithAttributeError. With the resources branch,pytestgives 769 passed, 3 skipped (macOS, Python 3.12, pandas installed).ruff check,ruff format --checkandpyrightpass.Refs #507