0

I am trying to run this code from a directory, C:\users\user1\plant_app\main.py Edit: I am able to start the server, but I can't get the numpy array.

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


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

app=FastAPI()
@app.get('/check')
async def check():
     return {'message':'hello world'}
@app.post('/predict')
async def predict(file: UploadFile = File(...)):
     image=read_image(await file.read())
     return image


if __name__=='__main__':
    uvicorn.run(app,host='localhost',port=8000)

I am getting this error.

PS C:\Users\user1\plant_app> python -u "c:\Users\user1\plant_app\main.py"
INFO:     Started server process [21740]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:58115 - "GET / HTTP/1.1" 404 Not Found

What am I doing wrong here?

2
  • 3
    It looks like your code only defines urls for /check and /predict. There is no / url. Commented Jun 25, 2023 at 21:22
  • You might also find this and this helpful Commented Jun 26, 2023 at 5:05

4 Answers 4

0
@app.get('/')
dуf root():
    return {'message':'FastAPI app'}

you should define / url

Sign up to request clarification or add additional context in comments.

1 Comment

That seems to start the server, but when I upload an image using docs I am getting null and {"detail":"Not Found"} with postman @Baktybek Baiserkeev
0
@app.get('/')
async def root():
    return {'message': 'test'}

Try adding this because it seems like the GET route to '/' hasn't been declared.

1 Comment

That seems to start the server, but when I upload an image using docs I am getting null and {"detail":"Not Found"} with postman @Kionling
0

We can access the API from http://localhost:8000/check or http://localhost:8000/predict. Or we can also use SwaggerUI which is integrated with FastAPI to interact with API.

Comments

0

Try this. I think the problem is returning the image. You need to create a response with a type of image (here image/png as my test case was a png file). I wasn't sure of what output you wanted. So, I made three services: one returns the np array, the other returns the shape of the array, and finally the one returns the image itself. Check the following links for more info:

from fastapi import FastAPI, File, UploadFile
import uvicorn
from io import BytesIO
import numpy as np
from PIL import Image
from fastapi.responses import Response
import json

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

app=FastAPI()

@app.get('/')
async def check():
     return {'message':'hello world'}


@app.post('/array')
async def predict_array(file: UploadFile = File(...)):
     content = await file.read()
     image = read_image(content)
     return json.dumps(image.tolist())


@app.post('/shape')
async def predict_shape(file: UploadFile = File(...)):
     content = await file.read()
     image = read_image(content)
     return {'shape': image.shape}

@app.post('/image')
async def return_image(file: UploadFile = File(...)):
     content = await file.read()
     return Response(content=content, media_type="image/png")

if __name__=='__main__':
    uvicorn.run(app,host='localhost', port=8000)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.