img_file = 'timg.jpg'
ori_img = cv2.imread(img_file)
height, width = ori_img.shape[:2]
with open(img_file, 'rb') as fp:
img_info = fp.read()
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
client.bodySeg(img_info);
res = client.bodySeg(img_info)
foreground = base64.b64decode(res['foreground'])
nparr_foreground = np.frombuffer(foreground,np.uint8)
foregroundimg = cv2.imdecode(nparr_foreground,1)
cv2.imshow('0', foregroundimg)
很奇怪 labelmap 和 scoremap 就可以成功分割,foreground就不行,为什么?!?!。我就想要个png透明背景 怎么就那么难,求高人指点一二
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
谢谢
顶上去同步,二值图 和 灰度度 都没有问题,可前景透明图就是出不来
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)
问题出在哪????python的代码
你可以试试