-
Notifications
You must be signed in to change notification settings - Fork 6
Shrink oversized images client-side before submission #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ed61b1e
first commit
4e94f99
addressing PR feedback
24764e3
adding an end-to-end test
b34711a
adding a space
c5593a8
Automatically reformatting code
71af6f5
adjusting a comment
816ae6b
Merge branch 'tim/client-side-image-shrinking' of github.com:groundli…
168ed89
refactoring tests to not use numpy
3b8419d
Automatically reformatting code
cee2d0c
fixing a broken test
9c85a95
Merge branch 'tim/client-side-image-shrinking' of github.com:groundli…
d9cc52b
refactoring tests
c715466
Automatically reformatting code
cb80fc1
fixing failing test
dd0f499
fixing merge conflict
8a2adaf
Automatically reformatting code
a05e597
fixing typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """Tests for image handling behavior in Groundlight.submit_image_query.""" | ||
|
|
||
| from io import BytesIO | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from groundlight import Groundlight | ||
| from groundlight.images import MAX_BYTES_IMAGE_SIZE, MAX_IMAGE_RESOLUTION_LONGSIDE | ||
| from groundlight.internalapi import InternalApiError | ||
| from PIL import Image | ||
| from utils import make_random_jpeg | ||
|
|
||
|
|
||
| def test_submit_image_query_sends_shrunken_image(gl: Groundlight): | ||
| """Verifies that image shrinking runs in the submission path by inspecting the bytes at the HTTP layer. | ||
|
|
||
| Submits an oversized image to a mocked urllib3 transport, then checks that the body | ||
| that actually went on the wire was already resized to the expected dimensions. | ||
| """ | ||
| big = make_random_jpeg(4000, 3000) | ||
| assert len(big) > MAX_BYTES_IMAGE_SIZE | ||
|
|
||
| with mock.patch("urllib3.PoolManager.request") as mock_request: | ||
| mock_request.return_value.status = 500 | ||
| with pytest.raises(InternalApiError): | ||
| gl.submit_image_query(detector="det_test", image=big, wait=0) | ||
|
|
||
| body = mock_request.call_args_list[0].kwargs["body"] | ||
| sent_img = Image.open(BytesIO(body)) | ||
| assert max(sent_img.size) == MAX_IMAGE_RESOLUTION_LONGSIDE |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| """Shared utility functions for tests.""" | ||
|
|
||
| import os | ||
| from io import BytesIO | ||
|
|
||
| from PIL import Image | ||
|
|
||
|
|
||
| def make_random_jpeg(width: int, height: int, quality: int = 95) -> bytes: | ||
| """Generate a JPEG with random pixel data.""" | ||
| img = Image.frombytes("RGB", (width, height), os.urandom(width * height * 3)) | ||
| buf = BytesIO() | ||
| img.save(buf, "jpeg", quality=quality) | ||
| return buf.getvalue() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to break our optional pillow dependency with this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, would need to check the rest of the code, but I think we should be able to scale the image before it becomes a bytestream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see that PIL has ever been optional. I think if it becomes optional, we could just turn off image pre-processing on the client side if PIL is not installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for scaling the image before it becomes a bytestream, it seems that would be possible, but only for inputs that are already PIL objects. Claude points out that for string/bytes inputs (e.g. reading a file off disk), there's no PIL object in flight, so we'd still need the post-conversion step as a fallback anyway. Two code paths for marginal gain didn't seem worth it.