Google Vision API (ImageAnnotatorClient). 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().
    But, Facing "The code terminated for an unknown reason. Potentially, the memory limit 256 Mibs was breached.".

  • Details: """

class GoogleService:
    @classmethod
    def ocr_image(cls, image_url):
        """
        Extract text from an image using Google Vision API
        """
        # Request image content
        response = requests.get(image_url, stream=True)
        response.raise_for_status()  # Raise exception for bad status codes
        
        # Initialize Vision client
        vision_client = vision.ImageAnnotatorClient(credentials=credentials)
        
        # Process image content in chunks
        with io.BytesIO() as image_buffer:
            for chunk in response.iter_content(chunk_size=8192):
                image_buffer.write(chunk)
            image_content = image_buffer.getvalue()

        # Process image
        image = vision.Image(content=image_content)
        response = vision_client.text_detection(image=image)
        
        # Handle response
        if response.error.message:
            raise Exception(f"Google Vision API Error: {response.error.message}")
        
        return response.text_annotations[0].description


lang = GoogleService.ocr_image(image_url=image_urls[0])

"""