I get this error while I run my code. I try to catch a picture from a webcam with a raspberry pi but some time the first picture that I catch is empty. So I verify it like this
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 /home/pi/opencv-3.2.0/modules/core/src/matrix.cpp, line 307 Traceback (most recent 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 recognizer.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 function setSize
My whole code is:
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
I manage to correct 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With