Skip to content

Commit

Permalink
feat(crafter): add resize crafters
Browse files Browse the repository at this point in the history
  • Loading branch information
nan-wang committed Apr 8, 2020
1 parent e929ecc commit fd6a2e6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
6 changes: 3 additions & 3 deletions jina/executors/crafters/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def _load_image(self, blob: 'np.ndarray'):
return Image.fromarray(blob.astype('uint8'))

@staticmethod
def _resize_short(img, target_size):
from PIL.Image import LANCZOS
def _resize_short(img, target_size, how='LANCZOS'):
import PIL.Image as Image
percent = float(target_size) / min(img.size[0], img.size[1])
resized_width = int(round(img.size[0] * percent))
resized_height = int(round(img.size[1] * percent))
img = img.resize((resized_width, resized_height), LANCZOS)
img = img.resize((resized_width, resized_height), getattr(Image, how))
return img

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion jina/executors/crafters/image/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self,
self.img_mean = np.array(img_mean).reshape((1, 1, 3))
self.img_std = np.array(img_std).reshape((1, 1, 3))

def craft(self, blob, chunk_id, doc_id, *args, **kwargs) -> Dict:
def craft(self, blob: 'np.ndarray', chunk_id: int, doc_id: int, *args, **kwargs) -> Dict:
"""
:param blob: the ndarray of the image with the color channel at the last axis
Expand Down
24 changes: 24 additions & 0 deletions jina/executors/crafters/image/resize.py
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'))

2 changes: 1 addition & 1 deletion tests/executors/crafters/image/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class MyTestCase(JinaImageTestCase):
def test_transform_results(self):
img_size = 224
img_size = 217
crafter = ImageNormalizer(output_dim=img_size)
img_array = self.create_test_img_array(img_size, img_size)
crafted_chunk = crafter.craft(img_array, chunk_id=0, doc_id=0)
Expand Down
19 changes: 19 additions & 0 deletions tests/executors/crafters/image/test_resize.py
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()

0 comments on commit fd6a2e6

Please sign in to comment.