penscola commited on
Commit
19dbe8e
1 Parent(s): d9f6600

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py CHANGED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import cv2
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+
7
+ # Load the model
8
+ model = tf.keras.models.load_model('plant_model_v5-beta.h5')
9
+
10
+ # Define the class names
11
+ class_names = {
12
+ 0: 'Apple___Apple_scab',
13
+ 1: 'Apple___Black_rot',
14
+ 2: 'Apple___Cedar_apple_rust',
15
+ 3: 'Apple___healthy',
16
+ 4: 'Not a plant',
17
+ 5: 'Blueberry___healthy',
18
+ 6: 'Cherry___Powdery_mildew',
19
+ 7: 'Cherry___healthy',
20
+ 8: 'Corn___Cercospora_leaf_spot Gray_leaf_spot',
21
+ 9: 'Corn___Common_rust',
22
+ 10: 'Corn___Northern_Leaf_Blight',
23
+ 11: 'Corn___healthy',
24
+ 12: 'Grape___Black_rot',
25
+ 13: 'Grape___Esca_(Black_Measles)',
26
+ 14: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
27
+ 15: 'Grape___healthy',
28
+ 16: 'Orange___Haunglongbing_(Citrus_greening)',
29
+ 17: 'Peach___Bacterial_spot',
30
+ 18: 'Peach___healthy',
31
+ 19: 'Pepper,_bell___Bacterial_spot',
32
+ 20: 'Pepper,_bell___healthy',
33
+ 21: 'Potato___Early_blight',
34
+ 22: 'Potato___Late_blight',
35
+ 23: 'Potato___healthy',
36
+ 24: 'Raspberry___healthy',
37
+ 25: 'Soybean___healthy',
38
+ 26: 'Squash___Powdery_mildew',
39
+ 27: 'Strawberry___Leaf_scorch',
40
+ 28: 'Strawberry___healthy',
41
+ 29: 'Tomato___Bacterial_spot',
42
+ 30: 'Tomato___Early_blight',
43
+ 31: 'Tomato___Late_blight',
44
+ 32: 'Tomato___Leaf_Mold',
45
+ 33: 'Tomato___Septoria_leaf_spot',
46
+ 34: 'Tomato___Spider_mites Two-spotted_spider_mite',
47
+ 35: 'Tomato___Target_Spot',
48
+ 36: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
49
+ 37: 'Tomato___Tomato_mosaic_virus',
50
+ 38: 'Tomato___healthy'
51
+ }
52
+
53
+ def edge_and_cut(img, threshold1, threshold2):
54
+ emb_img = img.copy()
55
+ edges = cv2.Canny(img, threshold1, threshold2)
56
+ edge_coors = []
57
+ for i in range(edges.shape[0]):
58
+ for j in range(edges.shape[1]):
59
+ if edges[i][j] != 0:
60
+ edge_coors.append((i, j))
61
+
62
+ if len(edge_coors) == 0:
63
+ return emb_img
64
+
65
+ row_min = edge_coors[np.argsort([coor[0] for coor in edge_coors])[0]][0]
66
+ row_max = edge_coors[np.argsort([coor[0] for coor in edge_coors])[-1]][0]
67
+ col_min = edge_coors[np.argsort([coor[1] for coor in edge_coors])[0]][1]
68
+ col_max = edge_coors[np.argsort([coor[1] for coor in edge_coors])[-1]][1]
69
+ new_img = img[row_min:row_max, col_min:col_max]
70
+
71
+ emb_color = np.array([255], dtype=np.uint8) # Grayscale version of [255, 0, 0]
72
+ emb_img[row_min-10:row_min+10, col_min:col_max] = emb_color
73
+ emb_img[row_max-10:row_max+10, col_min:col_max] = emb_color
74
+ emb_img[row_min:row_max, col_min-10:col_min+10] = emb_color
75
+ emb_img[row_min:row_max, col_max-10:col_max+10] = emb_color
76
+
77
+ return emb_img
78
+
79
+
80
+
81
+ def classify_and_visualize(image):
82
+ # Preprocess the image
83
+ img_array = tf.image.resize(image, [256, 256])
84
+ img_array = tf.expand_dims(img_array, 0) / 255.0
85
+
86
+ # Make a prediction
87
+ prediction = model.predict(img_array)
88
+ predicted_class = tf.argmax(prediction[0], axis=-1)
89
+ confidence = np.max(prediction[0])
90
+
91
+ if confidence < 0.60:
92
+ class_name = "The image you uploaded might not be in the dataset. Try making your leaf background white."
93
+ bounded_image = image
94
+ else:
95
+ class_name = class_names[predicted_class.numpy()]
96
+ bounded_image = edge_and_cut(image, 200, 400)
97
+
98
+ return class_name, confidence, bounded_image
99
+
100
+
101
+ iface = gr.Interface(
102
+ fn=classify_and_visualize,
103
+ inputs="image",
104
+ outputs=["text", "number", "image"],
105
+ interpretation="default",
106
+ examples=[
107
+ ['examples/grot.jpg'],
108
+ ['examples/corn.jpg'],
109
+ ])
110
+ iface.launch()