Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

post multiple images to fastapi using requests python

i want to use python requests library to send multiple images to fastapi ?

API

from fastapi import FastAPI, UploadFile, File
from typing import List
app = FastAPI()

@app.post("/")
def index(file: List[UploadFile] = File(...)):
    list_image = []
    for i in file:
        list_image.append(i.filename)
    return {"list_image": list_image}

Request

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently, I can only request one image and I want to be able to request multiple images

import requests
import cv2

frame = cv2.imread("../image/3.png")
imencoded = cv2.imencode(".jpg", frame)[1]
file = {'file': ('image.jpg', imencoded.tostring(),
                    'image/jpeg', {'Expires': '0'})}

response = requests.post(
    "http://127.0.0.1:8000/", files=file)

>Solution :

Use as below:

import requests

url = 'http://127.0.0.1:8000/'
files = [('file', open('images/1.png', 'rb')), ('file', open('images/2.png', 'rb'))]
resp = requests.post(url=url, files=files) 

or, you could also use:

import requests
import glob

path = glob.glob("images/*", recursive=True) # images' path
files = []
for img in path:
    files.append(('file', open(img, 'rb')))
    
url = 'http://127.0.0.1:8000/'
resp = requests.post(url=url, files=files) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading