-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpPostRequest.py
50 lines (41 loc) · 1.52 KB
/
httpPostRequest.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
49
50
#from classification_models.keras import Classifiers
#from tensorflow.keras import preprocessing
from tensorflow.keras.preprocessing import image
import numpy as np
import os
import requests
from scipy import spatial
from sklearn.preprocessing import minmax_scale
import json
from PIL import Image
preprocess_input = None
def load_image_featureVectors(path):
#global preprocess_input
#ResNet18, preprocess_input = Classifiers.get('resnet18')
img = image.load_img("images/coin3.jpg", target_size=(224, 224))
#img = Image.open("images/coin3.jpg")
img_data = prepare_image(img)
#featureVector = model.predict(img_data)
url = 'http://localhost:8501/v1/models/similarityModel:predict'
headers = {"content-type": "application/json"}
#print(img_data)
#print(img_data.shape)
data = json.dumps({"signature_name": "serving_default", "instances": img_data.tolist()})
#print(json_str)
response = requests.post(url, data = data, headers=headers)
dict = json.loads(response.text)
#print(dict["predictions"])
feature_np = np.array(dict["predictions"])
# min-max scale the data between 0 and 1
scaledVec = minmax_scale(feature_np.flatten())
roundedVec = np.round(scaledVec, 2)
print(roundedVec)
def prepare_image(img, target_size=(224,224)):
img = img.resize(target_size)
img = image.img_to_array(img)
#img = np.array(img)
img = np.expand_dims(img, axis=0)
#img = preprocess_input(img)
return img
if __name__ == "__main__":
load_image_featureVectors("images")