Spaces:
Sleeping
Sleeping
File size: 1,980 Bytes
13a7fc8 |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import base64
import streamlit as st
from PIL import ImageOps, Image
import numpy as np
def set_background(image_file):
"""
This function sets the background of a Streamlit app to an image specified by the given image file.
Parameters:
image_file (str): The path to the image file to be used as the background.
Returns:
None
"""
with open(image_file, "rb") as f:
img_data = f.read()
b64_encoded = base64.b64encode(img_data).decode()
style = f"""
<style>
.stApp {{
background-image: url(data:image/png;base64,{b64_encoded});
background-size: cover;
}}
</style>
"""
st.markdown(style, unsafe_allow_html=True)
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
|