How can I make an OpenCV face detection for Python 2.7 on a Mac. I have tried many different codes but they all don't work. I am running them in terminal.
I get this error:
cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
This seems to be right but I can't figure out what the error is. Here is the code for the face detection.
import numpy as np
import cv2
faceCascade =
cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while True:
ret, img = cap.read()
img = cv2.flip(img, -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(20, 20)
)
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('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
Your error message indicates that the file path may be invalid, the program can't find .xml file. And you may want to replace 'Cascades/haarcascade_frontalface_default.xml' with a absolute path such as 'username/file/../cascade.xml'
Related
I am trying to run this OpenCV code to detect faces with my video camera. It's giving me this error whenever I run my code. The light on my video camera blinks but then shuts down with this error in the console box along with this one cv2.error: OpenCV(4.5.1) error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Here's the code
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
#THIS IS THE ERROR AREA
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Release the VideoCapture object
cap.release()
It seems your script is not able to find the haarcascade_frontalface_default.xml file properly because of relative path. Try to give an absolute path and check.
Im using opencv to detect faces in my webcam and whenever a face is detected I want to crop the ROI of face and save its image locally on my system. But when I run the code it doesnt do anything at all.
My webcam is working in opencv(which Ive already tested). But this specific code isnt working, for unknown reasons.
import os
import cv2
ctr=0
# import face detection cascade
face_cascade = cv2.CascadeClassifier('/home/opi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml')
# create capture object
cap = cv2.VideoCapture(0)
while(True):
# capture frame-by-frame
ret, img = cap.read()
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(img, 1.3, 5)
# for each face draw a rectangle around and copy the face
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]
roi_gray = gray[y:y+h, x:x+w]
#SAVE THE DETECTED FACES LOCALLY
roi_gray=cv2.resize(roi_gray,(100,100))
cv2.imwrite('faces'+'/'+str(ctr)+'.jpg',roi_gray)
ctr+=1
# display the resulting frame
cv2.imshow('frame',img)
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
When I kill the code pressing Ctrl+C, I can see this error which also doesnt make much sense :-
opi#admin:~$ python input.py
^CTraceback (most recent call last):
File "input.py", line 18, in <module>
faces = face_cascade.detectMultiScale(img, 1.3, 5)
KeyboardInterrupt
You have forgotten to add cv2.waitKey(1) and that is why your code crashes while it is in an infinite loop ( while True: ... ). We can also say that there is no time for showing(refreshing) frames ...
Add this block of code:
# display the resulting frame
cv2.imshow('frame',img)
key = cv2.waitKey(1)
if key == 32: # if the space key is pressed
break
The final code :
import os
import cv2
ctr=0
# import face detection cascade
face_cascade = cv2.CascadeClassifier('/home/opi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml')
# create capture object
cap = cv2.VideoCapture(0)
while True:
# capture frame-by-frame
ret, img = cap.read()
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(img, 1.3, 5)
# for each face draw a rectangle around and copy the face
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]
roi_gray = gray[y:y+h, x:x+w]
#SAVE THE DETECTED FACES LOCALLY
roi_gray=cv2.resize(roi_gray,(100,100))
cv2.imwrite('faces'+'/'+str(ctr)+'.jpg',roi_gray)
ctr+=1
# display the resulting frame
cv2.imshow('frame',img)
key = cv2.waitKey(1)
if key == 32: # if space key is pressed
break
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Here is my code :
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascase_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while True:
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]
eye = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eye:
cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2 )
cv2.imshow('img',img)
k= cv2.waitKey(30) & 0xff
if k == 27:``
break
cap.release()
cap.destroyAllWindows()
These are the errors I'm getting:
error: OpenCV(4.2.0)
C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182:
error: (-215:Assertion failed) !_src.empty() in function
'cv::cvtColor'
error Traceback (most recent call
last) in
3 while True:
4 ret, img = cap.read()
----> 5 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
6 faces = face_cascade.detectMultiScale(gray,1.3,5)
7 for (x,y,w,h) in faces:
error: OpenCV(4.2.0)
C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182:
error: (-215:Assertion failed) !_src.empty() in function
'cv::cvtColor'
in your code have 3 little errors:
1= if k == 27:``` for solve first that delete `` 2=cap.destroyAllWindows()for solve secound, typecv2.destroyAllWindows() 3=('haarcascase_frontalface_default.xml')for solve third ,just type thishaarcascade_frontalface_default.xml`
you type cascase but that is cascade.
There could be numerous reasons as to why it doesn't work (eg. some OS will require that you grant permission to your term to access the webcam), however there's a couple of issues with your code:
start of the while loop: you don't check the value of the parameter ret. It's good practice to check its value with a small check like if not ret: break to exit the loop. That won't help with your webcam not working, but that will prevent the error that you are seeing (retis False, so img is likely empty and had no data)
End of your code. destroyAllWindows is a method in the cv2 namespace, not a property of your capture. You should call cv2.destroyAllWindows() instead of cap.destroyAllWindows()
I'm currently trying to run a simple python program to detect face and eyes.
I downloaded the appropriate classifiers and they're in the same directory. I even checked my camera settings and it's set to on with permission to python. Any ideas how to solve this?
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while True:
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.rectange(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]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,ehh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,0), 2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Why does this OpenCV program give the error:
My camera settings show that python is using it:
Thank you!
!_src.empty() means you have an empty frame
You can check the frame to make sure you actually get the image:
ret, img = cap.read()
if img is not None:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else: < implement how you want to deal with empty frame >
I have tried running this code for the OpenCV face detection and upon running it I get the error (AttributeError: module 'cv2.cv2' has no attribute 'CascadeClassifer') Is there some sort of new code I need to use?
Error is on line 4 (face_cascade = cv2.CascadeClassifer('haarcascade_frontalface_default.xml')
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifer('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifer('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = 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]
eye = 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('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destoryAllWindows()
Nevermind, I have fixed the code if anyone is interested.
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.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]
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('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Assuming you want to load the classifier from a file.
Use the correct spellings
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
not
face_cascade = cv2.CascadeClassifer('haarcascade_frontalface_default.xml')
same error for me, I've solved this error by downgrading the openCV version.
NOTE: The downgrading approach is not something I would recommend or use, but since i was unable to find a fix, downgrading to previous version helped my case.
Use pip install opencv-python== to get a list of openCV versions.
Try openCV versions below 4
first uninstall using pip uninstall opencv-python
then Install using pip install opencv-python==
EDIT: Consider downgrading as your last resort, if all other methods wont fix.