DeOldify 接口化改造 集成 Flask

类似的图片修复项目 GFPGAN 的改造见我另一篇文
https://blog.csdn.net/weixin_43074462/article/details/132497146

DeOldify 是一款开源软件,用于给黑白照片或视频上色,效果还不错。

安装部署教程请参考别的文章,本文基于你给项目跑通,并 pip 集成过 flask 后

本修改支持加载一次模型,持续提供 HTTP 服务

在根目录新建主文件 run_serv.py

import io

from deoldify import device
from deoldify.device_id import DeviceId
from deoldify.visualize import *
from flask import Flask, request, Response

app = Flask(__name__)

# 提取通用代码
# choices:  CPU, GPU0...GPU7
device.set(device=DeviceId.GPU0)
plt.style.use('dark_background')
colorizer = get_image_colorizer(artistic=True)
torch.backends.cudnn.benchmark = True


@app.route('/action', methods=['POST', 'GET'])
def action():
    file = request.files['file']
    img_name = file.filename
    _, ext = os.path.splitext(img_name)
    img_in_memory = io.BytesIO(file.read())

	#此处的35应该是种子而不是参数 可以改成25或者什么的,影响似乎不大
    img_ret = colorizer.get_transformed_image_pzj(img_in_memory, 35)
    # colorizer.plot_transformed_image(path='C:/Users/PiaoZhenJia/Pictures/微信图片_20230728194252.png',
    #                                  render_factor=35, results_dir=Path('D:/'), post_process=False, watermarked=False)

    img_ret_bytes = io.BytesIO()
    img_ret.save(img_ret_bytes, format='PNG')
    img_ret_bytes.seek(0)
    res = Response(img_ret_bytes)
    res.headers.add('Content-Type', 'image/' + ext)
    res.headers.add('Content-Disposition', f'attachment; filename=colorize_{img_name}')
    return res

def main():
    app.run(port=2021, host="127.0.0.1", debug=False)


if __name__ == '__main__':
    main()

然后修改 visualize.py 我选择在187行附近加入这个函数

    def get_transformed_image_pzj(
            self, img_bytes: io.BytesIO, render_factor: int = None
    ) -> Image:
        self._clean_mem()
        orig_image = PIL.Image.open(img_bytes)
        filtered_image = self.filter.filter(
            orig_image, orig_image, render_factor=render_factor
        )

        return filtered_image

测试方法如图

在这里插入图片描述

接受一个form表单,file字段传输二进制图片文件,返回结果直接为二进制图片。可以通过 PostMan 的 SaveResponse 保存到一个 PNG 文件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

控场的朴哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值