How can I make this process more efficient - Importing data file into numpy array #4355
-
|
Hi all, I am new to FastAPI but I love this library, thank you so much! So here is my problem statement. My code works but it is taking very long to return the response via the browser. I have many steps I want to do to investigate this, but first I wanted to share the problem here to see what your thoughts are. My bottleneck is most likely occurring at the transformation/preprocessing using machine learning, but I can optimise this part myself. The part I can't optimise yet is the method of reading and returning data using FastAPI. I have only included the relevant code to make things easier to follow. Problem statement I have this endpoint where users can upload an image (UploadFile = File(...)). Once the image is uploaded into UploadFile = File(...), I use data = await file.read() to convert it into bytes. Am I correct in doing this, and is it in fact bytes? Secondly, I have this function that converts it into an numpy array, np.array(Image.open(BytesIO(data_obj)))/255. Is there a better way for me to convert the uploaded image into an numpy array? Thirdly, I use matplotlib to plot an image from the pixels and save it locally. I will look for a better way to save an image from numpy array. If anyone has recommendations let me know (will look into opencv, PIL ect...) Finally, Ideally I don't save this image, but the only way I can think of returning it to the end user is by using return FileResponse(path=filepath, filename=filename, media_type='image/jpg'). This means I need an extra step to remove the file after this is all complete. Ideally I don't save the file locally and just return the image to the user. Does anyone know how to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
You can shift some of the functions that don't necessarily need to be run before returning a response using background tasks. |
Beta Was this translation helpful? Give feedback.
-
|
You are using matplotlib just to convert the array to image? Use Pillow to convert it to an image https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.fromarray Then use Pillow to Save the image to a BytesIO. https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save then return just a normal response, something like |
Beta Was this translation helpful? Give feedback.
You can shift some of the functions that don't necessarily need to be run before returning a response using background tasks.
A good candidate for that is "removing the stored image after returning it as a response".