运行代码时出现此错误。我尝试从具有树莓派的网络摄像头中捕获图片,但有时我捕获的第一张图片为空。所以我这样验证
ret, img = cam.read();
if not ret: continue
我该怎么做才能避免此错误?
我的整个代码是:
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
最佳答案
我设法通过添加以下内容来纠正错误:“如果不是img为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
关于python - cv2.error:(-215)s> = 0在函数setSize中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43557830/