tensorflow decode base64 解析图像

Base64是网络上最常见的用于传输字节码的编码方式之一,可用于在HTTP环境下传递较长的信息。如果要通过Http调用tensorflow服务完成图像分类或者检测等任务,就需要用base64来传递图像信息。tensorflow也提供了函数decode_base64来解析图像。

tensorflow decode_base64函数的使用方法如下:

def base64_decode_img(b64code):
    """
    :param b64code:输入为图像的base64编码字符串
    :return:
    """
    base64_tensor = tf.convert_to_tensor(b64code, dtype=tf.string)
    img_str = tf.decode_base64(base64_tensor) 
    #得到(width, height, channel)的图像tensor
    img = tf.image.decode_image(img_str, channels=3) 
    with tf.Session() as sess:
        img_value = sess.run([img])[0] #得到numpy array类型的数据
        print(img_value.shape)  

使用decode_base64有可能会遇到invalid character的异常。这是因为decode_base64要求输入是web-safe的base64编码,编码后的字符串中不应该有+或者/。下面这段代码是decode_base64的函数声明:

def decode_base64(input, name=None):
  r"""Decode web-safe base64-encoded strings.

  Input may or may not have padding at the end. See EncodeBase64 for padding.
  Web-safe means that input must use - and _ instead of + and /.

python语言可以用base64.urlsafe_b64encode函数代替base64.b64encode来生成web-safe的base64字符串。

def base64_encode_img(img):
    """
    :param img:
    :return:
    """
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    print('shape of img_rgb:', img_rgb.shape)
    pil_img = Image.fromarray(img_rgb)

    buf = cStringIO.StringIO()
    pil_img.save(buf, format="JPEG", quality=100)
    b64code = base64.urlsafe_b64encode(buf.getvalue()) #web-safe
    # b64code = base64.b64encode('abcdefgdisoaufd,0.342,0.456,0.987')  # not web-safe
    return b64code
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符识别。安装tensorflow。 conda create -n cpu_avx2 python=3.7 The following packages will be downloaded: package | build ---------------------------|----------------- certifi-2020.4.5.1 | py37_0 156 KB openssl-1.1.1g | he774522_0 4.8 MB python-3.7.7 | h60c2a47_2 18.3 MB setuptools-46.1.3 | py37_0 528 KB sqlite-3.31.1 | h2a8f88b_1 1.3 MB zlib-1.2.11 | h62dcd97_4 132 KB ------------------------------------------------------------ Total: 25.2 MB The following NEW packages will be INSTALLED: ca-certificates pkgs/main/win-64::ca-certificates-2020.1.1-0 certifi pkgs/main/win-64::certifi-2020.4.5.1-py37_0 openssl pkgs/main/win-64::openssl-1.1.1g-he774522_0 pip pkgs/main/win-64::pip-20.0.2-py37_1 python pkgs/main/win-64::python-3.7.7-h60c2a47_2 setuptools pkgs/main/win-64::setuptools-46.1.3-py37_0 sqlite pkgs/main/win-64::sqlite-3.31.1-h2a8f88b_1 vc pkgs/main/win-64::vc-14.1-h0510ff6_4 vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1 wheel pkgs/main/win-64::wheel-0.34.2-py37_0 wincertstore pkgs/main/win-64::wincertstore-0.2-py37_0 zlib pkgs/main/win-64::zlib-1.2.11-h62dcd97_4 Proceed ([y]/n)? y Downloading and Extracting Packages setuptools-46.1.3 | 528 KB | ############################################################################ | 100% python-3.7.7 | 18.3 MB | ############################################################################ | 100% certifi-2020.4.5.1 | 156 KB | ############################################################################ | 100% zlib-1.2.11 | 132 KB | ###################

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值