Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,11 @@
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
-
import tensorflow as tf
|
4 |
-
|
5 |
-
import numpy as np
|
6 |
import cv2
|
7 |
-
import
|
8 |
-
import h5py
|
9 |
-
def analyse(img,plant_type):
|
10 |
-
import json
|
11 |
|
|
|
12 |
# Load label_disease.json
|
13 |
with open('data/label_disease.json', 'r') as f:
|
14 |
label_disease = json.load(f)
|
@@ -16,41 +13,60 @@ def analyse(img,plant_type):
|
|
16 |
# Load plant_label_disease.json
|
17 |
with open('data/plant_label_disease.json', 'r') as f:
|
18 |
plant_label_disease = json.load(f)
|
19 |
-
|
20 |
HEIGHT = 256
|
21 |
WIDTH = 256
|
22 |
-
modelArchitecturePath ='model/model_architecture.h5'
|
23 |
modelWeightsPath = 'model/model_weights.h5'
|
24 |
-
|
|
|
|
|
25 |
dnn_model.load_weights(modelWeightsPath)
|
26 |
-
|
27 |
-
|
28 |
-
process_img =
|
|
|
29 |
process_img = np.expand_dims(process_img, axis=0)
|
30 |
-
|
31 |
-
|
32 |
y_pred = dnn_model.predict(process_img)
|
33 |
y_pred = y_pred[0]
|
34 |
-
|
35 |
-
|
36 |
p_id = plant_label_disease[plant_type.lower()][0]
|
37 |
-
|
38 |
for disease in plant_label_disease[plant_type.lower()]:
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
overall_predicted_id = np.argmax(y_pred)
|
43 |
overall_predicted_name = label_disease[str(overall_predicted_id)]
|
44 |
overall_predicted_acc = y_pred[overall_predicted_id]
|
45 |
-
|
46 |
-
plant_predicted_id =p_id
|
47 |
plant_predicted_name = label_disease[str(plant_predicted_id)]
|
48 |
plant_predicted_acc = y_pred[plant_predicted_id]
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
import tensorflow as tf # version 2.13.0
|
4 |
+
from keras.models import load_model # version matching your TensorFlow
|
|
|
5 |
import cv2
|
6 |
+
import json
|
|
|
|
|
|
|
7 |
|
8 |
+
def analyse(img, plant_type):
|
9 |
# Load label_disease.json
|
10 |
with open('data/label_disease.json', 'r') as f:
|
11 |
label_disease = json.load(f)
|
|
|
13 |
# Load plant_label_disease.json
|
14 |
with open('data/plant_label_disease.json', 'r') as f:
|
15 |
plant_label_disease = json.load(f)
|
16 |
+
|
17 |
HEIGHT = 256
|
18 |
WIDTH = 256
|
19 |
+
modelArchitecturePath = 'model/model_architecture.h5'
|
20 |
modelWeightsPath = 'model/model_weights.h5'
|
21 |
+
|
22 |
+
# Load the model
|
23 |
+
dnn_model = load_model(modelArchitecturePath, compile=False)
|
24 |
dnn_model.load_weights(modelWeightsPath)
|
25 |
+
|
26 |
+
# Preprocess the image
|
27 |
+
process_img = cv2.resize(img, (HEIGHT, WIDTH), interpolation=cv2.INTER_LINEAR)
|
28 |
+
process_img = process_img / 255.0
|
29 |
process_img = np.expand_dims(process_img, axis=0)
|
30 |
+
|
31 |
+
# Predict using the model
|
32 |
y_pred = dnn_model.predict(process_img)
|
33 |
y_pred = y_pred[0]
|
34 |
+
|
35 |
+
# Identify predictions
|
36 |
p_id = plant_label_disease[plant_type.lower()][0]
|
|
|
37 |
for disease in plant_label_disease[plant_type.lower()]:
|
38 |
+
if y_pred[disease] > y_pred[p_id]:
|
39 |
+
p_id = disease
|
40 |
+
|
41 |
overall_predicted_id = np.argmax(y_pred)
|
42 |
overall_predicted_name = label_disease[str(overall_predicted_id)]
|
43 |
overall_predicted_acc = y_pred[overall_predicted_id]
|
44 |
+
|
45 |
+
plant_predicted_id = p_id
|
46 |
plant_predicted_name = label_disease[str(plant_predicted_id)]
|
47 |
plant_predicted_acc = y_pred[plant_predicted_id]
|
48 |
+
|
49 |
+
# Return results as a JSON object
|
50 |
+
result = {
|
51 |
+
"plant_predicted_id": int(plant_predicted_id),
|
52 |
+
"plant_predicted_name": plant_predicted_name,
|
53 |
+
"plant_predicted_accuracy": float(plant_predicted_acc),
|
54 |
+
"overall_predicted_id": int(overall_predicted_id),
|
55 |
+
"overall_predicted_name": overall_predicted_name,
|
56 |
+
"overall_predicted_accuracy": float(overall_predicted_acc),
|
57 |
+
}
|
58 |
+
|
59 |
+
return result
|
60 |
+
|
61 |
+
# Gradio interface
|
62 |
+
demo = gr.Interface(
|
63 |
+
fn=analyse,
|
64 |
+
inputs=[
|
65 |
+
gr.Image(type="numpy"),
|
66 |
+
gr.Radio(["Apple", "Blueberry", "Cherry", "Corn", "Grape", "Orange", "Peach",
|
67 |
+
"Pepper", "Potato", "Raspberry", "Soybean", "Squash", "Strawberry", "Tomato"])
|
68 |
+
],
|
69 |
+
outputs=gr.JSON()
|
70 |
+
)
|
71 |
+
|
72 |
+
demo.launch(share=True, show_error=True)
|