Repository navigation
fix(cv2): turn rotated videos upright without OpenCV - #2601
Merged
Borda merged 4 commits intoSep 21, 2026
Merged
Conversation
OpenCV's FFmpeg backend applies a video's display rotation to decoded frames and to the reported width and height. The PyAV fallback ignored it, so portrait phone videos were read sideways without OpenCV. Co-Authored-By: Claude Opus 5 <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Co-Authored-By: Claude Opus 5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2601 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11462 11482 +20
=======================================
+ Hits 10469 10489 +20
Misses 993 993 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The non-quarter-turn behavior lacks regression coverage, and the changelog modification date is stale.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds PyAV fallback support for video display rotation, matching OpenCV behavior.
Changes:
- Rotates decoded frames and reports upright dimensions.
- Adds fallback and integration tests.
- Documents the fix in the changelog.
Review scores: Code 5/5 · Tests 4/5 · Docs 4/5
File summaries
| File | Description |
|---|---|
src/supervision/_cv2/_video.py |
Applies and caches display rotation metadata. |
tests/cv2/test_video.py |
Tests fallback rotation and OpenCV parity. |
tests/utils/test_video.py |
Tests public video APIs with rotated input. |
docs/changelog.md |
Adds the user-facing fix entry. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Changes: - Integrate current develop into the PR branch and retain both Unreleased changelog entries. Impact: - The PR now includes current target-branch behavior before remediation. Verification: - git merge-tree identified only the changelog conflict; conflict markers resolved and no unmerged paths remain. Residual limits: - Target-branch integration tests have not yet run. Co-authored-by: Codex <[email protected]>
Changes: - Add a 45-degree display-rotation regression case for the PyAV fallback capture. Impact: - Protect the documented no-op behavior for rotations not divisible by 90 degrees. Verification: - uv run pytest tests/cv2/test_video.py -k fallback_capture_turns_rotated_video_upright - uv run ruff check tests/cv2/test_video.py - uv run ruff format --check tests/cv2/test_video.py - git diff --check Residual limits: - OpenCV parity tests remain skipped locally because OpenCV is not installed. Co-authored-by: Codex <[email protected]>
Borda
approved these changes
Sep 21, 2026
3 tasks done
Borda
added a commit
that referenced
this pull request
Sep 22, 2026
OpenCV's FFmpeg backend applies a video's display rotation to decoded frames and to the reported width and height. The PyAV fallback ignored it, so portrait phone videos were read sideways without OpenCV. --------- Co-authored-by: Claude Opus 5 <[email protected]> Co-authored-by: jirka <[email protected]> Co-authored-by: Codex <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Without OpenCV installed, a portrait video recorded on a phone is read sideways. With OpenCV installed, the same calls return it upright:
Phones usually store a portrait clip as landscape frames and record the turn in the container's display matrix. OpenCV's FFmpeg backend reads that matrix (
CAP_PROP_ORIENTATION_AUTOis on by default), turns every decoded frame upright, and swapsCAP_PROP_FRAME_WIDTHandCAP_PROP_FRAME_HEIGHTto match. The PyAV fallback insupervision/_cv2/_video.pyreturnedframe.to_ndarray()and the stream's coded size as they are, so it ignored the matrix.This PR applies the rotation in the fallback the way OpenCV does:
_VideoCapture.readturns each frame byVideoFrame.rotation, which PyAV reads from the same display matrix (available since PyAV 14.2, the minimum Supervision requires)._VideoCapture.getswaps the width and height for a quarter turn. PyAV only exposes the matrix on decoded frames, so_rotationdecodes the first frame from a second handle and caches the angle._frame_countalready opens a second handle in the same way, and this leaves the capture's position untouched.Motivation and Context
The fallback is what runs for anyone without
opencv-python:sv.VideoInfo.from_video_path,sv.get_video_frames_generatorandsv.process_videoall go through it. Phone footage is a common input for these, and without this fix models ran on sideways frames andsv.VideoSinkwrote the annotated output sideways. The image path already handles the equivalent case: the fallbackimreadapplies EXIF orientation as OpenCV does.Changes Made
src/supervision/_cv2/_video.py: new_quarter_turnshelper,_VideoCapture._rotation, and rotation handling in_VideoCapture.getand_VideoCapture.read.tests/cv2/test_video.py:test_fallback_capture_turns_rotated_video_uprightwrites a 32×16 clip with a display rotation of 90, -90, 180 or 0 degrees (VideoStream.set_display_rotation) and expects the upright frame and size.test_fallback_capture_matches_opencv_display_rotationcompares the size and first frame withcv2.VideoCapture, and is skipped without OpenCV.tests/utils/test_video.py:test_video_info_and_frames_follow_display_rotationchecksVideoInfo.from_video_pathandget_video_frames_generatoron a rotated clip. It passes with OpenCV today, and it runs against the fallback in the OpenCV-free CI jobs, wheretests/cv2is skipped.docs/changelog.md: entry under Unreleased.Out of scope: the writer. OpenCV's
VideoWriterdoesn't write a display matrix either, so the output of both backends is already the same.Testing
I checked parity against
cv2.VideoCapture(opencv-python 5.0.0) for rotations of 90, -90, 180 and 270 degrees, for 45 degrees, and for matrices with a horizontal or vertical flip. The fallback's size and first frame matched OpenCV's exactly in every case.New tests on
develop, with OpenCV installed:Without OpenCV (
sys.modules["cv2"] = None, as in the OpenCV-free CI jobs):With this change:
mypyon the changed source and test files reports the same errors ondevelopand on this branch.docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
The test fixtures use
VideoStream.set_display_rotation, which PyAV added in 17.1.uv.lockpins 17.1.0 and 18.0.0, so CI has it. The fix itself only needsVideoFrame.rotation, which PyAV has had since 14.2.🤖 Generated with Claude Code