The ef
function preprocesses an input image by converting it to grayscale and normalizing pixel values to the range [0, 1]. It returns a preprocessed image feature with shape (1, 48, 48, 1).
def ef(image):
img = load_img(image,grayscale = True )
feature = np.array(img)
feature = feature.reshape(1,48,48,1)
return feature/255.0
import numpy as np
from PIL import Image
def preprocess_image(image):
"""
Preprocess an image by converting it to grayscale and normalizing the pixel values.
Parameters:
image (str): Path to the image file
Returns:
np.ndarray: Preprocessed image data
"""
# Load the image in grayscale mode
img = Image.open(image).convert('L')
# Convert the image to a numpy array
feature = np.array(img)
# Reshape the array to match the expected input shape for the neural network
feature = feature.reshape(1, 48, 48, 1)
# Normalize the pixel values to the range [0, 1]
return feature / 255.0
Preprocesses an image by converting it to grayscale and normalizing pixel values.
image
: The input image to be preprocessed.load_img
function, converting it to grayscale.