Repository navigation
FIX: AddImageVideoConverter handles grayscale and 16-bit images, keeps fractional FPS, and raises for unreadable input or an unopenable writer - #3240
Open
Wade (Sriram-PR) wants to merge 5 commits into
Conversation
…s fractional FPS, and raises for unreadable input or an unopenable writer
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
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
Fixes #3238 (the unresolved converter bugs from #3004).
All of these are in
_add_image_to_video_syncinpyrit/converter/add_image_to_video_converter.py:cv2.imdecode(..., IMREAD_UNCHANGED)returns a 2D array for a grayscale PNG, so unpackingoverlay.shapeinto three values raisedValueError. 2D and single-channel images are now converted to BGR before resizing. Gray+alpha PNGs already decode as 4 channels and keep using the alpha blend path.IMREAD_UNCHANGEDalso keeps 16-bit depth, and assigning auint16overlay into theuint8frame silently wrapped the values (a 40000 gray came out as 64). These are now scaled to 8-bit first. This one wasn't in the issue, but it's in the same decode step, so I fixed it here.int(...), so 29.97 stays 29.97 rather than playing slow at 29.VideoCapturecan't open the input, or the input has no frames, the converter raisesValueError("Could not read the input video...")instead of returning empty bytes.ValueErrornaming the extension, codec, FPS and size, instead of returning an empty video. The writer is also created after the overlay is decoded, so a bad overlay image fails before anything is opened.Temp files are still removed on every error path, since the existing
finallyblocks cover the new raises too.Tests and Documentation
New unit tests in
tests/unit/converter/test_add_image_video_converter.py, using synthetic videos written with OpenCV:test_add_image_to_video_accepts_grayscale_image: the gray overlay ends up in the output frame and the rest of the frame stays blacktest_add_image_to_video_scales_16_bit_image: a 40000/65535 gray comes out near 156, not wrappedtest_add_image_to_video_keeps_fractional_frame_rate: 29.97 FPS in, 29.97 outtest_add_image_to_video_raises_for_unreadable_video(b""andb"not a video"): raises, and leaves no temp filestest_add_image_to_video_raises_when_writer_cannot_open: raises before writing any frame, and leaves no temp filesEach new test fails on
mainand passes with the fix. The reproduction script from the issue now printsoutput FPS: 29.97and raises for the unreadable inputs.pytest tests/unit/converterpasses (2475 tests), with Python 3.11 and theopencvandspeechextras.ruff format,ruff checkandty checkpass on the changed files. No docs changes are needed, since the converter's public behaviour and parameters are unchanged apart from raising where it used to return empty output. I didn't run JupyText because no notebooks or docs code changed.