@@ -228,14 +228,16 @@ def create_sprite(self):
228228 example_strings = [
229229 self .json_to_proto (ex ).SerializeToString ()
230230 for ex in self .examples ]
231- encoded = base64 .b64encode (
232- inference_utils .create_sprite_image (example_strings ))
233- if sys .version_info >= (3 , 0 ):
234- encoded = encoded .decode ('utf-8' )
231+ encoded = self ._ensure_string (base64 .b64encode (
232+ inference_utils .create_sprite_image (example_strings )))
235233 return 'data:image/png;base64,{}' .format (encoded )
236234 else :
237235 return None
238236
237+ def _ensure_string (self , str_or_bytes ):
238+ return (str_or_bytes .decode ('utf-8' )
239+ if sys .version_info >= (3 , 0 ) else str_or_bytes )
240+
239241 def _json_from_tf_examples (self , tf_examples ):
240242 json_exs = []
241243 feature_names = self .config .get ('feature_names' )
@@ -264,7 +266,8 @@ def _json_from_tf_examples(self, tf_examples):
264266 elif ex .features .feature [feat ].HasField ('float_list' ):
265267 json_ex [feat_idx ] = ex .features .feature [feat ].float_list .value [0 ]
266268 else :
267- json_ex [feat_idx ] = ex .features .feature [feat ].bytes_list .value [0 ]
269+ json_ex [feat_idx ] = self ._ensure_string (
270+ ex .features .feature [feat ].bytes_list .value [0 ])
268271 else :
269272 json_ex = {}
270273 for feat in ex .features .feature :
@@ -275,7 +278,8 @@ def _json_from_tf_examples(self, tf_examples):
275278 elif ex .features .feature [feat ].HasField ('float_list' ):
276279 json_ex [feat ] = ex .features .feature [feat ].float_list .value [0 ]
277280 else :
278- json_ex [feat ] = ex .features .feature [feat ].bytes_list .value [0 ]
281+ json_ex [feat ] = self ._ensure_string (
282+ ex .features .feature [feat ].bytes_list .value [0 ])
279283 json_exs .append (json_ex )
280284 return json_exs
281285
0 commit comments