The code terminated for an unknown reason. Potentially, the memory limit 256 Mibs was breached

  • Goal: Pass Image URL to google vision to perform OCR.

  • Steps: 1. Tried to resize images from 2048x2048 to 128x128.
    2. Tried streaming of images using io.BytesIO().
    3. Converted Class based to function based code, in order to make memory efficient.
    But, Facing "The code terminated for an unknown reason. Potentially, the memory limit 256 Mibs was breached.".

  • Details:

def ocr_image(image_url):
    """
    Extract text from an image using Google Vision API
    """
    # Fetch the image from the URL
    response = requests.get(image_url)
    image_bytes = io.BytesIO(response.content)
    
    # Open and convert the image to ensure proper format
    image = Image.open(image_bytes).convert("RGB")
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    
    # Base64 encode without adding any headers or additional formatting
    base64_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
    
    # Create an Image object for Google Vision
    image_for_vision = vision.Image(content=base64_image)
    
    # Perform text detection
    response = vision_client.text_detection(image=image_for_vision)
    if response.error.message:
        raise Exception(f"Google Vision API Error: {response.error.message}")
    
    # Return the detected text (just the description of the first result)
    return response.text_annotations[0].description if response.text_annotations else "No text found."

Hi @retool_taleemabad :wave:

Based on the Python code I'm guessing your are building a Workflow, correct?

It's important to keep in mind that the Workflow Editor can only keep track of 258MB of data in memory (see the Workflow usage limits). Assuming that you're converting more than one images to bytes, you're memory usage is going to ramp up pretty quickly.

I don't know the Google Vision API, but I would recommend to use image URLs instead of byte arrays to save up on space in the Workflow.

Hope this helps!

1 Like