How to pass a base64 image to the Infer API endpoint

https://docs.roboflow.com/deploy/hosted-api/custom-models/object-detection
In the documentation for the API, it says to pass base64 images through the body of our POST request.

response = requests.post(roboflow_url, params=params, data=base64_image)

This is the request code and all it returns is error code 400. params is my API key. What do I do?

Hi @george_bibu , what is the value of roboflow_url?

Can you make sure that roboflow_url=f'https://detect.roboflow.com/{project_id}/{version_id}

Yes, that is the value of my roboflow_url.

Hello? Were you able to find anything out? I would really like to know if it’s possible to access the endpoint in this manner or not, because I really don’t want to waste time trying to do something that’s impossible.

Try

import base64
import os
from PIL import Image
import requests

API_KEY = os.environ["API_KEY"]
IMAGE_PATH = "image.jpg"
PROJECT_ID = ""
VERSION_ID = ""


def encode_bas64(image_path):
    with open(image_path, "rb") as image:
        x = image.read()
        image_string = base64.b64encode(x)

    return image_string.decode("ascii")


def do_request():
    api_key = API_KEY
    infer_payload = {
        "image": {
            "type": "base64",
            "value": encode_bas64(IMAGE_PATH),
        },
        "api_key": api_key,
    }
    roboflow_url = f"https://detect.roboflow.com/{PROJECT_ID}/{VERSION_ID}",
    response = requests.post(roboflow_url, json=infer_payload)
    print(response.json())

If that doesn’t work, try using the inference client:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.