I'm completely new to the raspberry pi and am looking for help with this same error I've been trying to solve for the past few days. I completely reinstalled OpenCV twice now from two methods on a raspberry pi3 modal B

Many of the packages were out of date, so that may be my problem... I looked through many for the up-to-date ones and downloaded them but it only worked one day and now I'm back to where I started with the following error-

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file 
/build/opencv-ISmtkH/opencv-, line 3737
    Traceback (most recent call last):
     File "faceRecognition.py", line 11, in <module>
       gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     cv2.error: /build/opencv-ISmtkH/opencv-
/src/color.cpp, line 3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

Then here's the file faceRecognition.py done in python 3 (3.4.2) [also tried in python 2 (2.7.9)]:

# multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
# https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml

import numpy as np
import cv2

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

cap = cv2.VideoCapture(0)

while 1:
    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:
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

    k = cv2.waitKey(30) & 0xff
    if k == 27:


I reviewed and checked to make sure the Haarcascade worked, and it was connected. I did this just yesterday and the error had gone away and the program had been working fine. I shutdown the pi and had it sit out overnight. I plugged it in the following morning and that error came up again. the software I'm using by the way is installed from Noobs. I have tried changing the port for the pi camera to 1, but that was not the solution. I've looked though basically every forum and it seems nothing has worked for me, but I have suspicion that the pi camera is simply not loading the video into the correct format for the pi to correctly process the series of jpg images. If you could offer any assistance that would be great, thank you and let me know if you need any other information.

2 Answers 2


OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor Usually means that the image your are trying to convert is not in the expected color format.

As you are converting images from Color to Gray, your img should have a format like RBG, BGR or RGBA. These formats use 3 or 4 colors per pixels (as stated in the error message).

So you may want to save your image once captured and look for these 2 points:
- Is the image valid ? (0 sized frame may throw this error)
- Is the image already black and white ?


Tested your code and it works to detect front face. Suspected the problem might be caused by your Pi Camera. You may try below.

  1. Double check the Pi Camera FPC cable is well inserted.
  2. Change the while loop as while cap.isOpened(): for better camera status checking.
  3. Change video capture source to cv2.VideoCapture(-1) for using an active one.

Hope this help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.