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-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, 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-2.4.9.1+dfsg/modules/imgproc
/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:
cv2.rectangle(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]
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
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.