Spaces:
Sleeping
Sleeping
Update app.py
#2
by
amirkhanbloch
- opened
app.py
CHANGED
@@ -6,49 +6,64 @@ from tensorflow.keras.models import load_model
|
|
6 |
from tensorflow.keras.preprocessing.image import img_to_array
|
7 |
from PIL import Image
|
8 |
|
9 |
-
#
|
10 |
model = load_model('plant_diseases.h5')
|
11 |
|
12 |
-
#
|
13 |
-
class_labels = [
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def preprocess_image(image, image_size=(224, 224)):
|
17 |
-
#
|
18 |
image = np.array(image.convert('L'))
|
19 |
-
#
|
20 |
image = cv2.resize(image, image_size)
|
21 |
-
|
22 |
-
# Redimensionner pour le modèle
|
23 |
image = img_to_array(image)
|
24 |
image /= 255.0
|
25 |
image = np.expand_dims(image, axis=0)
|
26 |
return image
|
27 |
|
|
|
28 |
st.title("Classification des Maladies des Plantes")
|
29 |
st.write("Téléchargez une image de plante pour la classification")
|
30 |
|
31 |
uploaded_file = st.file_uploader("Choisissez une image...", type=["jpg", "jpeg", "png"])
|
32 |
|
33 |
if uploaded_file is not None:
|
34 |
-
#
|
35 |
image = Image.open(uploaded_file)
|
36 |
st.image(image, caption='Image téléchargée', use_column_width=True)
|
37 |
-
|
38 |
st.write("Classification en cours...")
|
39 |
-
|
40 |
-
#
|
41 |
processed_image = preprocess_image(image)
|
42 |
-
|
43 |
-
#
|
44 |
predictions = model.predict(processed_image)
|
45 |
probabilities = predictions[0]
|
46 |
-
|
47 |
-
#
|
48 |
for i, label in enumerate(class_labels):
|
49 |
if probabilities[i] > 0:
|
50 |
st.write(f"{label}: {probabilities[i]:.2f}")
|
51 |
|
52 |
-
#
|
53 |
predicted_class = class_labels[np.argmax(probabilities)]
|
54 |
st.write(f"Classe prédite: {predicted_class}")
|
|
|
6 |
from tensorflow.keras.preprocessing.image import img_to_array
|
7 |
from PIL import Image
|
8 |
|
9 |
+
# Load the pre-trained model
|
10 |
model = load_model('plant_diseases.h5')
|
11 |
|
12 |
+
# Class labels (replace with your own classes)
|
13 |
+
class_labels = [
|
14 |
+
'Piment: Bacterial_spot',
|
15 |
+
'Piment: healthy',
|
16 |
+
'Pomme de terre: Early_blight',
|
17 |
+
'Pomme de terre: Late_blight',
|
18 |
+
'Pomme de terre: Healthy',
|
19 |
+
'Tomate: Bacterial Spot',
|
20 |
+
'Tomate: Early Blight',
|
21 |
+
'Tomate: Late Blight',
|
22 |
+
'Tomate: Leaf mold',
|
23 |
+
'Tomate: Septoria leaf spot',
|
24 |
+
'Tomate: Spider mites',
|
25 |
+
'Tomate: Spot',
|
26 |
+
'Tomate: Yellow Leaf Curl',
|
27 |
+
'Tomate: Virus Mosaïque',
|
28 |
+
'Tomate: Healthy'
|
29 |
+
]
|
30 |
|
31 |
def preprocess_image(image, image_size=(224, 224)):
|
32 |
+
# Convert image to grayscale
|
33 |
image = np.array(image.convert('L'))
|
34 |
+
# Resize image
|
35 |
image = cv2.resize(image, image_size)
|
36 |
+
# Prepare image for the model
|
|
|
37 |
image = img_to_array(image)
|
38 |
image /= 255.0
|
39 |
image = np.expand_dims(image, axis=0)
|
40 |
return image
|
41 |
|
42 |
+
# Streamlit app setup
|
43 |
st.title("Classification des Maladies des Plantes")
|
44 |
st.write("Téléchargez une image de plante pour la classification")
|
45 |
|
46 |
uploaded_file = st.file_uploader("Choisissez une image...", type=["jpg", "jpeg", "png"])
|
47 |
|
48 |
if uploaded_file is not None:
|
49 |
+
# Display the uploaded image
|
50 |
image = Image.open(uploaded_file)
|
51 |
st.image(image, caption='Image téléchargée', use_column_width=True)
|
52 |
+
|
53 |
st.write("Classification en cours...")
|
54 |
+
|
55 |
+
# Preprocess the image
|
56 |
processed_image = preprocess_image(image)
|
57 |
+
|
58 |
+
# Make predictions
|
59 |
predictions = model.predict(processed_image)
|
60 |
probabilities = predictions[0]
|
61 |
+
|
62 |
+
# Display probabilities for each class
|
63 |
for i, label in enumerate(class_labels):
|
64 |
if probabilities[i] > 0:
|
65 |
st.write(f"{label}: {probabilities[i]:.2f}")
|
66 |
|
67 |
+
# Show predicted class
|
68 |
predicted_class = class_labels[np.argmax(probabilities)]
|
69 |
st.write(f"Classe prédite: {predicted_class}")
|