Spaces:
Configuration error
Configuration error
File size: 1,138 Bytes
f1ba785 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | """Numeric crop preprocessing variants for multi-pass recognition."""
from io import BytesIO
def _to_gray_array(image_bytes: bytes):
import numpy as np
from PIL import Image
with Image.open(BytesIO(image_bytes)) as source:
image = source.convert("L")
width = max(64, round(image.width * 64 / max(image.height, 1)))
image = image.resize((width, 64))
return (255 - np.asarray(image, dtype=np.float32)) / 255
def default_tensor(image_bytes: bytes):
import numpy as np
array = _to_gray_array(image_bytes)
return array[None, None, :, :], "model_default"
def inverted_tensor(image_bytes: bytes):
import numpy as np
array = 1.0 - _to_gray_array(image_bytes)
return array[None, None, :, :], "polarity_inverted"
def contrast_tensor(image_bytes: bytes):
import numpy as np
array = _to_gray_array(image_bytes)
mean = float(array.mean())
stretched = np.clip((array - mean) * 1.35 + mean, 0.0, 1.0)
return stretched[None, None, :, :], "contrast_stretch"
VARIANTS = (default_tensor, inverted_tensor, contrast_tensor)
|