Ralph797
Posts: 1
Joined: Fri Jun 16, 2017 2:16 pm

OpenCV with Facial Recognition

Fri Jun 16, 2017 2:54 pm

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-
http://www.pyimagesearch.com/2016/04/18 ... -opencv-3/
https://robertcastle.com/2014/02/instal ... 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-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)]-

import numpy as np
import cv2

# multiple cascades: https://github.com/Itseez/opencv/tree/m ... arcascades

#https://github.com/Itseez/opencv/blob/m ... efault.xml
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 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.

profbit
Posts: 45
Joined: Fri May 05, 2017 4:34 am

Re: OpenCV with Facial Recognition

Sun Jul 09, 2017 12:53 am

got the same issue before.
just make sure you're the haarcascade path is correct

Return to “Troubleshooting”