forked from charann29/opensource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictions.py
48 lines (39 loc) · 1.65 KB
/
predictions.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
40
41
42
43
44
45
46
47
48
import numpy as np
import tensorflow as tf
from keras.preprocessing import image
# load the models when import "predictions.py"
model_elbow_frac = tf.keras.models.load_model("weights/ResNet50_Elbow_frac.h5")
model_hand_frac = tf.keras.models.load_model("weights/ResNet50_Hand_frac.h5")
model_shoulder_frac = tf.keras.models.load_model("weights/ResNet50_Shoulder_frac.h5")
model_parts = tf.keras.models.load_model("weights/ResNet50_BodyParts.h5")
# categories for each result by index
# 0-Elbow 1-Hand 2-Shoulder
categories_parts = ["Elbow", "Hand", "Shoulder"]
# 0-fractured 1-normal
categories_fracture = ['fractured', 'normal']
# get image and model name, the default model is "Parts"
# Parts - bone type predict model of 3 classes
# otherwise - fracture predict for each part
def predict(img, model="Parts"):
size = 224
if model == 'Parts':
chosen_model = model_parts
else:
if model == 'Elbow':
chosen_model = model_elbow_frac
elif model == 'Hand':
chosen_model = model_hand_frac
elif model == 'Shoulder':
chosen_model = model_shoulder_frac
# load image with 224px224p (the training model image size, rgb)
temp_img = image.load_img(img, target_size=(size, size))
x = image.img_to_array(temp_img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
prediction = np.argmax(chosen_model.predict(images), axis=1)
# chose the category and get the string prediction
if model == 'Parts':
prediction_str = categories_parts[prediction.item()]
else:
prediction_str = categories_fracture[prediction.item()]
return prediction_str