File size: 999 Bytes
246be56
 
 
 
6c87ba0
d5cb601
246be56
 
 
 
 
 
 
6c87ba0
246be56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import numpy as np
from PIL import Image

MODEL_PATH = "/app/src/best_model.h5"
IMG_SIZE = 224

model = tf.keras.models.load_model(MODEL_PATH)


def preprocess(image):

    image = image.resize((IMG_SIZE, IMG_SIZE))
    image = np.array(image)
    image = np.expand_dims(image, axis=0)

    return image


def load_model_and_predict(image):

    img = preprocess(image)

    pred = model.predict(img)

    class_index = np.argmax(pred)
    confidence = np.max(pred)

    # class mapping inside function (safe fallback)
    class_names = [
        'Tomato___Bacterial_spot',
        'Tomato___Early_blight',
        'Tomato___Late_blight',
        'Tomato___Leaf_Mold',
        'Tomato___Septoria_leaf_spot',
        'Tomato___Spider_mites Two-spotted_spider_mite',
        'Tomato___Target_Spot',
        'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
        'Tomato___Tomato_mosaic_virus',
        'Tomato___healthy'
    ]

    return class_names[class_index], float(confidence)