sunbal7 commited on
Commit
a7d61f9
·
verified ·
1 Parent(s): 904f66a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # Set page config
8
+ st.set_page_config(page_title="Rice Disease Classifier", page_icon="🌾")
9
+
10
+ # Constants from your training
11
+ IMG_SIZE = (224, 224)
12
+ CLASS_NAMES = ['Bacterial_leaf_blight', 'Brown_spot', 'Healthy', 'Leaf_blast']
13
+
14
+ # Cache the model loading
15
+ @st.cache_resource
16
+ def load_model():
17
+ return tf.keras.models.load_model('rice_disease_model.keras')
18
+
19
+ # Load model
20
+ try:
21
+ model = load_model()
22
+ except Exception as e:
23
+ st.error(f"Error loading model: {str(e)}")
24
+ st.stop()
25
+
26
+ # Preprocessing function
27
+ def preprocess_image(image):
28
+ image = image.resize(IMG_SIZE)
29
+ img_array = tf.keras.utils.img_to_array(image)
30
+ img_array = tf.expand_dims(img_array, 0) # Create batch axis
31
+ img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)
32
+ return img_array
33
+
34
+ # Streamlit interface
35
+ st.title("Rice Disease Classifier 🌾")
36
+ st.write("Upload an image of a rice leaf for disease diagnosis")
37
+
38
+ uploaded_file = st.file_uploader("Choose an image...",
39
+ type=["jpg", "jpeg", "png", "webp"])
40
+
41
+ if uploaded_file is not None:
42
+ try:
43
+ # Read and display image
44
+ image = Image.open(uploaded_file).convert('RGB')
45
+ st.image(image, caption="Uploaded Image", use_container_width=True) # Fixed parameter here
46
+
47
+ # Preprocess and predict
48
+ with st.spinner('Analyzing...'):
49
+ processed_image = preprocess_image(image)
50
+ predictions = model.predict(processed_image)
51
+ predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
52
+ confidence = np.max(predictions[0]) * 100
53
+
54
+ # Display results
55
+ st.subheader("Results")
56
+ st.success(f"Predicted Disease: **{predicted_class}**")
57
+ st.info(f"Confidence: **{confidence:.2f}%**")
58
+
59
+ # Show probability distribution
60
+ st.subheader("Class Probabilities")
61
+ for class_name, prob in zip(CLASS_NAMES, predictions[0]):
62
+ st.progress(float(prob), text=f"{class_name}: {prob*100:.2f}%")
63
+
64
+ except Exception as e:
65
+ st.error(f"Error processing image: {str(e)}")