File size: 1,265 Bytes
c440368
 
 
 
 
56bd5bc
c440368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbb6f4
c440368
adbb6f4
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
from flask import Flask, request, jsonify
from flask_cors import CORS
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np
import os 

app = Flask(__name__)
CORS(app)

MODEL_PATH = './model/best_model.h5'  
model = load_model(MODEL_PATH)

@app.route('/', methods=['POST'])
def predict():
    
    imagefile = request.files['imagefile']

    image_path = "./images/" + imagefile.filename
    imagefile.save(image_path)

    # Preprocessing
    image = load_img(image_path, target_size=(224, 224))  
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = image / 255.0  

    # Prediction
    predictions = model.predict(image)
    predicted_class = np.argmax(predictions[0])  
    probability = float(np.max(predictions[0])) * 100  

    class_labels = ['Bacterial_Spot','Early_Blight', 'Late_Blight', 'Leaf_Mold', 'Septoria_Leaf_Spot', 'Spider_Mites', 'Target_Spot', 'Tomato_Yellow_Leaf_Curl_Virus', 'Tomato_Mosaic_Virus', 'Healthy', 'Powdery_Mildew'] #akan disesuaikan dengan label di model 
    label = class_labels[predicted_class]

    result = {
        'label': label,
        'probability': probability
    }

    os.remove(image_path)

    return jsonify(result)