import time
import requests
import json
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os
def mainProcess(image_data):
"""
核心代码: 将请求数据发送到远端模型, 返回预测结果
"""
start = time.time()
####--------------------------核心代码----------------------------------------####
""" REST API端口
10.0.20.197为服务端地址
my_sample为部署时设置的模型名称
"""
url = 'http://10.0.20.197:8501/v1/models/my_sample:predict'
data = json.dumps({'inputs':image_data.tolist()}) # 要求输入的数据是json格式
response = requests.post(url,data=data)
# print(response)
result = json.loads(response.content) # 解析返回数据
# for key,val in result.items():
# print(key,val)
outputs = result['outputs'][0] # 得到输出
output_array = np.array(outputs) # list转numpy数组
####--------------------------核心代码---------------------------------------####
print(f'花费时间:{time.time()-start:.2f}s')
# print(type(output_array))
return output_array
def letterbox_image(image, size):
iw, ih = image.size
w, h = size
scale = min(w/iw, h/ih)
nw = int(iw*scale)
nh = int(ih*scale)
image = image.resize((nw,nh), Image.BICUBIC)
new_image = Image.new('RGB', size, (128,128,128))
new_image.paste(image, ((w-nw)//2, (h-nh)//2))
return new_image
def preprocess_input(x):
x /= 127.5
x -= 1.
return x
def preProcessing(filepath,input_shape:list):
image = Image.open(filepath)
image_data = letterbox_image(image, [input_shape[1], input_shape[0]])
image_data = np.expand_dims(preprocess_input(np.array(image_data, np.float32)), 0)
return image_data,image
def postProcessing(output_array,class_names,image,filepath):
"""
对预测结果进行解码并保存结果
"""
saveDir = "preResults/"
if not os.path.exists(saveDir):
os.mkdir(saveDir)
probability = np.max(output_array)
cls_index = np.argmax(output_array)
class_name = class_names[cls_index]
plt.ion()
plt.imshow(image)
plt.title(f'Class:{class_name} Probability:{probability:.3f}')
# 保存
plt.axis('off')
plt.savefig(saveDir+os.path.basename(filepath))
plt.show(block=False)
plt.pause(1) # 显示1s
plt.close()
if __name__ == '__main__':
input_shape = [224,224,3]
class_names = ["EOSINOPHIL","LYMPHOCYTE","MONOCYTE","NEUTROPHIL"]
while True:
filepath = input('请输入待预测图像路径(输入c退出): ')
if filepath == 'c':
break
image_data,image = preProcessing(filepath=filepath,input_shape=input_shape)
# 远端预测
output_array = mainProcess(image_data)
# 根据预测得到的输出进行解码,并保存结果
postProcessing(output_array,class_names, image,filepath)
图像分类的serving
最新推荐文章于 2024-07-20 17:12:48 发布