简体   繁体   中英

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)

_ getting this error

cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed).ssize:empty() in function 'cv::resize'

Here you repeated twice "y" instead of specifying "h"

for (x,y,w,y) in faces:
    face_cropped = img[y:y+h,x:x+w]
    return face_cropped

then if face_classifier does not find any faces, what do you return? You should check if faces is empty, in that case you can not resize something that does not exist.

In fact if your code finds a face it works, i just putted some costants (x, y, w, h) for testing purpose and there's no problem with resize:

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()

This issue may be caused to the fact that sometimes the object in input to the resize function, which would be supposed to be a frame, is None. You can check what is it by adding print(type(face_cropped(my_frame)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM