I am new to opencv and am trying to detect a face in an image however I am having problems with this line of code
faces = cv2.CascadeClassifier.detectMultiScale(gray_img, scaleFactor=1.05, minNeighbors=5, minSize=(1,1))
I get this error
TypeError: descriptor 'detectMultiScale' requires a 'cv2.CascadeClassifier' object but received a 'numpy.ndarray'
I have tried doing research on this error however nothing seems to tell me why this is happening and how to fix it.
full code
import cv2
img = cv2.imread("C:\\Users\\Astroid\\Desktop\\.py pics\\pic.jpg")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier('C:\\Users\\Astroid\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
minSize=(10, 10)
flags=cv2.CASCADE_SCALE_IMAGE
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("pic", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
If i'm not so wrong, i guess you have not mentioned the CascadeClassifier in your code.
e.g.
# Classifier you want to use
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
...
...
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
...
Here's a code that executes:
import cv2
# don't add 1 in arg
img = cv2.imread("your path to the image")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier('your Path to
haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
minSize=(10, 10)
flags=cv2.CASCADE_SCALE_IMAGE
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("pic", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
so after all that, I tweaked with the code a bit and I honestly have no idea what was wrong but here is the final working code.
import cv2
img = cv2.imread("C:\\Users\\Astroid\\Desktop\\.py pics\\image.png")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faceCascade =
cv2.CascadeClassifier('C:\\Users\\Astroid\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
)
print(type(faces))
print(faces)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
This question already has answers here:
error: (-215) !empty() in function detectMultiScale
(26 answers)
Closed 10 months ago.
I am trying to do face and eye recognition with rectangles for eyes and face using OpenCv Python.
I read some of the questions and I tried the answers below the questions on similiar topics but still getting the error. The code that i've tried activates the webcam and in a second it stops working.
Here the error message:
error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Here my code that i have tried:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye_default.xml')
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
roi_gray = gray[y:y+w, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Thank you.
Edit: I noticed that once I run the code the webcam has been activated and if there is no face in front of the camera it stays active without any error but once I show my face it stops working.
Hope this will work !
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
roi_gray = gray[y:y+w, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I'm trying to detect faces from images using Haar Cascades in opencv and getting Syntax Error while running the below snippet.
import cv2
import os
imagePath = os.path.abspath("C:\\Users\\rohit\\Desktop\\Project\\1.jpg")
cascPath = os.path.abspath("C:\\Users\\rohit\\Desktop\\Project\\haarcascade_frontalface_default.xml")
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
filename = 'myconvertedimage.jpg'
cv2.imwrite(filename, image)
u missed the closing bracket in linegray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY
Hi I need help with the boundary of the eye roi since it is so to the left, to get it in the middle.A lot of examples for eyes are separated and/or in a circular shape.
img = cv2.imread('man1.png')
newImg = cv2.resize(img, (600,600))
#gray = cv2.cvtColor(newImg, cv2.COLOR_BGR2GRAY)
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(newImg,
scaleFactor=1.1,
minNeighbors=5,
#minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in faces:
cv2.rectangle(newImg, (x, y), (x + w, y + h),(0,0,0), 1)
faceROI = newImg[y:y+h,x:x+w]
#cv2.imwrite(str(w) + str(h) + '_faces.jpg', faceROI)
#roi_gray = gray[y:y + h, x:x + w]
#roi_color = frame[y:y + h, x:x + w]
eyes = eyeCascade.detectMultiScale(faceROI)
smiles = mouthCascade.detectMultiScale(faceROI,scaleFactor=1.6, minNeighbors=5)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(faceROI,(x,y),(ex+w,y+eh),(0,0,0),-1)
# Display the resulting frame
cv2.imshow('Image', newImg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
last = cv2.imwrite('faces_detected.png', faceROI)
cv2.destroyAllWindows()
I've been trying to load the .xml file on cascadeclassifier but it wont work.
Solutions on internet include using \ or / in file path but that does not work for me either
#creating a cascade classifier object
fc = 'C:/Users/abc/Desktop/cv/opencv/data/haarcascades/haarcascade_frontalcatface_default.xml'
face_cascade = cv2.CascadeClassifier(fc)
img1 = cv2.imread("images.jpg")
grayImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(grayImg, scaleFactor=1.05, minNeighbors = 5)
for x,y,w,h in faces:
img1 = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 3)
resizedd = cv2.resize(img1, (int(img1.shape[1]*7), int(img1.shape[0]*7)))
cv2.imshow("gray", resizedd)
cv2.waitKey(0)
cv2.destroyAllWindows()
also tried:
#creating a cascade classifier object
face_cascade = cv2.CascadeClassifier('C:/Users/abc/Desktop/cv/opencv/data/haarcascades/haarcascade_frontalcatface_default.xml')
img1 = cv2.imread("images.jpg")
grayImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(grayImg, scaleFactor=1.05, minNeighbors = 5)
for x,y,w,h in faces:
img1 = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 3)
resizedd = cv2.resize(img1, (int(img1.shape[1]*7), int(img1.shape[0]*7)))
cv2.imshow("gray", resizedd)
cv2.waitKey(0)
cv2.destroyAllWindows()
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
frame,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1)==ord('e'):
break
code gives this error, please help me
Traceback (most recent call last): File
"C:\Users\yavuz\Desktop\sor\sor2.pyw", line 15, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) error: ........\opencv\modules\imgproc\src\color.cpp:3737: error: (-215)
scn == 3 || scn == 4 in function cv::cvtColor
You need to check if "ret == True' before going on with cvtColor. The code should look like this:
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
frame,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1)==ord('e'):
break