用Lama大模型去水印!

一、本地安装大模型。

https://github.com/advimman/lama?tab=readme-ov-file

二、调用大模型去除水印。

生成掩码图

import cv2
import numpy as np

# 你的原图路径
input_path = "inputs/input.png"

# 加载原图来获取宽高
img = cv2.imread(input_path)
h, w = img.shape[:2]

# 创建全黑 mask(0 表示保留区域)
mask = np.zeros((h, w), dtype=np.uint8)

# 填白色区域表示要修复的部分(logo 坐标)
cv2.rectangle(mask, (782, 1272), (1046, 1353), 255, thickness=-1)

# 保存 mask
cv2.imwrite("inputs/mask.png", mask)
print("mask.png 已生成")

问题:

python3 bin/predict.py model.path=$(pwd)/big-lama indir=./inputs outdir=./outputs +config=./configs/prediction/default.yaml device=cpu

没有生成对应的 outputs图片的解决步骤:

python3 -c "from PIL import Image; print(Image.open('./inputs/input.png').size, Image.open('./inputs/mask.png').size)"


python3 -c "from PIL import Image; print(Image.open('./inputs/input.png').mode)"
RGB

python3 -c "from PIL import Image; print(Image.open('./inputs/input.png').size, Image.open('./inputs/mask.png').size)"
L 单通道灰度图像

1.检查模型路径

你的命令中指定 model.path=$(pwd)/big-lama,确认 big-lama 文件或目录存在于当前工作目录:
ls $(pwd)/big-lama
LaMa 模型通常需要一个 .pth 或 .ckpt 文件(检查点文件)。
如果 big-lama 是一个目录,确认其中包含正确的模型文件(例如 best.ckpt)。
如果模型文件路径不正确,脚本可能无法加载模型,导致无输出

2.检查配置文件


确认 ./configs/prediction/default.yaml 文件存在且格式正确:

掩码文件名问题:
配置文件未明确指定 mask_suffix,LaMa 默认可能要求掩码文件名为 _mask.png(如 input_mask.png),而你的掩码是 mask.png。
重命名掩码文件:

mv ./inputs/mask.png ./inputs/input_mask.png

3.确认输入图片和掩码格式

input.png:确认是 RGB 图像(3 通道)。可以用以下命令检查:

file ./inputs/input.png
python3 -c "from PIL import Image; print(Image.open('./inputs/input.png').mode)"


预期输出类似 PNG image 和 RGB。如果模式是 RGBA 或 L(灰度),可能需要转换为 RGB:

mask.png:确认是单通道(灰度)图像,白色区域(255)表示需要修复的部分,黑色(0)表示保留区域。检查:
bash

python3 -m bin.predict   model.path=$(pwd)/big-lama   indir=$(pwd)/inputs   outdir=$(pwd)/outputs   device=cpu

用Lama大模型去水印!_第1张图片

你可能感兴趣的:(用Lama大模型去水印!)