cv2.error: (-215) s >= 0在函数setSize中

3

当我运行我的代码时,出现了这个错误。我试图使用树莓派从网络摄像头中捕获图片,但有时第一张捕获的图片是空的。因此,我进行了如下验证:

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

我该怎么做才能避免这个错误?

损坏的JPEG数据:标记0xd1之前有1个多余字节 OpenCV错误:在setSize函数中断言失败(s >= 0),文件/home/pi/opencv-3.2.0/modules/core/src/matrix.cpp,行307 追溯(最近的调用最先):在total.py的第248行,facialReco(directory) 在total.py的第236行,facialReco(faceDetec) 在total.py的第188行reco(gray[yHMax:yHMax + hMax, xHMax:xHMax + wHMax], collector) 中的recognizer.predict_collect函数

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

我的整个代码如下:

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
1个回答

2
我通过添加:'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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接