cv2.error:函数setSize

时间:2017-04-22 09:59:39

标签: python opencv raspberry-pi raspberry-pi3

我运行代码时遇到此错误。我试图从带有覆盆子pi的网络摄像头中捕捉到一张照片但是有一段时间我抓到的第一张照片是空的。所以我像这样验证它

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

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

  

损坏的JPEG数据:标记0xd1 OpenCV之前的1个无关字节   错误:setSize文件中的断言失败(s> = 0)   /home/pi/opencv-3.2.0/modules/core/src/matrix.cpp,第307行Traceback   (最近的呼叫最后一次):文件" total.py",第248行,in       facialReco(目录)文件" total.py",第236行,在facialReco中       id,dist,it = reco(faceDetec)File" total.py",188行,in reco       recognizer.predict_collect(灰色[yHMax:yHMax + hMax,xHMax:xHMax + wHMax],收集器)

     

cv2.error:/home/pi/opencv-3.2.0/modules/core/src/matrix.cpp:307:   错误:函数setSize

中的(-215)s> = 0

我的整个代码是:

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 个答案:

答案 0 :(得分:2)

我设法通过添加以下内容来纠正错误:&#39;如果不是img就是无:&#39;

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