shaheer-data commited on
Commit
730cb6b
·
verified ·
1 Parent(s): 19d3a8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
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 make predictions
25
- def predict_image(image):
26
- image = image.resize((224, 224)) # Resize to match model input dimensions
27
- image_array = tf.keras.preprocessing.image.img_to_array(image)
28
- image_array = tf.expand_dims(image_array, axis=0) # Expand dimensions for batch prediction
29
- predictions = loaded_model.predict(image_array)
30
- return predictions
31
-
32
- # Class labels for Yellow Rust severity levels
33
- CLASS_LABELS = [
34
- "Healthy",
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
- # Display progress bar
50
- with st.spinner("Making predictions..."):
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
- else:
56
- st.write("Please upload an image file to make predictions.")
 
 
 
 
 
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]}")