a b/classifier/getPrediction.py
1
import pickle
2
3
import cv2
4
import numpy as np
5
import tensorflow as tf
6
from tensorflow.keras.models import load_model
7
# Process image and predict label
8
from tensorflow.python.keras.preprocessing.image import img_to_array
9
10
from class_names import prediction_classs
11
12
IMG_SIZE = 256
13
LEARNING_RATE = 0.001
14
COLOUR_MAP = 3
15
classes = prediction_classs
16
17
18
# Evaluate the model
19
def evaluate_image(model_path, class_path, valididation_path, image_size):
20
    # Load the model
21
    model = tf.keras.models.load_model(model_path)
22
    # Load classes
23
    with open(class_path, 'rb') as file:
24
        classes = pickle.load(file)
25
    # Get a list of categories
26
27
    image = cv2.imread(valididation_path)
28
    # Get input reshaped and rescaled
29
    image = cv2.resize(image, (image_size, image_size))
30
    image_f = image.astype("float") / 255.0
31
    image_a = img_to_array(image_f)
32
    image_p = np.expand_dims(image_a, axis=0)
33
    # Get predictions
34
    predictions = model.predict(image_p).ravel()
35
    # Print predictions
36
    print(predictions)
37
    # Get the class with the highest probability
38
    prediction = np.argmax(predictions)
39
    # Check if the prediction is correct
40
    return classes[prediction]
41
42
43
def processImg(img_path):
44
    return evaluate_image(model_path=r'/Users/sikarwar07/PycharmProjects/MLPredictModelFlask/models'
45
                                     r'/NASNet_32B_331x331.h5',
46
                          class_path=r'/Users/sikarwar07/PycharmProjects/MLPredictModelFlask/models'
47
                                     r'/NASNet_32B_331x331.pkl',
48
                          valididation_path=img_path,
49
                          image_size=331)
50
51
# TODO Future implantation
52
# Create image prediction in tabular format
53
#     # Read image
54
#     model = load_model("models/resnet152_class_23_epoch_50.h5")
55
#     loss = tf.keras.losses.CategoricalCrossentropy()
56
#     optimizer = tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE)
57
#     model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
58
#     image = cv2.imread(r"D:\test_data/" + category + '/' + name)
59
#     # Preprocess image
60
#     image = cv2.imread(IMG_PATH, 1)
61
#     image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
62
#     image = image.astype("float") / 255.0
63
#     image = img_to_array(image)
64
#     image = np.expand_dims(image, axis=0)
65
#     predictions = model.predict(image).ravel()
66
#     # Print predictions
67
#     print(predictions)
68
#     # Get the class with the highest probability
69
#     prediction = np.argmax(predictions)
70
#     # Check if the prediction is correct
71
#     correct = True if classes[prediction].lower() == category else False
72
#     # Draw the image and show the best prediction
73
#     image = cv2.resize(image, (256, 256))
74
#     cv2.putText(image, '{0}: {1} %'.format(classes[prediction], str(round(predictions[prediction] * 100, 2))),
75
#                 (12, 22), cv2.FONT_HERSHEY_DUPLEX, 0.7, (0, 0, 0), 2)
76
#     cv2.putText(image, '{0}: {1} %'.format(classes[prediction], str(round(predictions[prediction] * 100, 2))),
77
#                 (10, 20), cv2.FONT_HERSHEY_DUPLEX, 0.7, (65, 105, 225), 2)
78
#     cv2.putText(image, '{0}'.format('CORRECT!' if correct else 'WRONG!'), (12, 50), cv2.FONT_HERSHEY_DUPLEX, 0.7,
79
#                 (0, 0, 0), 2)
80
#     cv2.putText(image, '{0}'.format('CORRECT!' if correct else 'WRONG!'), (10, 48), cv2.FONT_HERSHEY_DUPLEX, 0.7,
81
#                 (0, 255, 0) if correct else (0, 0, 255), 2)
82
#
83
#     # Append the image
84
#     blocks.append(image)
85
#
86
#
87
# # Display images and predictions
88
# row1 = np.concatenate(blocks[0:3], axis=1)
89
# row2 = np.concatenate(blocks[3:6], axis=1)
90
# # cv2.imshow('Predictions', np.concatenate((row1, row2), axis=0))
91
# cv2.imwrite('D:/New folder/New folderpredictions_renet50.jpg', np.concatenate((row1, row2), axis=0))
92
# cv2.waitKey(0)