Skip to content

Commit

Permalink
Direct support for grayscale images.
Browse files Browse the repository at this point in the history
Grayscale images are interpreted by VisionBonnet as RGB where all
channels are the same. This is just an optimization to send 3 times less
data over SPI.

Change-Id: I0322cbff8381f295772b1eadead0a4d0d7f335fc
  • Loading branch information
dmitriykovalev authored and Peter Malkin committed Jan 3, 2018
1 parent 37aae14 commit 549edfc
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/aiy/vision/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ def load_model(self, descriptor):

try:
self._communicate(request)
except InferenceException:
logging.warning('Model "%s" is already loaded.', descriptor.name)
except InferenceException as e:
logging.warning(str(e))

return descriptor.name

Expand Down Expand Up @@ -257,20 +257,26 @@ def image_inference(self, model_name, image, params=None):
"""

assert model_name, 'model_name must not be empty'
assert image.mode == 'RGB', 'Only image.mode == RGB is supported.'

logging.info('Image inference with model "%s"...', model_name)

r, g, b = image.split()
width, height = image.size

request = protocol_pb2.Request()
request.image_inference.model_name = model_name
request.image_inference.tensor.shape.height = height
request.image_inference.tensor.shape.width = width
request.image_inference.tensor.shape.depth = 3
request.image_inference.tensor.data = (
_tobytes(r) + _tobytes(g) + _tobytes(b))

if image.mode == 'RGB':
r, g, b = image.split()
request.image_inference.tensor.shape.depth = 3
request.image_inference.tensor.data = _tobytes(r) + _tobytes(
g) + _tobytes(b)
elif image.mode == 'L':
request.image_inference.tensor.shape.depth = 1
request.image_inference.tensor.data = _tobytes(image)
else:
assert False, 'Only RGB and L modes are supported.'

for key, value in (params or {}).items():
request.image_inference.params[key] = str(value)
Expand Down

0 comments on commit 549edfc

Please sign in to comment.