Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of PIL.Image
#3
@deanhystad

I am reposting both the code and the actual error to make sure the error line numbers match up with the code.

This code expects an image posted from an http/post method. I am trying to convert that byte array into an Image object, but it appears I am not following proper conversion steps.

Th error message I presented in the post before, is displayed on my IDE when I hover over line 22 and the "image" is highlighted in the IDE. See the attached image.

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')
Class_Names = ["Early Blight", "Late Blight", "Healthy"]
Image_size = (256, 256)


@app.get("/ping")
async def ping():
    return 'Hello, I am still running'


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


if __name__ == "__main__":
    uvicorn.run(app, host='localhost', port=8000)
Error:
Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "C:\Potato_Leaf\Lib\site-packages\fastapi\__init__.py", line 7, in <module> from .applications import FastAPI as FastAPI File "C:\Potato_Leaf\Lib\site-packages\fastapi\applications.py", line 16, in <module> from fastapi import routing File "C:\Potato_Leaf\Lib\site-packages\fastapi\routing.py", line 22, in <module> from fastapi import params File "C:\Potato_Leaf\Lib\site-packages\fastapi\params.py", line 5, in <module> from fastapi.openapi.models import Example File "C:\Potato_Leaf\Lib\site-packages\fastapi\openapi\models.py", line 4, in <module> from fastapi._compat import ( File "C:\Potato_Leaf\Lib\site-packages\fastapi\_compat.py", line 20, in <module> from fastapi.exceptions import RequestErrorModel File "C:\Potato_Leaf\Lib\site-packages\fastapi\exceptions.py", line 3, in <module> from pydantic import BaseModel, create_model File "C:\Potato_Leaf\Lib\site-packages\pydantic\__init__.py", line 13, in <module> from . import dataclasses File "C:\Potato_Leaf\Lib\site-packages\pydantic\dataclasses.py", line 11, in <module> from ._internal import _config, _decorators, _typing_extra File "C:\Potato_Leaf\Lib\site-packages\pydantic\_internal\_decorators.py", line 15, in <module> from ..fields import ComputedFieldInfo File "C:\Potato_Leaf\Lib\site-packages\pydantic\fields.py", line 18, in <module> from . import types File "C:\Potato_Leaf\Lib\site-packages\pydantic\types.py", line 34, in <module> from ._internal import ( File "C:\Potato_Leaf\Lib\site-packages\pydantic\_internal\_fields.py", line 13, in <module> from . import _typing_extra File "C:\Potato_Leaf\Lib\site-packages\pydantic\_internal\_typing_extra.py", line 13, in <module> from typing_extensions import Annotated, Final, Literal, TypeAliasType, TypeGuard, get_args, get_origin ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (C:\Potato_Leaf\Lib\site-packages\typing_extensions.py) python-BaseException Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Use of PIL.Image - by nafshar - Sep-05-2023, 06:55 PM
RE: Use of PIL.Image - by deanhystad - Sep-05-2023, 07:02 PM
RE: Use of PIL.Image - by nafshar - Sep-05-2023, 07:55 PM
RE: Use of PIL.Image - by nafshar - Sep-05-2023, 08:14 PM
RE: Use of PIL.Image - by deanhystad - Sep-05-2023, 10:11 PM
RE: Use of PIL.Image - by nafshar - Sep-05-2023, 11:53 PM
RE: Use of PIL.Image - by deanhystad - Sep-06-2023, 12:17 AM
RE: Use of PIL.Image - by nafshar - Sep-06-2023, 12:45 AM
RE: Use of PIL.Image - by nafshar - Sep-07-2023, 12:15 AM
RE: Use of PIL.Image - by deanhystad - Sep-07-2023, 03:07 AM
RE: Use of PIL.Image - by nafshar - Sep-07-2023, 03:11 AM
RE: Use of PIL.Image - by deanhystad - Sep-07-2023, 08:54 PM
RE: Use of PIL.Image - by nafshar - Sep-07-2023, 11:02 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020