Ask Your Question
1

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

asked 2016-10-22 00:15:51 -0600

ALOMGEER gravatar image

updated 2016-10-22 01:08:01 -0600

berak gravatar image

using:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

i get:

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

here's the whole code:

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(1)

while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectange(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xFF
    if k == 27:
        break
cap.release()
cv2.destroyALLWindows()
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
0

answered 2016-10-22 01:14:19 -0600

berak gravatar image

updated 2016-10-22 01:25:58 -0600

your img is most likely invalid.

some things to check:

  1. did the capture open ? is 1 the correct id ? (i doubt so, rather try 0). please check:

    cap.isOpened() #should return True

  2. did you get a valid frame from your capture ? please check if ret==True

    (older webcams might need some "warmup", and you might need to continue, until you receive a valid image)

in general, please make it a habit , to always check, if resources got read/loaded, like from imread(), and also you need to check, if the cascade xml files get ead properly with cascade.empty()

edit flag offensive delete link more

Comments

as berak said you need to add a delay before enter the while loop import numpy as np import cv2 import time

cap = cv2.VideoCapture(0) print(cap.isOpened())

time.sleep(5) while(True): # Capture frame-by-frame ret, frame = cap.read() print(ret)

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

When everything done, release the capture

cap.release() cv2.destroyAllWindows()

VonFausto gravatar imageVonFausto ( 2017-07-21 17:14:15 -0600 )edit

^^ no , i never said that

also, sleeping will give you a problem, as input images get stuck in the internal queue. (latency)

berak gravatar imageberak ( 2017-07-21 17:58:12 -0600 )edit
0

answered 2016-10-30 02:05:55 -0600

xiaoerlaigeid gravatar image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

these two is wrong. you should add the path .

 face_cascade =   
 cv2.CascadeClassifier(r'D:\OpenCV\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
 eye_cascade = cv2.CascadeClassifier(r'D:\OpenCV\opencv\sources\data\haarcascades\haarcascade_eye.xml')
edit flag offensive delete link more
0

answered 2017-09-13 12:15:39 -0600

I had the same issue and I solved this error by changing the address of the camera from

cap = cv2.VideoCapture(1)

to : cap = cv2.VideoCapture(0)

edit flag offensive delete link more

Comments

1

yea, but probably only because you never checked, if the capture could actually open.

berak gravatar imageberak ( 2017-09-13 12:26:05 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-10-22 00:15:51 -0600

Seen: 21,373 times

Last updated: Sep 13 '17