图像分类的serving

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值