[英]OpenCv showing error while resizing written image
def face_cropped(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray,1.3,5)
#scaling factor = 1.3
#minimum neighbor = 5
for (x,y,w,y) in faces:
face_cropped = img[y:y+h,x:x+w]
return face_cropped
cap = cv2.VideoCapture(0)
img_id = 0
while True:
ret,my_frame = cap.read()
if face_cropped(my_frame) is not None:
img_id+=1
face = cv2.resize(face_cropped(my_frame),(450,450))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
file_name_path = "data/user."+str(id)+"."+str(img_id)+".jpg"
cv2.imwrite(file_name_path,face)
cv2.putText(face, str(img_id),(50,50), cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),2)
cv2.imshow("Cropped Face", face)
if cv2.waitKey(0) == 13 or int(img_id) == 100:
break
cap.release()
cv2.destroyAllWindows()
messagebox.showinfo("Result","Generating dataset completed!")
except Exception as es:
messagebox.showerror("Error",f"Due to:{str(es)}",parent=self.root)
_得到这个错误
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\resize.cpp:4051: 错误: (-215 :断言失败).ssize:empty() 在 function 'cv::resize'
这里你重复了两次“y”而不是指定“h”
for (x,y,w,y) in faces:
face_cropped = img[y:y+h,x:x+w]
return face_cropped
那么如果 face_classifier 没有找到任何人脸,你会返回什么? 你应该检查 faces 是否为空,在这种情况下你不能调整不存在的东西的大小。
事实上,如果你的代码找到了它工作的面孔,我只是为了测试目的而放置了一些成本值(x,y,w,h)并且调整大小没有问题:
import cv2
def face_cropped(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = [(100,150,234,145)]
#scaling factor = 1.3
#minimum neighbor = 5
for (x,y,w,h) in faces:
face_cropped = img[y:y+h,x:x+w]
return face_cropped
cap = cv2.VideoCapture(0)
img_id = 0
while True:
ret,my_frame = cap.read()
if face_cropped(my_frame) is not None:
img_id+=1
# else:
# continue # you can add this else statement for skipping "empty frames"
face = cv2.resize(face_cropped(my_frame),(450,450))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
file_name_path = "data/user."+str(id)+"."+str(img_id)+".jpg"
cv2.imwrite(file_name_path,face)
cv2.putText(face, str(img_id),(50,50), cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),2)
cv2.imshow("Cropped Face", face)
if cv2.waitKey(0) == 13 or int(img_id) == 100:
break
cap.release()
cv2.destroyAllWindows()
这个问题可能是由于有时输入到调整大小 function 的 object,这应该是一个框架,是无。 您可以通过添加print(type(face_cropped(my_frame)))
检查它是什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.