-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_operations.py
63 lines (48 loc) · 2.48 KB
/
model_operations.py
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
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image as keras_image
from tensorflow.keras.models import load_model
import json
# import sys
# sys.path.append('S:\MY_PROJECTS\Leaf Health Diagnosis')
class Operations:
def __init__(self):
self.model = None
self.class_names = None
def select_model(self, plant_name):
with open('Resources/model_mapping.json', 'r') as file:
self.model_mapping = json.load(file)
if plant_name not in self.model_mapping:
error_msg = "Invalid plant name. Choose among Potato, Pepper, or Tomato."
raise ValueError(error_msg)
model_info = self.model_mapping[plant_name]
self.model = load_model(model_info["model_path"])
self.class_names = model_info["class_names"]
return self.model, self.class_names
def _treatment_for_disease(self, plant_name, predicted_class):
with open('Resources/treatments.json', 'r') as file:
treatments = json.load(file)
return treatments.get(plant_name, {}).get(predicted_class, "Treatment not found for this disease.")
def make_prediction(self, plant_name, model, class_names, img):
try:
# Preprocess the image
target_size = (256, 256) # Replace with your desired target size
img = keras_image.array_to_img(img)
resized_img = img.resize(target_size)
resized_img_array = keras_image.img_to_array(resized_img)
resized_img_array = np.expand_dims(resized_img_array, axis=0) # Add batch dimension
preprocessed_img = resized_img_array / 255.0 # Normalize pixel values
# Make prediction
predictions = model.predict(preprocessed_img)
# Get predicted class and confidence
predicted_class = class_names[np.argmax(predictions[0])]
confidence = round(100 * np.max(predictions[0]), 2)
treatment = self._treatment_for_disease(plant_name,predicted_class)
return {
'class': predicted_class,
'confidence': float(confidence),
'treatment': treatment
}
except Exception as e:
error_msg = f"Error making prediction: {str(e)}"
return {"error": error_msg}