使用 python 人像抠图
wcf_first 发布于2018-10 浏览:7445 回复:3
3
收藏

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)

原图:

二值图:

最终图:

 

收藏
点赞
3
个赞
共3条回复 最后由wangwei8638回复于2019-05
#4wangwei8638回复于2019-05

说好的人像呢

0
#3kkive康康回复于2019-05

问一下怎么样才能把结果输出成图片呢??

 

0
#2melissayoung回复于2018-10

这个。。。。大猩猩表示很无辜啊

0
TOP
切换版块