-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
95 lines (81 loc) · 2.61 KB
/
script.py
File metadata and controls
95 lines (81 loc) · 2.61 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import os
import sys
import keras as K
import json
import base64
from io import BytesIO
from PIL import Image
from keras.preprocessing import image
from keras.applications.inception_resnet_v2 import preprocess_input
def init():
global model
print("Executing init() method...")
print("Python version: " + str(sys.version) + ", keras version: " + K.__version__)
# Load the model
#model = K.models.load_model('azureml-models/kerasmodel3/1/kerasmodel3.pkl')
model = K.models.load_model('kerasmodel3.pkl')
return
def predict(model, img):
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict_classes(x)
y = model.predict(x)[0]
y = np.expand_dims(y, axis=-1)
print(y)
b = []
a = y[0]
b.append(np.float(a))
a = y[1]
b.append(np.float(a))
a = y[2]
b.append(np.float(a))
a = y[3]
b.append(np.float(a))
b.append(preds[0])
return b
def run(inputString):
responses = []
base64Dict = json.loads(inputString)
for k, v in base64Dict.items():
img_file_name, base64Img = k, v
decoded_img = base64.b64decode(base64Img)
img_buffer = BytesIO(decoded_img)
#imageData = Image.open(img_buffer).convert("RGB")
# # Evaluate the model using the input data
# img = ImageOps.fit(imageData, (32, 32), Image.ANTIALIAS)
# img_conv = np.array(img) # shape: (32, 32, 3)
# # Scale pixel intensity
# x_test = img_conv / 255.0
# # Reshape
# x_test = np.moveaxis(x_test, -1, 0)
# x_test = np.expand_dims(x_test, 0) # shape (1, 3, 32, 32)
# y_pred = model.predict(x_test)
# y_pred = np.argmax(y_pred, axis=-1)
# # print(y_pred)
# data = json.loads(inputString)['data']
#img_input = base64.decodestring(json.dumps(inputString)[0])
img = image.load_img(img_buffer,target_size=(224, 224))
#img = json.loads(inputString)['data']
preds = predict(model, img)
LABELS = ["Category A","Category B","Category C","Category D"]
resp = LABELS[preds[-1]]
responses.append(preds[0])
responses.append(preds[1])
responses.append(preds[2])
responses.append(preds[3])
responses.append(resp)
return json.dumps(responses)
if __name__ == "__main__":
init()
# input data
img_path = '00000668.JPG'
#input_data = "{\"data\": [" + str(list(img_path)) + "]}"
encoded = None
with open(img_path, 'rb') as file:
encoded = base64.b64encode(file.read())
img_dict = {img_path: encoded.decode('utf-8')}
body = json.dumps(img_dict)
resp = run(body)
print(resp)