I have successfully created a webpage that takes an image file and passes it to the API I built. The only problem is that once I feed that image to preprocessing.image.load_img from tensorflow, I get this error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Here is the 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)