import numpy as np
import cv2
import thread, winsound
face_cascade = cv2.CascadeClassifier('C:\Users\Saddaqat\Desktop\Softwares\opencv\build\share\OpenCV\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\Users\Saddaqat\Desktop\Softwares\opencv\build\share\OpenCV\haarcascades\haarcascade_eye.xml')
def beep():
for i in xrange(4):
winsound.Beep(1500, 250)
cam = cv2.VideoCapture(0)
count = 0
iters = 0
while(True):
ret, cur = cam.read()
gray = cv2.cvtColor(cur, cv2.COLOR_BGR2GRAY)
faces = face
_cascade.detectMultiScale(gray,scaleFactor = 1.1, minNeighbors=1, minSize=(10,10))
for (x,y,w,h) in faces:
#cv2.rectangle(cur,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_color = cur[y:y+h,x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
if len(eyes) == 0:
print "Eyes closed"
else:
print "Eyes open"
count += len(eyes)
iters += 1
if iters == 2:
iters = 0
if count == 0:
print "Drowsiness Detected!!!"
thread.start_new_thread(beep,())
count = 0
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh), (0,255,0),2)
cv2.imshow('frame', cur)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
//
我正面临此错误请妥善解决此错误。提前感谢和爱;) Traceback(最近一次调用最后一次): 文件" C:\ Users \ Saddaqat \ Desktop \疲劳检测代码",第17行,in gray = cv2.cvtColor(cur,cv2.COLOR_BGR2GRAY) 错误:........ \ opencv \ modules \ imgproc \ src \ color.cpp:3739:错误:(-215)scn == 3 ||函数cv :: cvtColor中的scn == 4
答案 0 :(得分:1)
好吧,对我来说,看起来VideoCapture无效。您应该在阅读图像后检查是否可以阅读:
ret, cur = cam.read()
if not ret:
print "VideoCapture read no frame."
break
如果是这种情况,那么SO中有一些可能对您有帮助的答案,例如:OpenCV 2.4 VideoCapture not working on Windows 基本上,他们说你可能有ffmpeg的问题。也许你必须将它添加到Windows路径,和/或重命名它以在其上安装OpenCV版本。