TensorFlow學習('utf-8' codec can't decode byte 0xff in position 0: invalid start byte)


使用語句:

1 image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read()

讀取圖像時報錯如下:

1 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

原因:

0x92 即 10010010,UTF8 中編碼一個字符的第一個字節(start byte)只可能是 0xxxxxxx、110xxxxx、1110xxx、11110xxx……而后面的字節只可能是 10xxxxxx。也就是說 0x92 只能作為后面的字節,卻出現在了第一個字節的位置。

出現這種問題絕大部分情況是因為文件不是 UTF8 編碼的(例如,可能是 GBK 編碼的),而系統默認采用 UTF8 解碼。解決方法是改為對應的解碼方式。

解決方案:

將“r”改為“rb”的形式,如下:

1 image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM