深度学习系列24:开源抠图算法

开源地址:https://github.com/danielgatis/rembg,使用起来效果还不错,官网示例如下:
在这里插入图片描述

1. 安装

在python3.6/3.7上都没有尝试成功,建议直接在pyhont3.8以上的版本进行安装:

pip install rembg

如果有GPU,则如下:

pip install rembg[gpu]

2. 使用

第一种方式,使用命令行:

# 本地图片
rembg i input.png output.png

# 远程图片
curl -s http://input.png | rembg i > output.png

# 本地文件夹
rembg p input output

第二种方式,使用服务,先起服务

rembg s

发送请求即可进行处理:

http://localhost:5000?url=http://image.png

第三种方式,使用python库

from rembg.bg import remove
import numpy as np
import io
from PIL import Image

input_path = 'input.png'
output_path = 'out.png'

# Uncomment the following line if working with trucated image formats (ex. JPEG / JPG)
# ImageFile.LOAD_TRUNCATED_IMAGES = True

f = np.fromfile(input_path)
result = remove(f)
img = Image.open(io.BytesIO(result)).convert("RGBA")
img.save(output_path)

3. 综合应用:生活照转证件照

首先抠图,放在temp文件夹中:

rembg p pictures temp

其次根据眼睛和鼻子的位置,进行框图,放在result文件夹下:

import face_recognition,os
from skimage import io
from PIL import Image
import numpy as np
path = 'temp'
pics = os.listdir(path)
for p in pics:
	print(p)
	image = Image.open(path+p).convert("RGBA")
	new_image = Image.new("RGBA", image.size, "WHITE")
	new_image.paste(image, (0, 0), image)
	image=np.array(new_image)
	he,wi = image.shape[:2]
	fs = face_recognition.face_landmarks(face_recognition.load_image_file(path+p))[0]
	w=fs['right_eye'][2][0] - fs['left_eye'][1][0]
	h=fs['nose_tip'][2][1] - fs['right_eye'][2][1]
	io.imsave('result/'+p+'.png',image[max(0,fs['right_eye'][2][1]-int(h*2.9)):min(he,fs['nose_tip'][2][1]+int(h*2.8)),max(0,fs['left_eye'][1][0]-int(w*2)):min(wi,fs['right_eye'][2][0]+int(w*2)),:])
  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值