Skip to content

Commit

Permalink
Add generic [de]serialize_payload function in payload module.
Browse files Browse the repository at this point in the history
Signed-off-by: Bofu Chen (bafu) <bofu@dt42.io>
  • Loading branch information
bafu committed Feb 19, 2018
1 parent eb30d23 commit 189ba96
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions berrynet/comm/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def stringify_jpg(jpg_bytes):
return base64.b64encode(jpg_bytes).decode('utf-8')


def destringify_jpg(serialized_jpg):
def destringify_jpg(stringified_jpg):
"""
:return: JPEG bytes
:rtype: bytes
"""
return base64.b64decode(serialized_jpg.encode('utf-8'))
return base64.b64decode(stringified_jpg.encode('utf-8'))


def jpg2bgr(jpg_bytes):
Expand All @@ -36,6 +36,10 @@ def jpg2rgb(jpg_bytes):
return cv2.cvtColor(jpg2bgr(jpg_bytes), cv2.COLOR_BGR2RGB)


def serialize_payload(json_object):
return json.dumps(json_object)


def serialize_jpg(jpg_bytes):
"""Create Serialized JSON object consisting of image bytes and meta
Expand All @@ -50,14 +54,18 @@ def serialize_jpg(jpg_bytes):
return json.dumps(obj)


def deserialize_jpg(jpg_json):
"""Deserialized JSON object created by josnify_image.
def deserialize_payload(payload):
return json.loads(payload)

:param string :
:return:
:rtype:
"""
return json.loads(jpg_json)

#def deserialize_jpg(jpg_json):
# """Deserialized JSON object created by josnify_image.
#
# :param string :
# :return:
# :rtype:
# """
# return json.loads(jpg_json)


if __name__ == '__main__':
Expand All @@ -66,7 +74,7 @@ def deserialize_jpg(jpg_json):

# size of stringified dog.jpg is 1.33x larger than original
s_jpg = serialize_jpg(jpg_bytes)
d_jpg = deserialize_jpg(s_jpg)
d_jpg = deserialize_payload(s_jpg)
# TODO: Can we write JPEG bytes into file directly to prevent
# bytes -> numpy array -> decode RGB -> write encoded JPEG
cv2.imwrite('/tmp/dog.jpg', jpg2bgr(destringify_jpg(d_jpg['bytes'])))

0 comments on commit 189ba96

Please sign in to comment.