部署散记1(增强)

2023-05-16

拉取modelscope镜像

sudo docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.1.2

拉取增强代码

git clone https://gitee.com/binghai228/Bringing-Old-Photos-Back-to-Life.git
cd Bringing-Old-Photos-Back-to-Life

安装增强模块:

cd Face_Enhancement/models/networks/
git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
cp -rf Synchronized-BatchNorm-PyTorch/sync_batchnorm .
cd ../../../

cd Global/detection_models
git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
cp -rf Synchronized-BatchNorm-PyTorch/sync_batchnorm .
cd ../../

cd Face_Detection/
wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
bzip2 -d shape_predictor_68_face_landmarks.dat.bz2
cd ../

cd Face_Enhancement/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/face_checkpoints.zip
unzip face_checkpoints.zip
cd ../
cd Global/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/global_checkpoints.zip
unzip global_checkpoints.zip
cd ../

创建容器:

sudo nvidia-docker run -dit --net=host --name beauty --shm-size="1g" -v $PWD:/beauty registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.1.2 bash

进入容器:

sudo docker exec -it beauty bash
cd beauty

安装依赖:

pip install -r requirements.txt

测试修复:

python run.py --input_folder test \
              --output_folder result \
              --GPU 0 \
              --with_scratch \
              --HR

从官网下载redis稳定版到项目根目录下。

tar -zxvf redis-7.0.7.tar.gz

在dockers环境中编译redis:

cd redis-7.0.7
make

编译完成后安装redis:

cd src
make install

启动redis:

redis-server &

测试连接:

redis-cli
redis 127.0.0.1:6379> ping

正常输出如下:

PONG

想要查看所有键值:

keys *

删除某个键值:

del key1

断开连接:

exit

其他相关命令如下:

redis-server stop
redis-server start
redis-server restart

安装数据库驱动:

pip install redis

安装fastapi:

pip install fastapi -i https://mirror.baidu.com/pypi/simple
pip install "uvicorn[standard]" -i https://mirror.baidu.com/pypi/simple
pip install python-multipart -i https://mirror.baidu.com/pypi/simple

后台代码:

import numpy as np
import cv2
import base64
from fastapi import FastAPI, File, UploadFile, Depends
import time
import uuid
import os
from fastapi import Request
import json
from redis import Redis,ConnectionPool

# 导入自定义函数
from tool import base64_encode_image,base64_decode_image 

# 开发前端界面
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware


# 定义fastapi的启动app
app = FastAPI()

# 解除跨域访问限制
origins = ["*"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 定义Redis数据库依赖注入
def get_rdb():
    pool = ConnectionPool(host='127.0.0.1',port = 6379)
    rdb = Redis(connection_pool=pool)
    try:
        yield rdb
    finally:
        rdb.close()
        

# 定义访问接口
@app.post("/beauty/")
async def get_image(request: Request,file: UploadFile =File(...), db:Redis=Depends(get_rdb)):
    print("接收到数据")
    
    # 接收前端上传的图片
    imgdata = await file.read()
    imgdata = np.frombuffer(imgdata, np.uint8)
    image = cv2.imdecode(imgdata, cv2.IMREAD_COLOR)
    h,w,c = image.shape

    # 图像存储进redis
    k = str(uuid.uuid4())
    in_data = {"in_img": base64_encode_image(image.astype('float32')),"height":h,"width":w,"channel":c}
    db.set(k, json.dumps(in_data))
    db.expire(k, 50) # 设置失效时间
    
    # 等待返回
    time_out_num = 0
    while True:
        # 尝试获取结果
        output = db.get(k)
        output = json.loads(output.decode("utf-8"))
        if output.get('out_img') is not None:
            #取出结果
            im = base64_decode_image(output["out_img"],"float32",(output.get('height'), output.get('width'),output.get('channel')))
            
            #封装结果返回
            db.delete(k)
            _, buffer_img = cv2.imencode('.jpg', im)
            img64 = base64.b64encode(buffer_img)
            img64 = str(img64, encoding='utf-8')
            print("完成")
            return {"state":1 ,"img": img64}

        time.sleep(1)

        #设置超时机制
        time_out_num = time_out_num + 1
        if (time_out_num > 50):
            db.delete(k)
            print('超时')
            return {"state":-1 ,"code": '当前拥挤,请稍候再试'}


# 挂载静态资源目录
app.mount("/static", StaticFiles(directory="static"), name="static")

# 定义页面模板库位置
templates = Jinja2Templates(directory="templates")

# 定义首页
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

# if __name__ == "__main__":
#     '''程序启动入口'''
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8041)

