Home > Net >  FastAPI uploaded image as np.float32 to a Deep Learning model (OpenCV)
FastAPI uploaded image as np.float32 to a Deep Learning model (OpenCV)

Time:02-14

I have a TensorFlow Keras Deep Learning model in the form of an h5 file. How can i pass a Fast API uploaded image as np.float32 to my model.

import numpy as np
import cv2
from fastapi import FastAPI, File, UploadFile
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf

model=load_model("complete_model.h5")
app = FastAPI()

def prepare(image):
    IMG_SIZE = 224
    new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE)) 
    return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)

@app.post("/")
async def root(file: UploadFile = File(...)):
    global model
    content = await file.read()
    nparr = np.fromstring(content, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.float32)
    prediction = model.predict(prepare(img))
    return prediction

When uploading image using Swagger UI i get an error

line 137, in jsonable_encoder
data = dict(obj)
TypeError: 'numpy.float32' object is not iterable

Normal code

import numpy as np
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf
import cv2

model=load_model("complete_model.h5")

def prepare(image):
    IMG_SIZE = 224
    new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE)) 
    return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)

img = cv2.imread("./test.jpeg").astype(np.float32)
prediction = model.predict(prepare(img))
print(prediction)

Result in terminal:

[[0.25442022 0.74557984]]

How can I get the same result while using Fast API

CodePudding user response:

The error is thrown when returning the response (i.e., prediction in your case) from your endpoint. It looks like FastAPI is trying to convert the numpy array to a dict. Have a look a the discussion here as well. Thus, what you could do is to convert the numpy array to JSON:

return json.dumps(prediction.tolist())

On OpenAPI (Swagger UI), you will still be able to see the expected result. However, if you need to convert it back to a numpy array, you can parse the JSON string as shown below.

arr = np.array(json.loads(resp.json()))  # resp.json() if using Python requests
  • Related