Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of Python with Opencv and camera face detection
import cv2
cap = cv2.VideoCapture(0)
cap.set(3, 640) #WIDTH
cap.set(4, 480) #HEIGHT
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('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)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
@Abdulkereem

This comment has been minimized.

Copy link

Abdulkereem commented Oct 14, 2017

okay i wanna ask b4 testing is this a face detector or face recognition

@rahul6612

This comment has been minimized.

Copy link

rahul6612 commented Nov 5, 2017

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

This comment has been minimized.

Copy link

shyam3089 commented Nov 8, 2017

Nice work! Thank you.

@eduarte

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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

@kashiffff

This comment has been minimized.

Copy link

kashiffff commented Mar 29, 2018

Nice thanks yr

@sowri1

This comment has been minimized.

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

This comment has been minimized.

Copy link

Talha-T commented May 17, 2018

Thanks @eduarte , copying the folder solved the problem

@socratesk

This comment has been minimized.

Copy link

socratesk commented Jul 7, 2018

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

This comment has been minimized.

Copy link

Harshitgaur98 commented Oct 26, 2018

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

This comment has been minimized.

Copy link

Tarun3679 commented Nov 25, 2018

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

@AlperenHoroz

This comment has been minimized.

Copy link

AlperenHoroz commented Jan 22, 2019

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

This comment has been minimized.

Copy link

mohit816 commented Jun 13, 2019


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

This comment has been minimized.

Copy link

lakeshkhatri commented Jun 23, 2019

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

This comment has been minimized.

Copy link

prashant00797 commented Jul 20, 2019

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

This comment has been minimized.

Copy link

insung3511 commented Jul 27, 2019

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

This comment has been minimized.

Copy link

insung3511 commented Jul 27, 2019

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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

Pusak0816 commented Aug 6, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.