-
-
Save radames/effc25355c194bd23bbd1d0cbf87d449 to your computer and use it in GitHub Desktop.
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() |
Nice work! Thank you.
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
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
Nice thanks yr
I am getting an error like this..
"SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set"?
Guyzz pls help me......
Thanks @eduarte , copying the folder solved the problem
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?
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'
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
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')
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'
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.
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!!!!!!!!
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
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
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
@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)
help me!!! present this error
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
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
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'
Please help me to solve the problem
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")
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'
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")
Yes, working now. Thanks!
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'
hi @Mariela1817 maybe you can try this
pip install opencv-contrib-python
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
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