Spaces:
Sleeping
Sleeping
shaheer-data
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -21,36 +21,29 @@ model_path = hf_hub_download(repo_id="shaheer-data/Yellow-Rust-Prediction", file
|
|
21 |
|
22 |
loaded_model = load_model(model_path) # Load model using tf.keras directly
|
23 |
|
24 |
-
# Function to
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
"Mild Severity",
|
36 |
-
"Moderate Severity",
|
37 |
-
"Severe Severity",
|
38 |
-
"Very Severe",
|
39 |
-
"Extreme Severity"
|
40 |
-
]
|
41 |
-
|
42 |
-
# Image upload widget
|
43 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
44 |
|
45 |
if uploaded_file is not None:
|
|
|
46 |
image = Image.open(uploaded_file)
|
47 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
predictions = predict_image(image)
|
52 |
-
predicted_class = predictions.argmax(axis=-1)
|
53 |
-
st.write(f"Predicted Severity Level: {CLASS_LABELS[predicted_class[0]]} with confidence {predictions[0][predicted_class[0]]:.2f}")
|
54 |
|
55 |
-
|
56 |
-
st.
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
loaded_model = load_model(model_path) # Load model using tf.keras directly
|
23 |
|
24 |
+
# Function to preprocess the uploaded image
|
25 |
+
def preprocess_image(image):
|
26 |
+
# Resize the image to match the model input size (e.g., 224x224 for many pre-trained models)
|
27 |
+
image = image.resize((224, 224)) # Adjust size based on your model input
|
28 |
+
image = img_to_array(image) # Convert image to numpy array
|
29 |
+
image = image / 255.0 # Normalize pixel values to [0, 1]
|
30 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
31 |
+
return image
|
32 |
+
|
33 |
+
# Streamlit file uploader
|
34 |
+
uploaded_file = st.file_uploader("Upload a wheat leaf image", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if uploaded_file is not None:
|
37 |
+
# Display the uploaded image
|
38 |
image = Image.open(uploaded_file)
|
39 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
40 |
|
41 |
+
# Preprocess the image
|
42 |
+
processed_image = preprocess_image(image)
|
|
|
|
|
|
|
43 |
|
44 |
+
# Perform prediction
|
45 |
+
with st.spinner("Predicting..."):
|
46 |
+
prediction = loaded_model.predict(processed_image)
|
47 |
+
predicted_class = np.argmax(prediction, axis=1)[0] # Get the class index
|
48 |
+
class_labels = ['0', 'MR', 'MRMS', 'MS', 'R', 'S'] # Update based on your classes
|
49 |
+
st.success(f"Predicted Severity Class: {class_labels[predicted_class]}")
|