python抠图_使用 python 人像抠图

python 代码:

from aip import AipBodyAnalysis

APP_ID = '***';

API_KEY = '***';

SECRET_KEY = '***';

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

def get_file_content(filePath):

with open(filePath, 'rb') as fp:

return fp.read()

file = sys.argv[1]

real = cv2.imread(file)

height, width, channels = real.shape

maskfile = 'mask.'+file

resfile = 'res.'+file

yuantu = get_file_content(file)

res = client.bodySeg(yuantu);

labelmap = base64.b64decode(res['labelmap'])

nparr = np.fromstring(labelmap,np.uint8)

labelimg = cv2.imdecode(nparr,1)

#labelimg = cv2.resize(labelimg,(402,402),interpolation=cv2.INTER_NEAREST)

labelimg = cv2.resize(labelimg,(width,height),interpolation=cv2.INTER_NEAREST)

im_new = np.where(labelimg==1, 255, labelimg)

cv2.imwrite(maskfile, im_new)

#add merge

result = cv2.bitwise_and(real, im_new)

cv2.imwrite(resfile, result)

原图:

4FDBD33CC7554307AB6C8861A4E64A08

二值图:

760AF2E27E454F3BBF4EE5B0B32EE6D7

最终图:

EFBDAF148D1E443D8E8693C66639D3C2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值