etahamad commited on
Commit
d4f3029
1 Parent(s): 608aa57

Refactor app.py to load model and define class names before classifying image

Browse files
Files changed (1) hide show
  1. app.py +57 -9
app.py CHANGED
@@ -1,26 +1,74 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- import numpy
4
-
5
 
 
6
  model = tf.keras.models.load_model('plant_model_v5-beta.h5')
7
- class_names = {0: 'Apple___Apple_scab', 1: 'Apple___Black_rot', 2: 'Apple___Cedar_apple_rust', 3: 'Apple___healthy', 4: 'Not a plant', 5: 'Blueberry___healthy', 6: 'Cherry___Powdery_mildew', 7: 'Cherry___healthy', 8: 'Corn___Cercospora_leaf_spot Gray_leaf_spot', 9: 'Corn___Common_rust', 10: 'Corn___Northern_Leaf_Blight', 11: 'Corn___healthy', 12: 'Grape___Black_rot',
8
- 13: 'Grape___Esca_(Black_Measles)', 14: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 15: 'Grape___healthy', 16: 'Orange___Haunglongbing_(Citrus_greening)', 17: 'Peach___Bacterial_spot', 18: 'Peach___healthy', 19: 'Pepper,_bell___Bacterial_spot', 20: 'Pepper,_bell___healthy', 21: 'Potato___Early_blight', 22: 'Potato___Late_blight', 23: 'Potato___healthy', 24: 'Raspberry___healthy', 25: 'Soybean___healthy', 26: 'Squash___Powdery_mildew', 27: 'Strawberry___Leaf_scorch', 28: 'Strawberry___healthy', 29: 'Tomato___Bacterial_spot', 30: 'Tomato___Early_blight', 31: 'Tomato___Late_blight', 32: 'Tomato___Leaf_Mold', 33: 'Tomato___Septoria_leaf_spot', 34: 'Tomato___Spider_mites Two-spotted_spider_mite', 35: 'Tomato___Target_Spot', 36: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 37: 'Tomato___Tomato_mosaic_virus', 38: 'Tomato___healthy'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
 
11
  def classify_image(image):
12
  # Preprocess the image
13
- img = tf.keras.preprocessing.image.load_img(
14
- image.name, target_size=(256, 256))
15
- img_array = tf.keras.preprocessing.image.img_to_array(img)
16
  img_array = tf.expand_dims(img_array, 0) / 255.0
17
 
18
  # Make a prediction
19
  prediction = model.predict(img_array)
20
  predicted_class = tf.argmax(prediction[0], axis=-1)
 
21
 
22
- return class_names[predicted_class.numpy()]
23
 
24
 
25
- iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")
 
 
 
 
 
 
 
 
26
  iface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ import numpy as np
 
4
 
5
+ # Load the model
6
  model = tf.keras.models.load_model('plant_model_v5-beta.h5')
7
+
8
+ # Define the class names
9
+ class_names = {
10
+ 0: 'Apple___Apple_scab',
11
+ 1: 'Apple___Black_rot',
12
+ 2: 'Apple___Cedar_apple_rust',
13
+ 3: 'Apple___healthy',
14
+ 4: 'Not a plant',
15
+ 5: 'Blueberry___healthy',
16
+ 6: 'Cherry___Powdery_mildew',
17
+ 7: 'Cherry___healthy',
18
+ 8: 'Corn___Cercospora_leaf_spot Gray_leaf_spot',
19
+ 9: 'Corn___Common_rust',
20
+ 10: 'Corn___Northern_Leaf_Blight',
21
+ 11: 'Corn___healthy',
22
+ 12: 'Grape___Black_rot',
23
+ 13: 'Grape___Esca_(Black_Measles)',
24
+ 14: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
25
+ 15: 'Grape___healthy',
26
+ 16: 'Orange___Haunglongbing_(Citrus_greening)',
27
+ 17: 'Peach___Bacterial_spot',
28
+ 18: 'Peach___healthy',
29
+ 19: 'Pepper,_bell___Bacterial_spot',
30
+ 20: 'Pepper,_bell___healthy',
31
+ 21: 'Potato___Early_blight',
32
+ 22: 'Potato___Late_blight',
33
+ 23: 'Potato___healthy',
34
+ 24: 'Raspberry___healthy',
35
+ 25: 'Soybean___healthy',
36
+ 26: 'Squash___Powdery_mildew',
37
+ 27: 'Strawberry___Leaf_scorch',
38
+ 28: 'Strawberry___healthy',
39
+ 29: 'Tomato___Bacterial_spot',
40
+ 30: 'Tomato___Early_blight',
41
+ 31: 'Tomato___Late_blight',
42
+ 32: 'Tomato___Leaf_Mold',
43
+ 33: 'Tomato___Septoria_leaf_spot',
44
+ 34: 'Tomato___Spider_mites Two-spotted_spider_mite',
45
+ 35: 'Tomato___Target_Spot',
46
+ 36: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
47
+ 37: 'Tomato___Tomato_mosaic_virus',
48
+ 38: 'Tomato___healthy'
49
+ }
50
 
51
 
52
  def classify_image(image):
53
  # Preprocess the image
54
+ img_array = tf.image.resize(image, [256, 256])
 
 
55
  img_array = tf.expand_dims(img_array, 0) / 255.0
56
 
57
  # Make a prediction
58
  prediction = model.predict(img_array)
59
  predicted_class = tf.argmax(prediction[0], axis=-1)
60
+ confidence = np.max(prediction[0])
61
 
62
+ return class_names[predicted_class.numpy()], confidence
63
 
64
 
65
+ iface = gr.Interface(
66
+ fn=classify_image,
67
+ inputs="image",
68
+ outputs=["text", "number"],
69
+ interpretation="default",
70
+ examples=[
71
+ ['https://i.imgur.com/Ls6rCuQ.jpg'],
72
+ ['https://i.imgur.com/3Y68VBX.jpg'],
73
+ ])
74
  iface.launch()