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.
Related
This is my code:
import numpy as np
import cv2
import pickle
face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
eye_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_smile.xml')
[enter image description here][1]
recognizer = cv2.face.LBPHFaceRecognizer_create()
I'm getting #module 'cv2.cv2' has no attribute 'legacy' Error. How can I fix it? I'm using windows 10 and opencv 4.5.1 version. I tried to install pip install opencv_contrib_python, but it did not work.
recognizer.read("./recognizers/face-trainner.yml")
labels = {"person_name": 1}
with open("pickles/face-labels.pickle", 'rb') as f:
og_labels = pickle.load(f)
labels = {v:k for k,v in og_labels.items()}
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
#print(x,y,w,h)
roi_gray = gray[y:y+h, x:x+w] #(ycord_start, ycord_end)
roi_color = frame[y:y+h, x:x+w]
# recognize? deep learned model predict keras tensorflow pytorch scikit learn
id_, conf = recognizer.predict(roi_gray)
if conf>=4 and conf <= 85:
#print(5: #id_)
#print(labels[id_])
font = cv2.FONT_HERSHEY_SIMPLEX
name = labels[id_]
color = (255, 255, 255)
stroke = 2
cv2.putText(frame, name, (x,y), font, 1, color, stroke, cv2.LINE_AA)
img_item = "7.png"
cv2.imwrite(img_item, roi_color)
color = (255, 0, 0) #BGR 0-255
stroke = 2
end_cord_x = x + w
end_cord_y = y + h
cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
#subitems = smile_cascade.detectMultiScale(roi_gray)
#for (ex,ey,ew,eh) in subitems:
# cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I remember getting this error a while back.
In your CMD prompt run these three commands and then try to run your program again.
pip3 install opencv-python==4.4.0.46
pip3 install opencv-contrib-python==4.4.0.46
pip3 install opencv-contrib-python-headless==4.4.0.46
The version specifics may not be necessary but it's what worked last for me.
I want to check if a face has been detected.
I have the variable face_detect and when a face is detected I want to turn this variable to True however I don't know how to check for a detected face. I tried using faces.size() to check if it was greater than zero but it said
AttributeError: 'tuple' object has no attribute 'size'
So I don't know why that is not working.
import cv2
import numpy as np
import winsound
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
face_detect = False
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've slightly modified your code to update the face_detect variable to True whenever a face is detected.
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
face_detect = False
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) > 0:
face_detect = True
else:
face_detect = False
print(face_detect)
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'm trying to code a face recognition app using OpenCV and python on macOS 'Big Sur' and Pycharm, but unfortunately it does not show the image window/preview and it does not show any errors in console , below you can check code:
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('news.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray_img,
scaleFactor=1.05, minNeighbors=5)
for x, y, w, h in face:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
resized = cv2.resize(img, (int(img.shape[1]/3), int(img.shape[0]/3)))
# cv2.startWindowThread()
# cv2.namedWindow("preview")
cv2.imshow('preview', img)
cv2.waitKey(0)
cv2.destroyWindow('preview')
I have tried adding cv2.startWindowThread(), cv2.namedWindow("preview") and even installed headless by "pip3 install opencv-python-headless" but it does not work.
Replace the line face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
with face_cascade = cv2.CascadeClassifier(cv2.data.haarcascade + 'haarcascade_frontalface_default.xml')
and then it should work fine
Here's the final code :
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascade + 'haarcascade_frontalface_default.xml')
img = cv2.imread('news.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray_img,
scaleFactor=1.05, minNeighbors=5)
for x, y, w, h in face:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
resized = cv2.resize(img, (int(img.shape[1]/3), int(img.shape[0]/3)))
# cv2.startWindowThread()
# cv2.namedWindow("preview")
cv2.imshow('preview', img)
cv2.waitKey(0)
cv2.destroyWindow('preview')
Its seems the problem is related to macOS "Big Sur" framing system,
The problem can be solve by:
1- deleting the python env which I created for this particular project
2- deleting the "home brew" completely
3- created a new python3-env with python-3.9, OpenCV and "pip3 install opencv-python-headless" and it worked.
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 >
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'