无法将大小为 89401 的数组重塑为形状 (299,299,3)
cannot reshape array of size 89401 into shape (299,299,3)
长期以来,我一直在尝试将我的 PNG 图片 (299,299) 转换为 RGB (299,299,3),我尝试了很多建议的方法,但都没有成功。我正在将邮递员的图像发送到我的 pycharm fastapi 我的图像是 GREYSCALE PNG x 射线图像
code:
from fastapi import FastAPI, File, UploadFile
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
app = FastAPI()
MODEL = tf.keras.models.load_model("../saved_models/1")
CLASS_NAMES = ["COVID", "Lung_Opacity", "Normal", "Viral Pneumonia"]
@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 = image.reshape(299,299,3)
image = Image.fromarray(image)
image = image.convert('RGB')
img_batch = np.expand_dims(image, 0)
predictions = MODEL.predict(img_batch)
pass
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
我是第一次做这个,我不知道我做错了什么,请帮忙。我得到的错误是
cannot reshape array of size 89401 into shape (299,299,3)
为了得到3个通道np.dstack
:
image = np.dstack([image.reshape(299,299)]*3)
或者如果您只想要一个频道
image.reshape(299,299)
长期以来,我一直在尝试将我的 PNG 图片 (299,299) 转换为 RGB (299,299,3),我尝试了很多建议的方法,但都没有成功。我正在将邮递员的图像发送到我的 pycharm fastapi 我的图像是 GREYSCALE PNG x 射线图像
code:
from fastapi import FastAPI, File, UploadFile
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
app = FastAPI()
MODEL = tf.keras.models.load_model("../saved_models/1")
CLASS_NAMES = ["COVID", "Lung_Opacity", "Normal", "Viral Pneumonia"]
@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 = image.reshape(299,299,3)
image = Image.fromarray(image)
image = image.convert('RGB')
img_batch = np.expand_dims(image, 0)
predictions = MODEL.predict(img_batch)
pass
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
我是第一次做这个,我不知道我做错了什么,请帮忙。我得到的错误是
cannot reshape array of size 89401 into shape (299,299,3)
为了得到3个通道np.dstack
:
image = np.dstack([image.reshape(299,299)]*3)
或者如果您只想要一个频道
image.reshape(299,299)