Fix: video crops when webcam resolution is not 480p #5
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
If the demo receives webcam video of a resolution different from 480p, the demo does not scale the webcam video or canvas correctly. This appears on both the Roboflow homepage and the test page in this repository. This fix changes
script.js
so that canvas height is fixed to 480 pixels, but canvas width is scaled to match the aspect ratio of incoming video.Loom Explainer
https://www.loom.com/share/53c58cccee104e2d951244d0ddda6032
Motivation
I encountered this bug because I was on a Google Meet call with a colleague while testing the demo. I determined that Google Meet was requesting 720p video from the webcam. The Roboflow demo appears to tap the same video source, so it also received 720p video. It handled this incorrectly; the video cropped to the top left corner of the video. Inference was also performed incorrectly as a result of this.
To Reproduce
Fix
Summary of changes
webcamInference()
videoAspectRatio
as width/heightgetCoordinates()
calculation outside of video render loopgetCoordinates()
calculation of canvas width and heightgetCoordinates()
videoAspectRatio
videoAspectRatio
sWidth
to the incoming true webcam video width, which fixes the scalingRatio calculationcanvasRatio
andimageRatio
are not necessary given that we know the true webcam video dimensionsgetBase64Image()
imageInference()
To Test the Fix
Type of change