moazx commited on
Commit
eb0216c
·
1 Parent(s): fc9f47e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import gradio as gr
4
+ from tensorflow.keras.utils import img_to_array
5
+ from tensorflow.keras.models import load_model
6
+ import os
7
+ # Load your pre-trained model
8
+ model = load_model(r"plant_leaf_diseases_model.h5")
9
+
10
+ classes = ['Pepper_bell_Bacterial_spot',
11
+ 'Pepper_bell_healthy',
12
+ 'Potato_Early_blight',
13
+ 'Potato_Late_blight',
14
+ 'Potato_healthy',
15
+ 'Tomato_Bacterial_spot',
16
+ 'Tomato_Early_blight',
17
+ 'Tomato_Late_blight',
18
+ 'Tomato_Leaf_Mold',
19
+ 'Tomato_Septoria_leaf_spot',
20
+ 'Tomato_Spider_mites_Two_spotted_spider_mite',
21
+ 'Tomato_Target_Spot',
22
+ 'Tomato_Tomato_YellowLeaf_Curl_Virus',
23
+ 'Tomato_Tomato_mosaic_virus',
24
+ 'Tomato_healthy']
25
+
26
+
27
+ # Define the prediction function that takes an image as input and returns the predicted label
28
+ def predict_image(img):
29
+
30
+ x = img_to_array(img)
31
+ x = cv2.resize(x, (256, 256), interpolation=cv2.INTER_AREA)
32
+ x /= 255
33
+ x = np.expand_dims(x, axis=0)
34
+ image = np.vstack([x])
35
+ prediction = model.predict(image)[0] # Get the predictions for the first (and only) image
36
+ class_probabilities = {class_name: prob for class_name, prob in zip(classes, prediction)}
37
+ formatted_class_probabilities = {class_name: "{:.2f}".format(prob) for class_name, prob in class_probabilities.items()}
38
+ return formatted_class_probabilities
39
+
40
+ # Define the Gradio Interface with the desired title and description
41
+ description_html = """
42
+ <p>This model was trained by Moaz Eldsouky You can find more about me here:</p>
43
+ <p>GitHub: <a href="https://github.com/MoazEldsouky">GitHub Profile</a></p>
44
+ <p>LinkedIn: <a href="https://www.linkedin.com/in/moaz-eldesouky-762288251/">LinkedIn Profile</a></p>
45
+ <p>Kaggle: <a href="https://www.kaggle.com/moazeldsokyx">Kaggle Profile</a></p>
46
+ <p>This model was trained to classify plant diseases using the Plant Villages dataset.</p>
47
+ <p>You can see how this model was trained on the following Kaggle Notebook:</p>
48
+ <p><a href="https://www.kaggle.com/code/moazeldsokyx/plant-leaf-diseases-detection-using-cnn">Kaggle Notebook</a></p>
49
+ <p>Upload a photo to see how the model predicts!</p>
50
+ """
51
+
52
+ # Define example images and their true labels for users to choose from
53
+ example_data = [
54
+ r"0a0dbf1f-1131-496f-b337-169ec6693e6f___NREC_B.Spot 9241.JPG",
55
+ r"0e69c47d-72c6-4fc6-9437-910c95b183dc___JR_HL 8113.JPG",
56
+ r"0c4f6f72-c7a2-42e1-9671-41ab3bf37fe7___RS_Early.B 6752.JPG",
57
+ r"1f9870b3-899e-46fb-98c9-cfc2ce92895b___RS_HL 1816.JPG",
58
+ r"3f0fd699-1ce7-437e-a899-662a51d59974___RS_LB 2904.JPG",
59
+ r"01b2013e-4030-4cd0-843c-2dbacf5f3758___Com.G_TgS_FL 8398.JPG",
60
+ r"0ce66ec5-0bb7-4fde-9c61-750a1a150f75___UF.GRC_YLCV_Lab 02219.JPG",
61
+ r"0afe3bbd-b18b-4c70-8fbd-072844e742a2___GCREC_Bact.Sp 3434.JPG",
62
+ r"0ba3d536-8732-4ea1-b3e1-a1be86e5dc6a___RS_Erly.B 9499.JPG",
63
+ r"2e96661f-a313-4deb-bf32-1eacbc10c48d___GH_HL Leaf 424.JPG"
64
+
65
+ # Add more example images and labels as needed
66
+ ]
67
+
68
+
69
+ gr.Interface(
70
+ fn=predict_image,
71
+ inputs="image",
72
+ outputs=gr.Label(num_top_classes=15,min_width=360),
73
+ title="Plant Diseases Classification 🌱🦠",
74
+ description=description_html,
75
+ allow_flagging='never',
76
+ examples=example_data
77
+ ).launch()