https://github.com/affinelayer/pix2pix-tensorflow/tree/master/tools https://github.com/affinelayer/pix2pix-tensorflow/tree/master/tools
在上述站点上编译“process.py”时发生错误。
python tools/process.py --input_dir data --operation resize --output_dir data2/resize
data/0.jpg -> data2/resize/0.png
Traceback (most recent call last):
File "tools/process.py", line 235, in <module>
main()
File "tools/process.py", line 167, in main
src = load(src_path)
File "tools/process.py", line 113, in load
contents = open(path).read()
File"/home/user/anaconda3/envs/tensorflow_2/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
错误的原因是什么?
Python的版本是3.5.2。
Python 尝试转换字节数组(abytes
它假设是一个 utf-8 编码的字符串)到一个 unicode 字符串(str
)。这个过程当然是按照utf-8规则进行解码。当它尝试这样做时,它遇到了 utf-8 编码字符串中不允许的字节序列(即位置 0 处的 0xff)。
由于您没有提供任何我们可以查看的代码,我们只能猜测其余的。
从堆栈跟踪中我们可以假设触发操作是从文件中读取(contents = open(path).read()
)。我建议以如下方式重新编码:
with open(path, 'rb') as f:
contents = f.read()
That b
在模式说明符中open()
声明该文件应被视为二进制文件,因此contents
将仍然是bytes
。这样就不会发生解码尝试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)