Python-Opencv error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

天涯浪子 提交于 2019-12-01 13:07:24

问题


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
//

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


回答1:


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

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 Windows Basically, 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.



来源:https://stackoverflow.com/questions/43287796/python-opencv-error-215-scn-3-scn-4-in-function-cvcvtcolor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!