Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
-
|
|
|
5 |
|
6 |
def predict_disease(image_file):
|
7 |
-
"""Predicts the disease of a plant from an image.
|
8 |
|
9 |
Args:
|
10 |
image_file: The uploaded image file.
|
@@ -12,24 +13,27 @@ def predict_disease(image_file):
|
|
12 |
Returns:
|
13 |
A string representing the predicted disease.
|
14 |
"""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
def main():
|
35 |
"""Creates the Streamlit app."""
|
@@ -50,7 +54,10 @@ def main():
|
|
50 |
disease = predict_disease(image_file)
|
51 |
|
52 |
# Display the prediction
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from transformers import AutoFeatureExtractor, TFAutoModelForImageClassification
|
6 |
|
7 |
def predict_disease(image_file):
|
8 |
+
"""Predicts the disease of a plant from an image using TensorFlow.
|
9 |
|
10 |
Args:
|
11 |
image_file: The uploaded image file.
|
|
|
13 |
Returns:
|
14 |
A string representing the predicted disease.
|
15 |
"""
|
16 |
+
try:
|
17 |
+
# Load the model and feature extractor
|
18 |
+
model_name = "ombhojane/healthyPlantsModel"
|
19 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
20 |
+
model = TFAutoModelForImageClassification.from_pretrained(model_name)
|
21 |
+
|
22 |
+
# Process the image
|
23 |
+
image = Image.open(image_file).convert("RGB")
|
24 |
+
inputs = feature_extractor(images=image, return_tensors="tf")
|
25 |
+
|
26 |
+
# Make prediction
|
27 |
+
outputs = model(**inputs)
|
28 |
+
logits = outputs.logits
|
29 |
+
predicted_class_idx = tf.argmax(logits, axis=-1).numpy()[0]
|
30 |
+
|
31 |
+
# Get the label
|
32 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
33 |
+
|
34 |
+
return predicted_label
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error: {str(e)}"
|
37 |
|
38 |
def main():
|
39 |
"""Creates the Streamlit app."""
|
|
|
54 |
disease = predict_disease(image_file)
|
55 |
|
56 |
# Display the prediction
|
57 |
+
if disease.startswith("Error"):
|
58 |
+
st.error(disease)
|
59 |
+
else:
|
60 |
+
st.success(f"Predicted disease: {disease}")
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
main()
|