File size: 520 Bytes
09c8783
b4f755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09c8783
b4f755d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
import cv2

def preprocess_image(file):
    # Read bytes from UploadFile
    image_bytes = file.file.read()
    # Convert bytes to NumPy array
    nparr = np.frombuffer(image_bytes, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    if img is None:
        raise ValueError("Could not decode image.")

    img = cv2.resize(img, (256, 256))  # Changed size to 256x256
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img / 255.0
    img = np.expand_dims(img, axis=0)
    return img