python 人像分割问题 foreground
失误的泡泡 发布于2019-05 浏览:7573 回复:3
0
收藏

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
个赞
共3条回复 最后由zhaomeng258回复于2022-10
#4zhaomeng258回复于2022-10
#2 用户已被禁言回复
你可以试试 [代码]

谢谢

1
#3爱罗堂回复于2019-09

顶上去同步,二值图 和 灰度度 都没有问题,可前景透明图就是出不来

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的代码

0
#2用户已被禁言回复于2019-05

你可以试试

imageData = base64.b64decode(res['foreground'])

file = open('ai.png','wb')
file.write(imageData)
file.close()
5
TOP
切换版块