AIStudio上线趣味体验馆功能,可在线体验AI模型。使用需先部署模型:选静态图推理模型,设输入输出(图像为Base64编码),编输入输出转换器代码(用PIL处理图像),测试沙盒后正式部署,再创建体验馆,配置名称、介绍及输入输出信息即可体验,项目可公开分享。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

import reimport base64import numpy as npimport paddle.fluid as fluidfrom PIL import Imagefrom io import BytesIO# 将 Base64 转成 PIL Image def base64_to_pil(image_base64):
"""read image from memory"""
image_base64 = re.sub('^data:image/.+;base64,', '', image_base64) # 需要去除头部格式信息
image_mem = BytesIO(base64.b64decode(image_base64)) # python3
image_pil = Image.open(image_mem).convert('RGB') return image_pil# 预处理代码def preprocess(img):
# 图像缩放
max_size, min_size = 720, 32
w, h = img.size if max(h,w)>max_size:
img = img.resize((max_size, int(h/w*max_size))) if h<w else img.resize((int(w/h*max_size), max_size)) elif min(h,w)<min_size:
img = img.resize((min_size, int(h/w*min_size))) if h>w else img.resize((int(w/h*min_size), min_size)) # 裁剪图片
w, h = img.size
img = np.array(img)
img = img[:h-(h%32), :w-(w%32), :] # 归一化
img = img/127.5 - 1.0
return imgdef reader_infer(data_args):
"""
reader inter
:param data_args: 接口请求参数
:return [[]], feeder
"""
def reader():
"""
reader
:return:
"""
# image <type 'image'> default value:None
image = data_args['image']
# 格式转换
image = base64_to_pil(image)
# 预处理
image = preprocess(image)
# 根据输入 Tensor 的名称和 Shape 构建 DataFeeder
h, w, c = image.shape
img = fluid.layers.data(name='x2paddle_generator_input', shape=[h, w, c], dtype='float32')
feeder = fluid.DataFeeder([img], fluid.CPUPlace()) return [[image]], feeder return readerimport reimport base64import numpy as npfrom PIL import Imagefrom io import BytesIO
# PIL Image 转换为 Base64def pil_to_base64(image_pil):
"""save PIL image in memory, and convert to base64"""
image_buffer = BytesIO() # image_buffer = StringIO() # python2
image_pil.save(image_buffer, format='JPEG')
image_bytes = image_buffer.getvalue()
image_base64 = base64.b64encode(image_bytes).decode() return image_base64# 后处理def postprocess(output):
# 反归一化
image = (output.squeeze() + 1.) / 2 * 255
# 限幅
image = np.clip(image, 0, 255).astype(np.uint8)
# 格式转换
image = Image.fromarray(image)
return imagedef output(results, data_args):
"""
模型评测结果输出转换
:param results
:param data_args 请求参数
:return dict
"""
# todo
# 后处理
image = postprocess(results[0])
# Base64 编码
code = pil_to_base64(image)
# 加上文件头进行结果返回
return {'image': 'data:image/jpeg;base64,'+code}
以上就是趣味体验馆踩坑指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号