Spaces:
Sleeping
Sleeping
File size: 1,336 Bytes
ea1f850 |
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 |
import base64
import streamlit as st
from PIL import ImageOps, Image
import numpy as np
def classify(image, model, class_names):
"""
This function takes an image, a model, and a list of class names and returns the predicted class and confidence
score of the image.
Parameters:
image (PIL.Image.Image): An image to be classified.
model (tensorflow.keras.Model): A trained machine learning model for image classification.
class_names (list): A list of class names corresponding to the classes that the model can predict.
Returns:
A tuple of the predicted class name and the confidence score for that prediction.
"""
# convert image to (224, 224)
image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
# convert image to numpy array
image_array = np.asarray(image)
# normalize image
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
# set model input
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
data[0] = normalized_image_array
# make prediction
prediction = model.predict(data)
# index = np.argmax(prediction)
index = 0 if prediction[0][0] > 0.95 else 1
class_name = class_names[index]
confidence_score = prediction[0][index]
return class_name, confidence_score |