-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (41 loc) 路 1.46 KB
/
Copy pathapp.py
File metadata and controls
49 lines (41 loc) 路 1.46 KB
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
import streamlit as st
from tensorflow import keras
from PIL import Image, ImageOps
import numpy as np
import os
PATH = os.path.dirname(__file__)
# Function to load the model
@st.cache_resource
def load_keras_model():
model = keras.models.load_model(PATH+"/keras_model.h5", compile=False)
return model
# Load the labels
@st.cache_data
def load_labels():
class_names = open(PATH+"/labels.txt", "r").readlines()
return class_names
model = load_keras_model()
class_names = load_labels()
st.write("""
# Image Classification
"""
)
file = st.file_uploader("Please upload an image", type=["jpg", "png"])
def import_and_predict(image_data, model):
size = (224, 224)
image = ImageOps.fit(image_data, size, Image.Resampling.LANCZOS)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
data[0] = normalized_image_array
prediction = model.predict(data)
return prediction
if file is not None:
image = Image.open(file).convert("RGB")
st.image(image, use_column_width=True)
predictions = import_and_predict(image, model)
index = np.argmax(predictions)
class_name = class_names[index]
confidence_score = predictions[0][index]
st.success(f"Class: {class_name[2:].strip()}")
st.success(f"Confidence Score: {confidence_score*100:.2f}%")