尝试将图像输入模型时出现 utf-8 解码错误

utf-8 decode error when trying to feed an image into a model

我已经成功创建了一个网页,该网页获取图像文件并将其传递给我构建的 API。唯一的问题是,一旦我将该图像从 tensorflow 提供给 preprocessing.image.load_img,我就会收到此错误:

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

这里是 API:

from starlette.responses import RedirectResponse
from fastapi import FastAPI, File, UploadFile
from tensorflow.keras import preprocessing
from fastapi.staticfiles import StaticFiles
from keras.models import load_model
import numpy as np
import uvicorn

app = FastAPI()
app.mount("/Templates", StaticFiles(directory="Templates"), name="Templates")

model_dir = 'F:\Saved-Models\Dog-Cat-Models\First_Generation_dog_cat_optuna.h5'
model = load_model(model_dir)


@app.get('/')
async def index():
    return RedirectResponse(url="/Templates/index.html")


@app.post('/prediction_page')
async def prediction_form(dogcat_img: UploadFile = File(...)):
    dogcat_img_bytes = dogcat_img.file.read()

    pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
    pp_dogcat_image_arr = preprocessing.image.img_to_array(pp_dogcat_image)
    input_arr = np.array([pp_dogcat_image_arr])
    prediction = np.argmax(model.predict(input_arr), axis=-1)

    print(prediction)


if __name__ == '__main__':
    uvicorn.run(app, host='localhost', port=8000)

没有完整的 traceback 异常可能很难帮助,但查看文档,tf.keras.utils.load_img 需要图像文件(不是原始图像数据)的路径。

你可以尝试这样的事情(底层 library 做类似的事情):

--- orig.py 2021-09-20 10:47:22.465636386 +0100
+++ new.py  2021-09-20 10:48:50.760734720 +0100
@@ -6,6 +6,8 @@
 import numpy as np
 import uvicorn
 
+from PIL import Image
+
 app = FastAPI()
 app.mount("/Templates", StaticFiles(directory="Templates"), name="Templates")
 
@@ -20,9 +22,10 @@
 
 @app.post('/prediction_page')
 async def prediction_form(dogcat_img: UploadFile = File(...)):
-    dogcat_img_bytes = dogcat_img.file.read()
+    # dogcat_img_bytes = dogcat_img.file.read()
 
-    pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
+    # pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
+    pp_dogcat_image = Image.open(dogcat_img.file).resize((150, 150), Image.NEAREST).convert("RGB") 
     pp_dogcat_image_arr = preprocessing.image.img_to_array(pp_dogcat_image)
     input_arr = np.array([pp_dogcat_image_arr])
     prediction = np.argmax(model.predict(input_arr), axis=-1)

顺便说一句,您可能还想考虑使用 BackgroundTasks 进行图像处理,否则单个长 运行 (async) 请求将阻止其他请求。