求助!调用百度API人像切割怎么图片保存不了?

本文讲述了作者尝试使用Python和百度AI的AipBodyAnalysis进行人像分割,但在保存处理后的图像时遇到OpenCV错误。主要关注cv2.imwrite函数的问题及其解决方案。
摘要由CSDN通过智能技术生成
import cv2
import base64
import numpy as np
import os
from aip import AipBodyAnalysis

APP_ID = "88888888"
API_KEY = '888888888888888'
SECRET_KEY = '888888888888888888888'

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
# 文件夹
jpg_file = os.listdir("D:\\DancingCat\\images")
# 要保存的文件夹
for i in jpg_file:
    open_file = os.path.join("D:\\DancingCat\\images",i)
    save_file = os.path.join("D:\\DancingCat\\save_images",i)
    if not os.path.exists(save_file):#文件不存在时,进行下步操作
        img = cv2.imread(open_file)  # 获取图像尺寸
        height, width, _ = img.shape
        if "D:\\DancingCat\\crop_images":# 若Crop_path 不为 None,则不进行裁剪
            crop_file = os.path.join("D:\\DancingCat\\crop_images",i)
            img = img[100:-1,300:-400] #图片太大,对图像进行裁剪里面参数根据自己情况设定
            cv2.imwrite(crop_file,img)
            image= get_file_content(crop_file)
        else:
            image = get_file_content(open_file)
        res = client.bodySeg(image)#调用百度API 对人像进行分割
        labelmap = base64.b64decode(res['labelmap'])
        labelimg = np.frombuffer(labelmap,np.uint8)# 转化为np数组 0-255
        labelimg = cv2.imdecode(labelimg,1)
        labelimg = cv2.resize(labelimg,(width,height),interpolation=cv2.INTER_NEAREST)
        img_new = np.where(labelimg==1,255,labelimg)# 将 1 转化为 255
        cv2.imwrite(save_file,img_new)
        print(save_file,'save successfully')

报错如下:

D:\PYTHON\python.exe D:/BOX/可视化/动态词云图/dance_cat.py 
Traceback (most recent call last):
  File "D:\BOX\可视化\动态词云图\dance_cat.py", line 24, in <module>
    cv2.imwrite(crop_file,img)
cv2.error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:786: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'


进程已结束,退出代码1

我提前创建了这几个路径,我的images文件夹是有图片的

但是其它处理后的文件夹没有保存成功图片,换了几个代码,都是cv2.imwrite(crop_file,img)这有错,请问我该咋办捏

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值