Dealing with long response times when processing many images using an AI API

Hey everyone! I’m having trouble with an AI API I’m using to remove backgrounds from video frames. It works fine for a few images but slows down a lot when I try to process more than 300 at once. Each image takes about 40-60 seconds to process which is way too slow for my needs.

I’m using async Python to handle the requests. Here’s a snippet of my code:

import aiohttp
import asyncio

async def get_processed_image(session, url):
    async with session.get(url) as response:
        return await response.read()

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [get_processed_image(session, url) for url in image_urls]
        results = await asyncio.gather(*tasks)

I’ve tried limiting concurrency and adding retries but it’s still super slow. Any ideas on how to speed this up? Should I add delays between requests or use some kind of backoff strategy? I’m not sure if there are rate limits I’m hitting or if it’s just the API being overwhelmed.

Any tips or tricks would be really helpful. I’m stuck and can’t move forward with my project until I figure this out. Thanks!

Have you considered using a local image processing library instead of relying solely on the API? Libraries like OpenCV or Pillow can handle background removal quite efficiently, especially for simpler cases. This approach could significantly reduce your processing time and API dependency.

For more complex images where the API is necessary, implementing a hybrid solution might work well. Process what you can locally, then send only the challenging frames to the API. This would decrease your API calls and potentially speed up the overall process.

If sticking with the API is a must, try implementing a queue system. Process a set number of images concurrently, then as each finishes, add the next to the queue. This can help manage resources and prevent overwhelming the API server.

I’ve seen similar slowdowns when working with image processing APIs. In my experience, the issue is often related to the provider’s capacity rather than a flaw in your code. One approach that helped me was to process images in smaller batches, which eased the load on the API. I also experimented with distributing the work across several machines, which allowed for better concurrency management. Moreover, reducing the size of the images and adding some caching mechanisms decreased unnecessary repeat calls. It might also help to check with the API support about any hidden rate limits or alternative optimization options.

yo, have u tried parallel processing? like using multiprocessing or threading in python? that could speed things up. also, maybe look into caching results or using a CDN to distribute load. sometimes API providers have bulk endpoints too - worth checkin out. good luck man!