Cv2.error: (-215) s> = 0 in setSize function

I am getting this error while I am running my code. I am trying to catch an image from a webcam with a raspberry pi, but for a while the first photo I catch is blank. So I check it like

ret, img = cam.read();
        if not ret: continue

      

What can I do to avoid this error?

Corrupt JPEG data: 1 extraneous bytes before marker 0xd1 OpenCV Error: Assertion failed (s> = 0) in setSize file, file / home / pi / opencv -3.2.0 / modules / core / src / matrix.cpp, line 307 Traceback (last call last): File "total.py", line 248, in facialReco (directory) File "total.py", line 236, in facialReco id, dist, it = reco (faceDetec) File "total.py" , line 188, in reco recognition .predict_collect (gray [yHMax: yHMax + hMax, xHMax: xHMax + wHMax], collector)

cv2.error: /home/pi/opencv-3.2.0/modules/core/src/matrix.cpp: 307: error: (-215) s> = 0 in setSize function

All my code:

def reco(faceDetec):

    recognizer = cv2.face.createLBPHFaceRecognizer()
    recognizer.load("recognizer/trainingData_LBPHF.yml")
    id = 0
    it = 0
    dist = 0
    cam = cv2.VideoCapture(0)
    # prendre les rectangle ayant la plus grde largeur seulement.
    while it < 20:
        ret, img = cam.read();

        if not ret: continue

        cv2.imshow("Face", img);
        cv2.waitKey(1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = faceDetec.detectMultiScale(
            img,
            scaleFactor=1.2,
            minNeighbors=7,
            minSize=(50, 50)
            )

        hMax=0
        wHMax=0
        xHMax=0
        yHMax=0
        for (x, y, w, h) in faces:
            if h>hMax:
                hMax=h
                wHMax=w
                xHMax=x
                yHMax=y
        collector = cv2.face.StandardCollector_create()
        recognizer.predict_collect(gray[yHMax:yHMax + hMax, xHMax:xHMax + wHMax], collector)

        if collector.getMinDist()<65:
            it += 1
            dist = dist + collector.getMinDist()
            id = collector.getMinLabel()
            numberOfRec(id)
    cam.release()
    cv2.destroyAllWindows()
    req="SELECT studentId FROM Student WHERE numberOfRec=(SELECT MAX(numberOfRec) FROM Student);"
    cursor.execute(req)
    rows = cursor.fetchall()
    for row in rows:
        id=row[0]
    req="UPDATE Student SET numberOfRec = %(numberOfRec)"
    values = {"numberOfRec": 0}
    cursor.execute(req, values)
    db.commit()
    return id, dist, it

      

+3


source to share


1 answer


I managed to fix the error by adding: 'if not img is None:'



def reco(faceDetec):

    recognizer = cv2.face.createLBPHFaceRecognizer()
    recognizer.load("recognizer/trainingData_LBPHF.yml")
    id = 0
    it = 0
    dist = 0
    cam = cv2.VideoCapture(0)
    # prendre les rectangle ayant la plus grde largeur seulement.
    while it < 20:
        ret, img = cam.read();
        if not img is None: 
            if not ret: continue

                cv2.imshow("Face", img);
                cv2.waitKey(1)
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                faces = faceDetec.detectMultiScale(
                    img,
                    scaleFactor=1.2,
                    minNeighbors=7,
                    minSize=(50, 50)
                    )

                hMax=0
                wHMax=0
                xHMax=0
                yHMax=0
                for (x, y, w, h) in faces:
                    if h>hMax:
                        hMax=h
                        wHMax=w
                        xHMax=x
                        yHMax=y
                collector = cv2.face.StandardCollector_create()
                recognizer.predict_collect(gray[yHMax:yHMax + hMax,         xHMax:xHMax + wHMax], collector)

                if collector.getMinDist()<65:
                    it += 1
                    dist = dist + collector.getMinDist()
                    id = collector.getMinLabel()
                    numberOfRec(id)
    cam.release()
    cv2.destroyAllWindows()
    req="SELECT studentId FROM Student WHERE numberOfRec=(SELECT MAX(numberOfRec) FROM Student);"
    cursor.execute(req)
    rows = cursor.fetchall()
    for row in rows:
        id=row[0]
    req="UPDATE Student SET numberOfRec = %(numberOfRec)"
    values = {"numberOfRec": 0}
    cursor.execute(req, values)
    db.commit()
    return id, dist, it

      

+2


source







All Articles