Saddaqat
Saddaqat

Reputation: 33

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

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

Upvotes: 0

Views: 1900

Answers (2)

Miguel Angel
Miguel Angel

Reputation: 1

you can solve the error like this:

you need to change the line "name_image" for the name the image or video:

import os

script_dir = os.path.dirname(os.path.abspath(__file__))
imagen_path = os.path.join(script_dir, 'name_image')
imagen = cv2.imread(imagen_path)

For some reason the file won't find you if we don't give you the full address

Upvotes: 0

facug91
facug91

Reputation: 93

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.

Upvotes: 1

Related Questions