Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert the uploaded image to Numpy array? #2376

Closed
pavitrashah opened this issue Nov 18, 2020 · 7 comments
Closed

How to convert the uploaded image to Numpy array? #2376

pavitrashah opened this issue Nov 18, 2020 · 7 comments
Labels
question Question or problem question-migrate

Comments

@pavitrashah
Copy link

Example Code To Get an Image

from fastapi import FastAPI, UploadFile, File, Form

app = FastAPI()


@app.post("/")
def read_root(file: bytes = File(...)):
    return {"Hello": "World"}

Or

from fastapi import FastAPI, UploadFile, File, Form

app = FastAPI()


@app.post("/")
def read_root(file: UploadFile = File(...)):
    return {"Hello": "World"}

Environment

  • OS: Windows 10
  • FastAPI Version == 0.61.2
  • Python version: 3.7

Question

How to convert the uploaded image to Numpy array? So it can be then used in libraries like openCV, tensorflow for Computer Vision or Deep Learning Applications.

Things I have already tried

from fastapi import FastAPI, UploadFile, File, Form
from PIL import Image
from io import BytesIO
import numpy as np

app = FastAPI()

def read_imagefile(data) -> Image.Image:
    image = Image.open(BytesIO(data))
    return image

@app.post("/")
async def read_root(file: UploadFile = File(...)):
    image = read_imagefile(await file.read())
    return {"Hello": "World"}

And

from fastapi import FastAPI, UploadFile, File, Form
from PIL import Image
from io import BytesIO
import numpy as np

app = FastAPI()

def load_image_into_numpy_array(data):
    return np.array(Image.open(BytesIO(data)))


@app.post("/")
async def read_root(file: UploadFile = File(...)):
    image = load_image_into_numpy_array(await file.read())
    return {"Hello": "World"}

But I keep getting errors. And none of the solutions in any other thread works.
Errors: AttributeError: 'JpegImageFile' object has no attribute 'read' or
AttributeError: 'bytes' object has no attribute 'read' or
AttributeError: 'numpy.ndarray' object has no attribute 'read' or
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

@pavitrashah pavitrashah added the question Question or problem label Nov 18, 2020
@includeamin
Copy link

includeamin commented Nov 18, 2020

both of the codes are working for me.

@app.post("/")
async def read_root(file: UploadFile = File(...)):
    image = load_image_into_numpy_array(await file.read())
    print(image.data)
    return {"Hello": "World"}
curl -X POST "http://localhost:3000/" -H  "accept: application/json" -H  "Content-Type: multipart/form-data" -F "file=@image (31).png;type=image/png"

here the output:
image

@pavitrashah
Copy link
Author

Thanks for the reply. Both the codes also worked out for me apparently I had made a mistake in other part of my code.

@tiangolo
Copy link
Owner

Thanks for the help here @includeamin ! 👏 🙇

Thanks for reporting back and closing the issue @pavitrashah 👍

@belito3
Copy link

belito3 commented Apr 2, 2021

Probably, we need convert image's color from BGR to RGB

def load_image_into_numpy_array(data):
    npimg = np.frombuffer(data, np.uint8)
    frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
    cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    return frame

@chaitanyakkumar
Copy link

hi guys. I am working on a similar code like yours but instead of printing the numpy array I need to return the numpy array. While returning the numpy array I am getting internal server error

@maazbin
Copy link

maazbin commented Nov 18, 2022

Thanks for the code. It worked and solved my problem.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7048 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

7 participants