Sep-05-2023, 06:55 PM
The following code generates an error which I am not understanding:
I am not sure what this error is occurring, since np.array() mehtod can convert PIL.Image.open(BytesIO(data))
I would be grateful for your help.
from fastapi import FastAPI, File, UploadFile import uvicorn import numpy as np from io import BytesIO from PIL import Image import tensorflow as tf app = FastAPI() MODEL = tf.keras.models.load_model('./code/Model-v1.keras') Image_size = (256, 256) def read_file_as_image(data) -> np.ndarray: image = Image.open(BytesIO(data)) image = np.array(image) return image @app.post("/predict") async def predict( file: UploadFile = File(...) ): image = read_file_as_image(await file.read()) # resize the image image.resize(Image_size) # expand the image dimensions by 1 to simulate a batch of images expanded_image = np.expand_dims(image, axis=0) # Now call predict with this image prediction = MODEL.predict(expanded_image) return
Error:Expected type 'ndarray | Iterable | int | float', got 'Image' instead
The error is referring to line 15.I am not sure what this error is occurring, since np.array() mehtod can convert PIL.Image.open(BytesIO(data))
I would be grateful for your help.