我已经尝试了很多次但它给了我错误 numpy.ndarray 对象没有属性'read' 我正在使用 cnn 模型对两个类进行分类
app = FastAPI()
MODEL = tf.keras.models.load_model("models/16") #model saved with tensorflow save lib
CLASS_NAMES = ["benign","malignant"]
@app.get("/ping")
async def ping():
return "Hello, I am alive"
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image
@app.post("/predict")
async def predict(
file: UploadFile = File(...)
):
image = read_file_as_image(await file.read())
image = np.array(
Image.open(image).convert("RGB").resize((256, 256)) # image resizing
)
image = image / 255
img_batch = np.expand_dims(image, 0)
predictions = MODEL.predict(img_batch)
predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
confidence = np.max(predictions[0])
return {
'class': predicted_class,
'confidence': float(confidence)
}
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
AttributeError:“numpy.ndarray”对象没有属性“读取”