启动fastapi:

uvicorn main_fastapi:app --host 0.0.0.0 --port 8041 &

其中tool.py文件函数如下:

import numpy as np
import base64

def base64_encode_image(a):
    """将numpy数组进行base64编码"""
    return base64.b64encode(a).decode("utf-8")
 
 
def base64_decode_image(a, dtype, shape):
    """base64转图像"""
    a = bytes(a, encoding="utf-8")
    a = np.frombuffer(base64.decodestring(a), dtype=dtype)
    a = a.reshape(shape)
    return a

模型循环推理代码:

#导入第三方库
import os
import numpy as np
import json
import cv2
import base64
import time
import redis

#导入自定义库
from tool import base64_encode_image,base64_decode_image 

# 导入推理脚本
# from run import inference
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
 

if __name__ == "__main__":
    #参数设置
    db = redis.StrictRedis(host="localhost", port=6379, db=0)
    
    skin_retouching = pipeline(Tasks.skin_retouching,model='damo/cv_unet_skin-retouching')

    print('美颜模型开始运行')
    
    #循环处理
    while True:
        # 获取有效键值
        keys = db.keys()
        isfinder = False
        for k in keys:
            output = db.get(k)
            if output is None:
                continue
            output = json.loads(output.decode("utf-8")) 
                
            if output.get('out_img') is None:
                isfinder = True
                in_img = base64_decode_image(output["in_img"],"float32",(output.get('height'), output.get('width'),output.get('channel')))
                break
            else:
                continue
        
        if not isfinder:
            time.sleep(0.2)
            continue
        
        # 解析图像   
        cv2.imwrite('./in/test.jpg',in_img)
        
        # 开始推理
        #inference()
        result = skin_retouching('./in/test.jpg')
        cv2.imwrite('./out/result.png', result[OutputKeys.OUTPUT_IMG])
        
        # 读取推理结果
        out_img = cv2.imread('./out/result.png',cv2.IMREAD_COLOR)
    
        # 将结果写入redis
        out_data = {"out_img": base64_encode_image(out_img.astype('float32'))}
        output.update(out_data)
        db.set(k, json.dumps(output))
        
        print("完成推理")

        time.sleep(0.2)

本地测试脚本client_beauty.py:

# 导入依赖库
import numpy as np
import requests
import cv2
import base64
 
# 定义http接口
url = "http://127.0.0.1:8041/beauty"
 
# 发送请求
files = {'file':open('1.jpg','rb'),}
result = requests.post(url=url, files=files)
 
# 解析返回值
result = result.json()
 
# 解码返回的图像数据
img = result["img"]
img = bytes(img, encoding='utf-8')  # str转bytes
img = base64.b64decode(img)  # base64解码
img = np.asarray(bytearray(img), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED)
 
# 保存图像到本地
if img is None:
    print('调用失败')
else:
    cv2.imwrite('result.png',img)
    print('完成')

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

部署散记1(增强) 的相关文章

  • C语言调用libcurl的一个简单例子

    首先我们创建一个php页面 xff1a lt meta http equiv 61 span class hljs string 34 Content Type 34 span content 61 span class hljs stri
  • 【C++】类构造函数、析构函数的调用顺序「完整版」

    一 全局变量 静态变量和局部变量 全局变量在程序开始时调用构造函数 在程序结束时调用析构函数 静态变量在所在函数第一次被调用时调用构造函数 在程序结束时调用析构函数 xff0c 只调用一次 局部变量在所在的代码段被执行时调用构造函数 xff
  • linux下使用shell发送http请求

    本文主要介绍如何在linux下使用shell发送http请求 一 curl 1 get请求 curl命令默认下就是使用get方式发送http请求 curl www span class hljs preprocessor baidu spa
  • 【STL真好用】1057 Stack C++(30)

    1057 Stack 30 分 Stack is one of the most fundamental data structures which is based on the principle of Last In First Ou
  • C++学习之头文件引用

    目录结构如下 test h的定义如下 xff1a ifndef TEST H define TEST H include lt vector gt include lt string gt using namespace std class
  • checksum 算法

    说明 checksum xff1a 总和检验码 xff0c 校验和 xff0c 可以理解为check xff08 校验 xff09 xff0c sum xff08 和 xff09 在数据处理和通信领域 xff0c 通过一定算法对传输的数据进
  • 解决cannot open shared object file: No such file or directory

    一 linux下调用动态库 so文件时提示 xff1a cannot open shared object file No such file or directory 解决办法 xff1a 1 此时ldd xxx查看依赖缺少哪些库 lib
  • cmake 使用(六)

    本文是 cmake 使用的第六篇 主要介绍如何设置编译器优化标志 上一篇的链接为 xff1a https blog csdn net QCZL CC article details 119825737 xff0c 主要介绍如何将自己的软件安
  • 8086寄存器介绍

    8086 有14个16位寄存器 xff0c 这14个寄存器按其用途可分为 1 通用寄存器 2 指令指针 3 标志寄存器和 4 段寄存器等4类 1 通用寄存器有8个 又可以分成2组 一组是数据寄存器 4个 另一组是指针寄存器及变址寄存器 4个
  • C++常用操作符:: -> . (例子详解)

    C 43 43 提供了三种访问类或者类对象的操作符 xff0c 他们是 双冒号 点 箭头 gt 这三种操作符有着各自的使用场景和定义 双冒号 A B 表示作用域运算符 A一定是一个类的名称或命名空间的名称 仅仅用于当B是A类 A命名空间的一
  • STM32中断优先级的分配以及中断原则

    STM32d的中断优先级由NVIC IPRx寄存器来配置 xff0c IPR的宽度为8bit所以原则上每个中断可配置的优先级为0 255 xff0c 数值越小优先级越高 xff0c 但对于大部分的 Cortex M3芯片都会精简设计 xff
  • 晶体管的结构、类型和三种组态

    晶体管有两大类型 双极型晶体管 BJT 和场效应管 FET 双极型晶体管又称为半导体三极管 晶体三极管 xff0c 简称晶体管 它由两个PN结组合而成 xff0c 有两种载流子参与导电是一种电流控制电流源器件 场效应管仅有一种载流子参与导电
  • STM32单片机基础09——重定向printf函数到串口输出的多种方法

    本文详细的介绍了如何重定向printf输出到串口输出的多种方法 xff0c 包括调用MDK微库 xff08 MicroLib xff09 的方法 xff0c 调用标准库的方法 xff0c 以及适用于 GNUC 系列编译器的方法 1 prin
  • STM32直流减速电机控制篇(一)PWM调速

    直流电机原理 下面是分析直流电机的物理模型图 其中 xff0c 固定部分有磁铁 xff0c 这里称作主磁极 xff1b 固定部分还有电刷 转动部分有环形铁芯和绕在环形铁芯上的绕组 直流电机的转动原理我就不再赘述 xff0c 比较简单易懂 直
  • STM32直流减速电机控制篇(二)编码器测速原理

    编码器 编码器是一种将角位移或者角速度转换成一连串电数字脉冲的旋转式传感器 xff0c 我们可以通过编码器测量到底位移或者速度信息 编码器从输出数据类型上分可以分为增量式编码器和绝对式编码器 从编码器检测原理上来分 xff0c 还可以分为光
  • STM32直流减速电机控制篇(三)编码器测速程序编写

    编程思路 任何一个程序的编写我们都应该先理清楚编程思路 xff0c 通过上一篇讲解的编码器测速原理我们应该知道要想通过编码器得知电机转速我们第一步就应该是捕获A相和B相输出的脉冲 因为电机速度的定义是单位时间内的转数 xff0c 所以第二步
  • GPIO模式

    开漏输出 只能输出低电平 xff0c 不能输出高电
  • 单片机485通信

    1 RS485简介 485 xff08 一般称作 RS485 EIA 485 xff09 是隶属于 OSI 模型物理层的电气特性规定为 2 线 xff0c 半双工 xff0c 多点信的标准 它的电气特性和 RS 232 大不一样 用缆线两端
  • Jetson Xavier NX 镜像制作、烧录及克隆

    以下所有方法仅适用于Jetson Xavier Nx 16G emmc版本 其他版本仅供参考 官方文档下载链接为https developer nvidia com embedded downloads search 61 Develope

随机推荐