caicloud / tensorflow-tutorial

Example TensorFlow codes and Caicloud TensorFlow as a Service dev environment.
2.93k stars 2.08k forks source link

第七章图片预处理关于完整样例中读取文件报错 #60

Closed saselovejulie closed 7 years ago

saselovejulie commented 7 years ago

image_raw_data = tf.gfile.FastGFile("../../datasets/cat.jpg", "r").read() 代码中读取图片的代码. 在我本地报错.UnicodeDecodeError: 'utf-8' codec can't decode byte error

将 image_raw_data = tf.gfile.FastGFile("../../datasets/cat.jpg", "r").read() 改为 image_raw_data = tf.gfile.FastGFile("../../datasets/cat.jpg", "rb").read() 错误消失, 我初学python,查了一下rb是读取二进制文件. 请问老师是否要改成rb? 谢谢 1500480054

ScorpioCPH commented 7 years ago

@saselovejulie 脚本里面,最前面加上下面这句试下:

# coding=utf-8
saselovejulie commented 7 years ago

@ScorpioCPH 加上了还是有这个issue, 麻烦有时间看看.谢谢 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

saselovejulie commented 7 years ago

1500553139

ScorpioCPH commented 7 years ago

试一下下面这个代码,测试过可以正常工作:

# coding=utf-8

import tensorflow as tf

# cat.jpg 放到相同目录
image_raw_data = tf.gfile.FastGFile("./cat.jpg",'r').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)

    # 输出解码之后的三维矩阵
    print 'img_data: ', img_data.eval()
    img_data.set_shape([1797, 2673, 3])
    print 'img_shape: ', img_data.get_shape()

PS,你用的 python 2 还是 3

ScorpioCPH commented 7 years ago

@saselovejulie

saselovejulie commented 7 years ago

@ScorpioCPH 谢谢, 我试试. 我是Python3.5

saselovejulie commented 7 years ago

@ScorpioCPH 你给我的代码还是改成rb, 看来是python版本问题... thanks

ScorpioCPH commented 7 years ago

我用的 python 2.7,那可能是版本的问题