如何解决发生异常:TypeError无法解包不可迭代的int对象图像处理
我收到此错误,我不知道如何解决它,看来我必须将错误行放在元组中,这是因为numpy库对于此行“ width,height = detected_image.size”,任何建议?
import cv2
import csv
from PIL import Image
import numpy as np
import os
import sys
def create_data_list(myDir,format='.jpg'):
data_list = []
print(myDir)
for root,dirs,files in os.walk(myDir,topdown=False):
for name in files:
if name.endswith(format):
fullName = os.path.join(root,name)
data_list.append(fullName)
return data_list
raw_data = create_data_list('./faces')
face_detection_cascade = cv2.CascadeClassifier('face_cascade.xml')
for each_image in raw_data:
image_file = Image.open(each_image)
each_file_path = image_file.filename
image = cv2.imread(each_file_path)
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face_detecting = face_detection_cascade.detectMultiScale(gray_image,scaleFactor=1.05,minNeighbors=5)
for x,y,w,h in face_detecting:
detected_image = cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)
print(type(detected_image))
width,height = detected_image.size
format = detected_imageformat
mode = detected_image.mode
img_grey = detected_image.convert('L')
value = np.asarray(img_grey.getdata(),dtype=np.int).reshape((img_grey.size[1],img_grey.size[0]))
value = value.flatten()
print(value)
with open("img_csv_data.csv",'a') as f:
writer = csv.writer(f)
writer.writerow(value)
resized = cv2.resize(image,(int(image.shape[1]*2),int(image.shape[0]*2)))
cv2.imshow('human_face',resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
解决方法
它刚刚解决了 需要添加以下内容:
image_from_array = Image.fromarray(detected_image)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。