File size: 5,654 Bytes
ff04594 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# pip install tensorflow
# python -m pip install --no-cache-dir tensorflow
# pip install Flask
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.enable_eager_execution()
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
image_size = (224, 224)
# /cropmodel/?modelname=tomatomodel
# /cropmodel/?modelname=sugarcanemodel
# /cropmodel/?modelname=cottonmodel
Model_related_data = {
"tomatomodel" : {
"dataset" : {
"total_images" : 14678,
"total_classes" : 10,
"classes_names" : [
"Late_blight",
"healthy",
"Early_blight",
"Septoria_leaf_spot",
"Tomato_Yellow_Leaf_Curl_Virus",
"Bacterial_spot",
"Target_Spot",
"Tomato_mosaic_virus",
"Leaf_Mold",
"Spider_mites Two-spotted_spider_mite",
],
},
"training" : {
"epochs" : 7,
"batch_size" : 32,
"img_frame_size" : (224,224),
"CNN_layers" : 5,
"CNN_layer_name" : 'relu',
"accuracy" : "89.23%",
},
"reference" : "https://www.kaggle.com/code/mrappplg/tomato-disease-detection"
},
"sugarcanemodel" : {
"dataset" : {
"total_images" : 240,
"total_classes" : 3,
"classes_names" : [ "Bacterial Blight", "Healthy", "Red Rot" ],
},
"training" : {
"epochs" : 10,
"batch_size" : 32,
"img_frame_size" : (224,224),
"CNN_layers" : 5,
"CNN_layer_name" : 'relu',
"accuracy" : "94.56%",
},
"reference" : "https://www.kaggle.com/code/handmadeprojects/sugarcane-disease-detection"
},
"cottonmodel" : {
"dataset" : {
"total_images" : 1951,
"total_classes" : 4,
"classes_names" : ["Diseased Cotton Leaf", "Diseased Cotton Plant", "Fresh Cotton Leaf", "Fresh Cotton Plant"],
},
"training" : {
"epochs" : 10,
"batch_size" : 32,
"img_frame_size" : (224,224),
"CNN_layers" : 4,
"CNN_layer_name" : 'relu',
"accuracy" : "83.13%",
},
"reference" : "https://www.kaggle.com/code/handmadeprojects/cotton-disease-detection"
},
}
def get_model_details():
return Model_related_data
def tomatomodel(img_path):
__classNames = [
"Bacterial_spot",
"Early_blight",
"Late_blight",
"Leaf_Mold",
"Septoria_leaf_spot",
"Spider_mites Two-spotted_spider_mite",
"Target_Spot",
"Tomato_Yellow_Leaf_Curl_Virus",
"Tomato_mosaic_virus",
"Healthy",
]
# Load the saved model
saved_model = load_model('model_folder/tomato_disease_model.h5')
# Example inference with a test image
# img_path = 'uysdeh'
img = image.load_img(img_path, target_size=image_size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
result = saved_model.predict(img_array)
# Get the predicted class
classIndex = np.argmax(result)
predicted_class = __classNames[classIndex]
print(f"tomatomodel __ Predicted class - {classIndex} : {predicted_class}")
# Free memory by deleting variables
del __classNames
del saved_model
del img
del img_array
del result
return predicted_class
def sugarcanemodel(img_path):
__classNames = [ "Bacterial Blight", "Healthy", "Red Rot" ]
# Load the saved model
saved_model = load_model("model_folder/sugarcane_disease_model.h5")
# img_path = "test_img_folder/Red Rot/S_RR (12).JPG"
img = image.load_img(img_path, target_size=image_size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0
result = saved_model.predict(img_array)
# Get the predicted class
classIndex = np.argmax(result)
predicted_class = __classNames[classIndex]
print(f"sugarcanemodel __ Predicted class - {classIndex} : {predicted_class}")
# Free memory by deleting variables
del __classNames
del saved_model
del img
del img_array
del result
return predicted_class
def cottonmodel(img_path):
# __classNames = ["Diseased Cotton Leaf", "Diseased Cotton Plant", "Fresh Cotton Leaf", "Fresh Cotton Plant"]
__classNames = ['fresh cotton plant', 'diseased cotton plant', 'fresh cotton leaf', 'diseased cotton leaf']
# Load the saved model
saved_model = load_model('model_folder/cotton_disease_model.h5')
# img_path = '/kaggle/input/cotton-disease-dataset/Cotton Disease/train/fresh cotton plant/dsd (138)_iaip.jpg'
img = image.load_img(img_path, target_size=image_size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
result = saved_model.predict(img_array)
# Get the predicted class
classIndex = np.argmax(result)
predicted_class = __classNames[classIndex]
print(f"cottonmodel __ Predicted class - {classIndex} : {predicted_class}")
# Free memory by deleting variables
del __classNames
del saved_model
del img
del img_array
del result
return predicted_class
|