Ask Your Question
0

How to solve this error ?error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

asked 2017-04-07 16:26:18 -0500

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() 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 (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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-07 23:36:26 -0500

berak gravatar image

typical n00b error, you forgot to check:

  • if your capture opened: cam.isOpened()
  • if you actually read an image from it: ret==True

so, cv2.cvtColor encountered an empty image, and thew an exception.

edit flag offensive delete link more

Comments

also check channel of image

sabra gravatar imagesabra ( 2017-04-08 03:16:09 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-07 16:24:14 -0500

Seen: 412 times

Last updated: Apr 07 '17