Skip to content

Commit

Permalink
Datachunk list image compression. Closes #234 (#235)
Browse files Browse the repository at this point in the history
* feat(images): Adding images support to DataChunk.

* feat(test): Added unittest for DataChunk.

* fix(gray images): Added support for gray images.

* fix()HW1 instead of HW.
  • Loading branch information
edavalosanaya authored Aug 23, 2023
1 parent eb69518 commit 73b6076
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
19 changes: 19 additions & 0 deletions chimerapy/engine/networking/data_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def __init__(self):
# Creating mapping from content_type and compression method
self._content_type_2_serial_mapping = {
"image": (self._serialize_image, self._deserialize_image),
"images": (self._serialize_images, self._deserialize_images),
}

# Creating mapping for checking content_type
self._content_type_2_checks_mapping = {
"image": self._check_image,
"images": self._check_images,
}

# Adding default key-value pair
Expand Down Expand Up @@ -86,12 +88,29 @@ def _check_image(self, image: np.ndarray):
image.dtype == np.uint8
), f"Numpy image needs to be np.uint8, currently {image.dtype}"

def _check_images(self, images: List[np.ndarray]):
assert isinstance(images, list)

def _serialize_image(self, image: np.ndarray):
if len(image.shape) == 2:
return simplejpeg.encode_jpeg(
np.ascontiguousarray(np.expand_dims(image, axis=-1)), colorspace="GRAY"
)
return simplejpeg.encode_jpeg(np.ascontiguousarray(image))

def _deserialize_image(self, image_bytes: bytes):
# Obtain header first
header = simplejpeg.decode_jpeg_header(image_bytes)
if header[2] == "Gray":
return np.squeeze(simplejpeg.decode_jpeg(image_bytes, colorspace="GRAY"))
return simplejpeg.decode_jpeg(image_bytes)

def _serialize_images(self, images: List[np.ndarray]):
return [self._serialize_image(image) for image in images]

def _deserialize_images(self, images_bytes: List[bytes]):
return [self._deserialize_image(image_bytes) for image_bytes in images_bytes]

####################################################################
# (De)Serialization
####################################################################
Expand Down
29 changes: 28 additions & 1 deletion test/test_data_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,34 @@ def video_data_chunk():
return data_chunk


@pytest.mark.parametrize("data_chunk", [(lazy_fixture("video_data_chunk"))])
@pytest.fixture
def images_data_chunk():
data_chunk = cpe.DataChunk()
data_chunk.add(
"images",
[(np.random.rand(10, 10, 3) * 255).astype(np.uint8) for _ in range(10)],
)
return data_chunk


@pytest.fixture
def grey_images_data_chunk():
data_chunk = cpe.DataChunk()
data_chunk.add(
"images",
[(np.random.rand(10, 10) * 255).astype(np.uint8) for _ in range(10)],
)
return data_chunk


@pytest.mark.parametrize(
"data_chunk",
[
(lazy_fixture("video_data_chunk")),
(lazy_fixture("images_data_chunk")),
(lazy_fixture("grey_images_data_chunk")),
],
)
def test_jsonify_data_chunk(data_chunk):
json_data = json.dumps(data_chunk.to_json())
new_data_chunk = cpe.DataChunk.from_json(json.loads(json_data))
Expand Down

0 comments on commit 73b6076

Please sign in to comment.