I'm coding an API with FastAPI for this project TripoSR
My code at this moment is following (I only need this 2 functions):
from fastapi import FastAPIfrom gradio_app import preprocess, generate# initialise FastAPI instanceapp = FastAPI()@app.get("/tripo-api")def generator(input_image, do_remove_background: bool, foreground_ratio: float, mc_resolution, formats=["obj", "glb"]):""" Generator function, which calls preprocessing and generating function Parameters: - input_image, data type: PIL.Image.Image - do_remove_background, data type: boolean - foreground_ratio, data type: float""" # call preprocessing function with given parameters output_prepr = preprocess(input_image=input_image, do_remove_background=do_remove_background, foreground_ratio=foreground_ratio) # call generate function, based on prepocessing rv = generate(output_prepr, mc_resolution=mc_resolution, formats=formats) return {rv}
The error I'm getting when I try to call this api is following:
Traceback (most recent call last): File "C:\Users\user\TripoSR\api_test.py", line 30, in <module> response = requests.post(url, json=payload) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\site-packages\requests\api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\site-packages\requests\sessions.py", line 575, in request prep = self.prepare_request(req) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\site-packages\requests\sessions.py", line 486, in prepare_request p.prepare( File "C:\Python\3.11\Lib\site-packages\requests\models.py", line 371, in prepare self.prepare_body(data, files, json) File "C:\Python\3.11\Lib\site-packages\requests\models.py", line 511, in prepare_body body = complexjson.dumps(json, allow_nan=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\json\__init__.py", line 238, in dumps **kw).encode(obj) ^^^^^^^^^^^ File "C:\Python\3.11\Lib\json\encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\json\encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ File "C:\Python\3.11\Lib\json\encoder.py", line 180, in default raise TypeError(f'Object of type {o.__class__.__name__} 'TypeError: Object of type bytes is not JSON serializable
I'm calling the api with following python code:
import requestsfrom PIL import Imageimport io# API endpointurl = "http://localhost:8000/tripo-api"# Example input parametersinput_image = Image.open("examples\captured.jpeg")do_remove_background = Trueforeground_ratio = 0.8mc_resolution = 512formats = ["obj", "glb"]# Convert the input image to bytesimg_bytes = io.BytesIO()input_image.save(img_bytes, format='JPEG')img_bytes = img_bytes.getvalue()# Prepare the request payloadpayload = {"input_image": img_bytes,"do_remove_background": do_remove_background,"foreground_ratio": foreground_ratio,"mc_resolution": mc_resolution,"formats": formats}# Send the POST request to the APIresponse = requests.post(url, json=payload)# Check the responseif response.status_code == 200: print("API request successful!") print(response.json())else: print("API request failed with status code:", response.status_code) print("Error message:", response.text)
The idea behind this is, to build an own frontend around this project. For this I need an API, so that's the reason why I write this. If anyone know help, would be very helpful!