Skip to content

Instantly share code, notes, and snippets.

@radames
Last active January 5, 2021 14:25
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save radames/effc25355c194bd23bbd1d0cbf87d449 to your computer and use it in GitHub Desktop.
Save radames/effc25355c194bd23bbd1d0cbf87d449 to your computer and use it in GitHub Desktop.
Example of Python with Opencv and camera face detection - repo complete https://github.com/radames/python_opencv_camera_haar
import cv2
cap = cv2.VideoCapture(0)
cap.set(3, 640) # WIDTH
cap.set(4, 480) # HEIGHT
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
# Display the resulting frame
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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('frame', frame)
k = cv2.waitKey(1)
if k == ord('q') or k == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
@rahul6612
Copy link

i am getting this error
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale please solve it

@shyam3089
Copy link

Nice work! Thank you.

@eduarte
Copy link

eduarte commented Nov 14, 2017

I got the same error and what I did to solve it was to point the following lines:

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

To the location of the classifiers, in this case I have copied the folder "haarcascades" from:
(folder where you have extracted opencv)/OPENCV/Build/etc

To the folder where I have executed the code and it worked for me

@radames
Copy link
Author

radames commented Feb 11, 2018

hi @eduarte, @rahul6612 Just make sure you have the correct haarcascades path, if you don't have those files you can download from here https://github.com/opencv/opencv/tree/master/data/haarcascades

@asimuaf41
Copy link

Nice thanks yr

@sowri1
Copy link

sowri1 commented Apr 5, 2018

I am getting an error like this..
"SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set"?
Guyzz pls help me......

@Talha-T
Copy link

Talha-T commented May 17, 2018

Thanks @eduarte , copying the folder solved the problem

@socratesk
Copy link

Hi,

Have you ever tried deploying this in a server environment? I tried to do that in Heroku with Flask but could not make it work. Any help?

@Harshitgaur98
Copy link

Hi,
Please solve this error
cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

@Tarun3679
Copy link

hi @Harshitgaur98,@sowri1 I faced the same problem. First you will have to give the correct path of the file to the system, which looks like this:/home/xxxx/Desktop/Projects/haarcascade_eye.xml.
Next, you'll have to open the code on github and save the raw version of the code for it run.
Happy coding

@malperenhoroz
Copy link

Troubleshoot this error:
Write the actual location of the xml files.
Example:
face_cascade = cv2.CascadeClassifier('C:\python3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\python3\Lib\site-packages\cv2\data\haarcascade_eye.xml')

@mohit816
Copy link


error Traceback (most recent call last)
in ()
21
22 # convert to gray scale of each frames
---> 23 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
24
25 # Detects faces of different sizes in the input image

error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

@lakeshkhatri
Copy link

i am getting this error.
error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale.plese solve it.

@prashant00797
Copy link

Traceback (most recent call last):
File "D:/Python opencv/detect.py", line 15, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
error: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp:3783: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
help!!!!!!!!

@insung3511
Copy link

opencv ❯ python3 face_detection.py
[1]    11302 segmentation fault  python face_detection.py

Anyone plz HELP ME...
I'm develop in Mac OS X 10.14.6, Python 3.7.4, openCV 4.1.0

@insung3511
Copy link

Traceback (most recent call last):
File "D:/Python opencv/detect.py", line 15, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
error: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp:3783: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
help!!!!!!!!

@prashant00797, See this. I think there's someone with the same error as
https://stackoverflow.com/questions/30506126/open-cv-error-215-scn-3-scn-4-in-function-cvtcolor

@radames
Copy link
Author

radames commented Jul 29, 2019

folks, the assertions error
...cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale' is mostly because your now initiating the CascadeClassifier with the correct haarcascade path , for example os OSX using Brew and opencv you can:

face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/4.1.0_2/share/opencv4/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/4.1.0_2/share/opencv4/haarcascades/haarcascade_eye.xml')

or just download the xml files and put on the path of your script

@radames
Copy link
Author

radames commented Jul 29, 2019

@insung3511 I'm not sure about that. Make sure the input frame is coming with a correct format. You want to convert the colored RGB frame from the webcam to Gray scale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

@Pusak0816
Copy link

help me!!! present this error
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

@radames
Copy link
Author

radames commented Aug 13, 2020

help me!!! present this error
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

Hi @Pusak0816 this is probably an error with the path for the haarcascades, make sure you have it pointing to the right place

@Anirudh15376
Copy link

I am getting the following error
error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-q0nmoxxv\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

@Anirudh15376
Copy link

Please help me to solve the problem

@radames
Copy link
Author

radames commented Aug 26, 2020

hi @Anirudh15376 I'm not sure what the error coming from, but I've build a repository with a env in case you want to try.
https://github.com/radames/python_opencv_camera_haar

I think it could be the path for the haar files you can try modify the first lines to

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")

@Anirudh15376
Copy link

Anirudh15376 commented Aug 26, 2020 via email

@Rasika-Gulhane
Copy link

Why is this error? Please help

error Traceback (most recent call last)
in
----> 1 faces= face_classifier.detectMultiScale(gray, 1.3, 5) #location of feature on face

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-q0nmoxxv\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

@radames
Copy link
Author

radames commented Sep 9, 2020

Hi @Rasika-Gulhane the problem is the path for the haar cascades double check if it's correct, or use this new syntax

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")

@Rasika-Gulhane
Copy link

Yes, working now. Thanks!

@Mariela1817
Copy link

practica $ python guardardatos.py
Traceback (most recent call last):
File "guardardatos.py", line 9, in
faceClassif= cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
AttributeError: 'module' object has no attribute 'data'

@radames
Copy link
Author

radames commented Dec 22, 2020

hi @Mariela1817 maybe you can try this

pip install opencv-contrib-python

@bxsyia
Copy link

bxsyia commented Jan 5, 2021

hi i am getting this error:

mouth = mouth_cascade.detectMultiScale(roi_gray)
for (mx, my, mw, mh) in mouth:
cv2.rectangle(roi_color, (mx, my), (mx + mw, my + mh), (0, 0, 0), 2)

error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

and i have already used this code:
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_mouth.xml')

but its still not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment