【深度学习】MAT,Image Inpainting,代码实战,接口直接用,水印去除,水印Inpaint
创始人
2024-12-29 07:40:48
0

https://github.com/fenglinglwb/mat

文章目录

  • 基础镜像
  • fastapi
  • 总结
  • 图片批量访问去除水印的请求代码
  • 使用感受

基础镜像

docker run -it -p 7898:7860 --gpus device=3 kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-xformers bash 
git clone https://github.com/fenglinglwb/MAT.git   cd MAT/  pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple 
apt-get update && apt-get install ffmpeg libsm6 libxext6  -y 

一系列操作后得到一个环境镜像,FFHQ_512.pkl在其中:

docker push kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat 

可以用这个镜像尝试inpaint效果:

docker run -it -p 7898:7860 --gpus device=3 kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat bash 

执行inpaint:

cd /workspace/MAT  python generate_image.py --network pretrained/FFHQ_512.pkl --dpath images --mpath masks --outdir samples 

原图
在这里插入图片描述
mask图:
在这里插入图片描述
结果去除图:
在这里插入图片描述

fastapi

安装了一些fastapi的环境:

kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat-apibase 

进而写dockerfile:

FROM kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat-apibase EXPOSE 7860 ENTRYPOINT cd /workspace/MAT/ && python /workspace/MAT/mianfastapi.py 

build:

docker build  -f Dockerfile1 . -t kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat-api 

只需要执行这个镜像就可以启动服务:

docker run -d -p 7898:7860 --gpus device=3 kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat-api  

在这里插入图片描述

总结

启动服务:

docker run -d -p 7898:7860 --gpus device=3 kevinchina/deeplearning:pytorch2.3.0-cuda12.1-cudnn8-devel-mat-api  

访问服务:

import requests  url = "http://10.136.19.26:7898/inpaint" image_path = "image.jpg" mask_path = "mask.jpg"  # 读取图像和掩码文件 with open(image_path, "rb") as img_file, open(mask_path, "rb") as mask_file:     files = {         "image": img_file,         "mask": mask_file     }      # 发送POST请求     response = requests.post(url, files=files)      # 检查响应状态码     if response.status_code == 200:         # 保存生成的图像         with open("output.png", "wb") as out_file:             out_file.write(response.content)         print("生成的图像已保存为 output.png")     else:         print(f"请求失败,状态码: {response.status_code}")         print(response.text)  

图片批量访问去除水印的请求代码

import base64 import io import os import traceback  import requests import cv2 import numpy as np import json from PIL import Image from PIL import ImageDraw import numpy as np import cv2 from tqdm import tqdm import json   def listPathAllfiles(dirname):     result = []     for maindir, subdir, file_name_list in os.walk(dirname):         for filename in file_name_list:             apath = os.path.join(maindir, filename)             result.append(apath)     return result   url = "http://10.136.19.26:7898/inpaint" # image_path = "image.jpg" # mask_path = "mask.jpg" # # # 读取图像和掩码文件 # with open(image_path, "rb") as img_file, open(mask_path, "rb") as mask_file: #     files = { #         "image": img_file, #         "mask": mask_file #     } # #     # 发送POST请求 #     response = requests.post(url, files=files) # #     # 检查响应状态码 #     if response.status_code == 200: #         # 保存生成的图像 #         with open("output.png", "wb") as out_file: #             out_file.write(response.content) #         print("生成的图像已保存为 output.png") #     else: #         print(f"请求失败,状态码: {response.status_code}") #         print(response.text)   src = r"/ssd/xiedong/xiezhenceshi/xiezhen_datasets" save_img_dst_output_inpaint_alpha = r"/ssd/xiedong/xiezhenceshi/inpaint_alpha" os.makedirs(save_img_dst_output_inpaint_alpha, exist_ok=True) files = listPathAllfiles(src) files.sort() files = [file for file in files if file.endswith(".jpg")]  for src_image_file in tqdm(files):     try:         ocr_ret_file = src_image_file.replace(".jpg", ".json")         output_image_file_alpha = src_image_file.replace(src, save_img_dst_output_inpaint_alpha)         if not os.path.exists(ocr_ret_file):             print(f"ocr_ret_file not exists: {ocr_ret_file}")             continue         if os.path.exists(output_image_file_alpha):             print(f"output_image_file_alpha exists: {output_image_file_alpha}")             continue         output_image_file_alpha_father = os.path.dirname(output_image_file_alpha)         os.makedirs(output_image_file_alpha_father, exist_ok=True)          # 造一个mask图片在本地         ocr_json_data = json.load(open(ocr_ret_file, "r", encoding="utf-8"))         image = cv2.imread(src_image_file)         # 只要中心512*512的图         image_zitu = image[image.shape[0] // 2 - 256:image.shape[0] // 2 + 256,                      image.shape[1] // 2 - 256:image.shape[1] // 2 + 256]         mask = np.zeros(image.shape, dtype=np.uint8)         for item in ocr_json_data:             box = item[0]             cv2.fillPoly(mask, np.array([box], dtype=np.int32), (255, 255, 255))         # 只要中心512*512的图         mask_zitu = mask[mask.shape[0] // 2 - 256:mask.shape[0] // 2 + 256,                     mask.shape[1] // 2 - 256:mask.shape[1] // 2 + 256]         # 取反mask_zitu的选择         mask_zitu = cv2.bitwise_not(mask_zitu)         src_image_file_rb = cv2.imencode('.jpg', image_zitu)[1].tobytes()         mask_file_rb = cv2.imencode('.jpg', mask_zitu)[1].tobytes()          files = {             "image": src_image_file_rb,             "mask": mask_file_rb         }          # 发送POST请求         response = requests.post(url, files=files)          # 检查响应状态码         if response.status_code == 200:             # 保存生成的图像             # with open("output.png", "wb") as out_file:             #     out_file.write(response.content)             # print("生成的图像已保存为 output.png")             image_inpaint = Image.open(io.BytesIO(response.content)).convert('RGB')             image_inpaint_cv2 = np.array(image_inpaint)             image_inpaint_cv2 = cv2.cvtColor(image_inpaint_cv2, cv2.COLOR_RGB2BGR)              # 贴回到原图             image[image.shape[0] // 2 - 256:image.shape[0] // 2 + 256, \             image.shape[1] // 2 - 256:image.shape[1] // 2 + 256] = image_inpaint_cv2              cv2.imwrite(output_image_file_alpha, image)         else:             print(f"请求失败,状态码: {response.status_code}")             print(response.text)     except:         traceback.print_exc()  

使用感受

不行,基本传统的inpaint就是很垃圾,效果不行无法投入使用,生成还得看StableDiffusion,但StableDiffusion就是很慢,如果有希望把LCM和小的SD模型用起来,就很nice了。

相关内容

热门资讯

随着!新九九辅助,wepoke... 随着!新九九辅助,wepoker到底有没有透视(透视)本来是真的有辅助插件(哔哩哔哩)1、完成新九九...
据悉"中至赣州小程序... 据悉"中至赣州小程序辅助器"原来存在有辅助软件(哔哩哔哩)1、进入游戏-大厅左侧-新手福利-激活码辅...
反观!闲逸透视app官方正版,... 反观!闲逸透视app官方正版,福建天天开心辅助器真的假的(原来真的是有修改器)-哔哩哔哩1、进入游戏...
现有说明如下!雀友圈辅助器,a... 您好,雀友圈辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在...
透视黑科技"衢州都莱... 透视黑科技"衢州都莱罗松怎么才能赢"本来真的有辅助攻略(哔哩哔哩)1)衢州都莱罗松怎么才能赢有没有挂...
迎来新发展!开心泉州作z弊,微... 迎来新发展!开心泉州作z弊,微信小程序游戏破解器(竟然存在有软件)-哔哩哔哩1、点击下载安装,微信小...
据目击者称!雀神广东定制插件,... 据目击者称!雀神广东定制插件,wepoker怎么挂底牌(透视)果然真的有辅助脚本(哔哩哔哩)1、起透...
透视计算"四川小程序... 透视计算"四川小程序辅助器免费"其实确实有辅助脚本(哔哩哔哩)透视计算"四川小程序辅助器免费"其实确...
今日!决战卡五星有辅助吗,hh... 今日!决战卡五星有辅助吗,hhpoker脚本(透视)都是有辅助工具(哔哩哔哩)1、任何决战卡五星有辅...
长期以来!朋友局app辅助器下... 长期以来!朋友局app辅助器下载,雀友会钻石辅助器潮汕麻将(果然是有平台)-哔哩哔哩1、起透看视 雀...