百度AI图像处理—人体分析(人像切割—AI抠图)调用教程(基于Python3-附Demo)

首先来看一下识别的效果:这里需要完整代码以及SDK的请点击此处下载:百度图像处理人像分割

首先需要注册百度账号并且创建对应的应用,这里具体方法如图:

访问:http://ai.baidu.com/  点击控制台

登录后创建应用:

此处注意:图像识别中的各项功能共用的是一个SDK包,只是不同功能实现的时候使用的函数以及返回参数不同,点击完创建应用后就可以生成三个我们后期识别过程中必须使用的参数:AppID,API Key和secert key,这里我们可以点击查看应用详情来获取

 

至此,前期的准备工作就完成了,这时我们通过Pip或者官网直接下载SDK包,pip下载指令为:

这里支持Python版本:2.7.+ ,3.+

如果已安装pip,执行pip install baidu-aip即可。
如果已安装setuptools,执行python setup.py install即可。

接下来,在下载的SDK文档下新建Python文件,当然你也可以使用导入包的模式:

然后创建一个AipBodyAnalysis(亦可以简单的理解为一个和百度的一个连接),这里代码为:

from aip import AipBodyAnalysis

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_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()

image = get_file_content('example.jpg')

""" 调用人像分割 """
client.bodySeg(image);

 注意,调用完成后,我们还需要进行二次处理才能获取到我们需要的图像(二值图,前景图),我们执行人像分割的调用后函数的返回为:

{
    "log_id": 716033439,
    "labelmap": "xxxx",
	"scoremap": "xxxx",
	"foreground": "xxxx"
}

这里,各个返回值的含义为:

字段是否必选类型说明
labelmapstring分割结果图片,base64编码之后的二值图像,需二次处理方能查看分割效果
scoremapstring分割后人像前景的scoremap,归一到0-255。Base64编码后的灰度图文件,图片中每个像素点的灰度值 = 置信度 * 255,置信度为原图对应像素点位于人体轮廓内的置信度,取值范围[0, 1]
foregroundstring分割后的人像前景抠图,透明背景,Base64编码后的png格式图片。将置信度大于0.5的像素抠出来,并通过image matting技术消除锯齿
log_idint64唯一的log id,用于问题定位

 接下来是博主实现的核心代码:

from aip import AipBodyAnalysis
import cv2
import numpy as np
import base64
import os

import json
""" 你的 APPID AK SK """
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()

image = get_file_content('test.png')

""" 调用人像分割 """
client.bodySeg(image);

res = client.bodySeg(image)

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

nparr_foreground = np.fromstring(foreground,np.uint8)
foregroundimg = cv2.imdecode(nparr_foreground,1)
foregroundimg = cv2.resize(foregroundimg,(512,512),interpolation=cv2.INTER_NEAREST)
im_new_foreground = np.where(foregroundimg==1, 10, foregroundimg)
cv2.imwrite('foreground.png', im_new_foreground)

nparr_labelmap = np.fromstring(labelmap,np.uint8)
labelmapimg = cv2.imdecode(nparr_labelmap,1)
labelmapimg = cv2.resize(labelmapimg,(512,512),interpolation=cv2.INTER_NEAREST)
im_new_labelmapimg = np.where(labelmapimg==1, 255, labelmapimg)
cv2.imwrite('labelmap.png', im_new_labelmapimg)

nparr_scoremap = np.fromstring(scoremap,np.uint8)
scoremapimg = cv2.imdecode(nparr_scoremap,1)
scoremapimg = cv2.resize(scoremapimg,(512,512),interpolation=cv2.INTER_NEAREST)
im_new_scoremapimg = np.where(scoremapimg==1, 255, scoremapimg)
cv2.imwrite('scoremap.png', im_new_scoremapimg)

 

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值