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)这有错,请问我该咋办捏