File size: 783 Bytes
c80d070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import tensorflow as tf

def preprocess_image(img):
    """Preprocess a single image for prediction."""
    img = tf.image.decode_jpeg(img, channels=1)
    img= tf.image.resize(img, (224, 224))
    img_flattened = tf.reshape(img, (-1,))

    # Convert to 2D array (expected input format for the model)
    img_flattened = np.expand_dims(img_flattened, axis=0)  # Shape: (1, features)

    return img_flattened


def predict_single_image(model, image):
    """Predict the label of a single image."""
    # Preprocess the image
    processed_image = preprocess_image(image)

    # Make prediction
    prediction = model.predict(processed_image)

    return prediction[0]  # Return the predicted label


# Test the single image