from keras.preprocessing import image
import os
import base64
from io import BytesIO
def image_to_base64(img):
buffered = BytesIO()
img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue())
return img_str.decode("utf-8")
def test(img_name):
img_name = img_name + '.jpg'
img_name = 'inference/images/{}'.format(img_name)
if os.path.exists(img_name) is False:
img_name = 'inference/output/{}'.format('not_exist.jpg')
res_img = image.load_img(img_name, target_size=(32, 32))
return image_to_base64(res_img)
def handle(conf):
"""
该方法是部署之后,其他人调用你的服务时候的处理方法。
请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。
范例:
params['key'] = value # value_type: str # description: some description
value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]
参数请放到params字典中,我们会自动解析该变量。
"""
img_name = conf['img_name'] # value_type: str # description: some description
# add your code
res_img = test(img_name)
return {'res_img': res_img}
if __name__ == '__main__':
conf = {}
conf['img_name'] = 'batch_1_000029'
res_img = handle(conf)
print(res_img)