Closed wljungbergh closed 6 months ago
I wonder if it would be better to allow for both raw tensors as well as actual images. Raw tensors are great if running on the same local connection (or even machine), but it could be horribly slow in other settings.
maybe something like this:
class ImageFormat(str, Enum):
raw = "raw"
png = "png"
jpg = "jpg"
@app.get("/image/")
async def get_image(format: ImageFormat = ImageFormat.jpg):
image_bytes = get_image_bytes(format) # Assuming get_image_bytes() method exists
if format in (ImageFormat.jpg, ImageFormat.png):
return Response(content=image_bytes, media_type=f"image/{format.value}")
else:
return PlainTextResponse(content=image_bytes, media_type="text/plain")
Alternatively, yours truly chatgpt suggested this, dunno if nice
if format == ImageFormat.raw:
return StreamingResponse(iter([image_bytes]), media_type="image/jpeg")
elif format == ImageFormat.png:
return StreamingResponse(iter([image_bytes]), media_type="image/png")
elif format == ImageFormat.jpg:
return StreamingResponse(iter([image_bytes]), media_type="image/jpeg")
I like it. I added options for raw, png, and jpeg/jpg. However, I didn't change to the streaming response for now.
Update the closed-loop server
async
definitions