程序报错 labelmap = base64.b64decode(seg_res['labelmap'])
KeyError: 'labelmap'
这是我的·原程序代码
APP_ID = '26266506'
API_KEY = 'phTmMGYkxonUV9duhZqp2GFj'
SECRET_KEY = 'qz3sRYcWbz0GaeKGgPnGFC1rtxQopjSH'
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
path = 'E:/pycharm_xiangmu/pictures/mask_img/'
img_files = os.listdir('E:/pycharm_xiangmu/pictures/')
print(img_files)
for num in range(88, len(img_files) + 1):
# 按顺序构造出图片路径
img = f'E:/pycharm_xiangmu/pictures/img_{num}.jpg'
img1 = cv2.imread(img)
height, width, _ = img1.shape
# print(height, width)
# 二进制方式读取图片
with open(img, 'rb') as fp:
img_info = fp.read()
# 设置只返回前景 也就是分割出来的人像
seg_res = client.bodySeg(img_info)
labelmap = base64.b64decode(seg_res['labelmap'])
nparr = np.frombuffer(labelmap, np.uint8)
labelimg = cv2.imdecode(nparr, 1)
labelimg = cv2.resize(labelimg, (width, height), interpolation=cv2.INTER_NEAREST)
new_img = np.where(labelimg == 1, 255, labelimg)
mask_name = path + 'mask_{}.png'.format(num)
# 保存分割出来的人像
cv2.imwrite(mask_name, new_img)
print(f'======== 第{num}张图像分割完成 ========')
time.sleep(random.randint(1, 2))