Page 1 of 1

OpenCV with Facial Recognition

Posted: Fri Jun 16, 2017 2:54 pm
by Ralph797
Hey, 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 these two methods on a raspberry pi3 modal B- ... -opencv-3/ ... pberry-pi/

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 "", line 11, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: /build/opencv-ISmtkH/opencv-, line 3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

Then here's the file done in python 3 (3.4.2) [also tried in python 2 (2.7.9)]-

import numpy as np
import cv2

# multiple cascades: ... arcascades

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

cap = cv2.VideoCapture(0)

while 1:
ret, img =
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 a checked to make sure the Haar Cascade 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 s 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 picamera 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.

Re: OpenCV with Facial Recognition

Posted: Sun Jul 09, 2017 12:53 am
by profbit
got the same issue before.
just make sure you're the haarcascade path is correct