File size: 2,839 Bytes
26ed034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from os import listdir
import sys
from pathlib import Path

import numpy as np
sys.path.append(str(Path(__file__).resolve().parent.parent))

from typing import Union
import pandas as pd
import tensorflow as tf

from plant_leave_diseases_model import __version__ as _version
from plant_leave_diseases_model.config.core import TRAINED_MODEL_DIR, config
from plant_leave_diseases_model.processing.data_manager import convert_image_to_array, get_class_file_list, get_model_file_name_path, load_leaf_disease_dataset, load_model, load_test_dataset
from plant_leave_diseases_model.processing.data_setup import test_directory,class_file_path


def make_prediction(*, test_dir_img_file_path) -> dict:
    """Make a prediction using a saved model """
    image_list=[]
    
    #x_test, y_test = load_leaf_disease_dataset(test_dir_img_file_path)
    root_dir = listdir(test_dir_img_file_path)
    for plant_image in root_dir :
        print("plant_image::",plant_image)
        img_file_path=str(test_dir_img_file_path)+"/"+str(plant_image)
        if plant_image.endswith(".jpg") == True or plant_image.endswith(".JPG") == True:
            image_list.append(convert_image_to_array(img_file_path))
            
    np_image_list = np.array(image_list, dtype=np.float16) / config.model_config.scaling_factor
           
    print("np_image_list.shape:",np_image_list.shape)
    results = {"predictions": None, "version": _version}
    
    load_model_and_predict(np_image_list) 
    pred_labels = model_file_name = get_model_file_name_path()
        
    results = {"predictions": pred_labels, "version": _version}
    

    return results

def load_model_and_predict(x_test):
    model_file_name = get_model_file_name_path()
    
    print("loading mode file:",model_file_name)
    
    model = load_model(file_name = model_file_name)
    
    predictions = model.predict(x_test,verbose = 0)
    
    ###########################################
    # Geting array of master class array
    ###########################################
    master_class_arr=get_class_file_list(class_file_path)
    print("master_class_arr:",master_class_arr)
    
    print(predictions)
    pred_labels = []
    for pred in predictions:
        np.multiply(np.array(pred), 100000)
        max_index = np.multiply(np.array(pred), 100000).argmax()
        leaf_pred_label=master_class_arr[max_index]
        print("load_model_and_predict:max_index::",max_index,"master_class_arr:",master_class_arr[max_index])
        pred_labels.append(leaf_pred_label)
        
    return pred_labels    
    
if __name__ == "__main__":

    # Define directory where test images are loaded
    test_dir_img_file_path=test_directory+"/Apple___Apple_scab"
    print("test_dir_img_file_path::",test_dir_img_file_path) 
    make_prediction(test_dir_img_file_path = test_dir_img_file_path)