参考博客
参考博客
我也是小白只是终于解决了这个问题,欢迎大佬指正错误
我也是查了几篇博客才找到的,希望看到这篇博客的你可以先仔细阅读百度云的文档
第一部分是百度云的文档就给出的一些东西,这些注册一个账号然后创建一个用于人像分割的实例就会分给你的
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);
""" 如果有可选参数 """
options = {}
options["type"] = "labelmap"
""" 带参数调用人像分割 """
client.bodySeg(image, options)
其中client.bodySeg(image)的返回值为:
{
“log_id”: ''xxx",
“labelmap”: “xxxx”,
“scoremap”: “xxxx”,
“foreground”: “xxxx”
}
从labelmap获取图片的方法为:
seg_res = client.bodySeg(image)
labelmap = base64.b64decode(seg_res['labelmap'])
nparr = np.frombuffer(labelmap, np.uint8)
labelimg = cv2.imdecode(nparr,1)
new_img = np.where(labelimg==1, 255, labelimg)
output_path = os.path.join('要保存的路径', "名字.jpg")
cv2.imwrite(output_path, new_img)
从labelmap获取图片的方法为:
labelmap = base64.b64decode(seg_res['scoremap'])
with open('路径/scoremap.jpg', 'wb') as fp:
fp.write(labelmap)
其实这个我也感到很疑惑,文档中说“不用进行二次处理,直接解码保存图片即可。”但我第一次用的和labelmap一样的方法处理也得到了一张类似的,和上述方法得到的不太一样,不过很像文档中给出的结果,大家可以试一试。
从foreground获取图片的方法同上:
labelmap = base64.b64decode(seg_res['foreground'])
with open('D:/bs/python/photo/foreground.jpg', 'wb') as fp:
fp.write(labelmap)
我也是一开始用的和labelmap 一样的方式,发现得到的似乎是原图很神奇,不过最后终于知道怎么解了