Alternative to filename string for image

#12
by codysmith - opened

It would be nice if you could use a numpy array, or PIL Image, or something like that. Specifically, I would like to do some preprocessing on the image before and then pass it to the ocr function without saving it to a temporary file.

You can monkeypatch the load function of the model until there is a better solution. Not sure if that fit's your bill.

from PIL import Image

def better_load_image(image_file):
    if isinstance(image_file, Image.Image):
        return image_file.convert('RGB')
    elif isinstance(image_file, str) and image_file.startswith(('https', 'http')):
        response = requests.get(image_file)
        return Image.open(BytesIO(response.content)).convert('RGB')
    elif isinstance(image_file, (str, Path)):
        return Image.open(image_file).convert('RGB')
    raise TypeError(f"image_file arg needs to be of type str, Path or Pil.Image.Image")

self.model.load_image = better_load_image

Sign up or log in to comment