File size: 1,495 Bytes
30c5114
 
 
 
 
32b6dbd
30c5114
 
 
 
 
 
 
 
 
 
 
32b6dbd
 
30c5114
 
 
 
 
 
 
 
 
 
ed89f9e
30c5114
 
ed89f9e
30c5114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
from torchvision.transforms import functional as TF
from torchvision.transforms import transforms
from PIL import Image
import cv2
import logging



def to_hsv(image):
    """Convert PIL image to HSV color space"""
    image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2HSV)
    return TF.to_pil_image(image)


# Define the preprocessing function for a single image
def preprocess_image(image):

    logging.info("Preprocessing image")
    # Load the image
    img = Image.open(image)

    # Ensure the image is in RGB format
    if isinstance(img, Image.Image):
        img = img.convert("RGB")
    elif isinstance(img, (np.ndarray, np.generic)):
        if img.shape[-1] == 1:  # Grayscale image
            img = np.stack([img, img, img], axis=-1)
        elif img.shape[-1] != 3:  # Not RGB or Grayscale
            logging.error("Input image must be in RGB or Grayscale format")
            raise ValueError("Input image must be in RGB or Grayscale format")
    else:
        logging.error("Unsupported image type")
        raise ValueError("Unsupported image type")

    # Define the transformations
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
    ])

    # Apply the transformations to the image
    preprocessed_img = transform(img)

    # Add batch dimension and reorder dimensions to [1, 224, 224, 3]
    preprocessed_img = preprocessed_img.unsqueeze(0).permute(0, 2, 3, 1)

    return preprocessed_img