-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
48 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,24 @@ | ||
import numbers | ||
import numpy as np | ||
from . import ImageChunkCrafter | ||
from typing import Union, Tuple, List, Dict | ||
|
||
|
||
class ImageResizer(ImageChunkCrafter): | ||
def __init__(self, | ||
output_dim: int, | ||
how='BILINEAR', | ||
*args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
if isinstance(output_dim, numbers.Number): | ||
self.output_dim = output_dim | ||
else: | ||
raise ValueError('output_dim {} should be an integer'.format(output_dim)) | ||
self.how = how | ||
|
||
def craft(self, blob: 'np.ndarray', chunk_id: int, doc_id: int, *args, **kwargs) -> Dict: | ||
raw_img = self._load_image(blob) | ||
processed_img = self._resize_short(raw_img, self.output_dim, self.how) | ||
return dict( | ||
doc_id=doc_id, offset=0, weight=1., blob=np.asarray(processed_img).astype('float32')) | ||
|
This file contains 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 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,19 @@ | ||
import unittest | ||
|
||
from jina.executors.crafters.image.resize import ImageResizer | ||
from tests.executors.crafters.image import JinaImageTestCase | ||
|
||
|
||
class MyTestCase(JinaImageTestCase): | ||
def test_resize(self): | ||
img_width = 20 | ||
img_height = 17 | ||
output_dim = 71 | ||
crafter = ImageResizer(output_dim=output_dim) | ||
img_array = self.create_test_img_array(img_width, img_height) | ||
crafted_chunk = crafter.craft(img_array, chunk_id=0, doc_id=0) | ||
self.assertEqual(min(crafted_chunk['blob'].shape[:-1]), output_dim) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |