-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (51 loc) · 2.1 KB
/
app.py
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
67
68
69
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import io
import numpy as np
# Cache the model loading function
@st.cache_resource
# Load the saved model
def load_model():
MODEL_PATH = r"C:\Users\Maddy\Documents\Python\Data Science\Deep Learning\Potato_Disease\training\model_v1.keras"
model = tf.keras.models.load_model(MODEL_PATH)
return model
# Load the model
model = load_model()
# In[10]:
# Define class names
class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
IMAGE_SIZE = 256
def predict(model, img):
img = img.resize((IMAGE_SIZE, IMAGE_SIZE))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions[0])]
confidence = round(100 * (np.max(predictions[0])), 2)
return predicted_class, confidence
# Streamlit app
st.title("Potato Disease Classification")
# Option to choose between uploading an image or taking a picture
option = st.radio("Select input method:", ('Upload Image', 'Take Picture'))
if option == 'Upload Image':
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption='Uploaded Image', use_column_width=True)
# Make prediction
predicted_class, confidence = predict(model, image)
# Display results
st.write(f"Predicted class: {predicted_class}")
st.write(f"Confidence: {confidence:.2f}")
elif option == 'Take Picture':
picture = st.camera_input("Take a picture")
if picture is not None:
image = Image.open(picture).convert('RGB')
st.image(image, caption='Captured Image', use_column_width=True)
# Make prediction
predicted_class, confidence = predict(model, image)
# Display results
st.write(f"Predicted class: {predicted_class}")
st.write(f"Confidence: {confidence:.2f}")