File size: 1,122 Bytes
51e944e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from PIL import Image
from typing import Callable
import io
from api_backend.configs import logger
from api_backend.models import InvalidImageError

executor = ThreadPoolExecutor(max_workers=4)

async def async_predict(model, input_tensor):
    """Run model prediction in a separate thread."""
    loop = asyncio.get_event_loop()
    return await loop.run_in_executor(executor, model.predict, input_tensor)

def preprocess_image(
    image_bytes: bytes,
    target_size: tuple,
    preprocess_func: Callable[[np.ndarray], np.ndarray]
) -> np.ndarray:
    """Preprocess image bytes into model input tensor."""
    try:
        image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
        image = image.resize(target_size)
        image_array = np.array(image).astype("float32")
        image_array = preprocess_func(image_array)
        return np.expand_dims(image_array, axis=0)
    except Exception as e:
        logger.error(f"Image preprocessing failed: {str(e)}")
        raise InvalidImageError(f"Invalid image file: {str(e)}")