-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_module.py
40 lines (33 loc) · 1.14 KB
/
app_module.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
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
import tensorflow as tf
from keras.models import model_from_json
import matplotlib.pyplot as plt
IMG_SIZE = (224, 224)
def load_model():
# load json and create model
json_file = open('model/best_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model/best_model.h5")
return loaded_model
def get_image(path, img_size=IMG_SIZE):
img = load_img(path, target_size=img_size)
img = img_to_array(img) / 255.
return img
def classify(path):
model = load_model()
label = ['clean', 'dirty']
img = get_image(path, img_size=IMG_SIZE)
my_image = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
y_pred = model.predict(my_image)
y_hat = np.argmax(y_pr, axis=1)
yi_hat = label[y_hat[0]]
yi_pr = y_pred[0].max()
title_sub = f"Prediction: {yi_hat} ({yi_pr:.1%})"
print("\n")
print(title_sub)
return title_sub