在用tensorflow 自带的读取文件gfile模块中,调用API,如:
filename = directory + DIRECTORY_IMAGES + name + '.jpg'
image_data = tf.gfile.FastGFile(filename, 'rb').read()
常出现:error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0错误。
根本原因为:python3中的读取模式‘r’,'rb'存在较大差异,采用'r'模式文件python 解释器认为该文件内容为UTF-8的文本格式,但错误提示则提示该文件为二进制格式(通常图片所采用的格式),因此修改打开模式为‘rb’即可解决,如下:
image_data = tf.gfile.FastGFile(filename, 'rb').read()