问题描述
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
//
我遇到此错误,请解决此错误.预先感谢和爱;) 追溯(最近一次通话): 在第17行的"C:\ Users \ Saddaqat \ Desktop \疲劳检测代码"文件中 灰色= cv2.cvtColor(cur,cv2.COLOR_BGR2GRAY) 错误:........ \ opencv \ modules \ imgproc \ src \ color.cpp:3739:错误:(-215)scn == 3 ||函数cv :: cvtColor中的scn == 4
I am facing this error Kindly resolve this error . thanks and love in advance ;) Traceback (most recent call last): File "C:\Users\Saddaqat\Desktop\fatigue detection code", line 17, in gray = cv2.cvtColor(cur, cv2.COLOR_BGR2GRAY) error: ........\opencv\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
推荐答案
对我来说,VideoCapture无法正常工作.阅读图像后,您应该检查图像是否可以阅读:
Well, for me it looks like the VideoCapture is not working. You should check after reading the image, if it could have read something:
ret, cur = cam.read()
if not ret:
print "VideoCapture read no frame."
break
在这种情况下,SO中有一些答案可能会对您有所帮助,例如:基本上,他们说您可能对ffmpeg有问题.也许您必须将其添加到Windows路径,和/或重命名以使其具有OpenCV版本.
If that's the case, there has been some answers here in SO that might help you, for example: OpenCV 2.4 VideoCapture not working on WindowsBasically, they say that you might have problems with the ffmpeg. Maybe you have to add it to Windows path, and/or rename it to have the OpenCV version on it.
这篇关于Python-Opencv错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!