-1
import cv2 as cv
img=cv.imread('photo/goku3.jpg')
cv.imshow('image', img)

gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray)

haar_cascade=cv.CascadeClassifier('faces.xml')
faces_rect=haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbours=3)
print(f"Number of faces found = {len(faces_rect)}")

for (x,y,w,h) in faces_rect:
    cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)

cv.imshow('detected face', img)
cv.waitKey(0)
Traceback (most recent call last):
  File "e:\imageRecognitionProj\faceDetect.py", line 9, in <module>
    faces_rect=haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbours=3)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'detectMultiScale'
> Overload resolution failed:
>  - 'minNeighbours' is an invalid keyword argument for CascadeClassifier.detectMultiScale()
>  - 'minNeighbours' is an invalid keyword argument for CascadeClassifier.detectMultiScale()

inside the haar_cascade variable i have defined the cascadeClassifier() with the xml file

I think the function detectMultiScale() is not available. I tried to reinstall open-cv but it didn't worked.

General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

0

The issue is just a spelling mistake:
Replace minNeighbours=3 with minNeighbors=3.

The error message means that there is no version of detectMultiScale() method that has an argument named minNeighbours (but that the method exists with other names of arguments).


Regarding the availability of detectMultiScale():

Make sure you don't have packages that my conflicted - opencv-python-headless and opencv-python, but only have opencv-contrib-python package installed.
If this is not the case, you may uninstall the packages or use new virtual environment.
If not exist, install by executing: pip install opencv-contrib-python.
(Note that according to the error message the package is installed).

I couldn't find faces.xml, and used haarcascade_frontalface_default.xml instead.

Code sample:

import cv2 as cv
img=cv.imread('photo/goku3.jpg')
cv.imshow('image', img)

gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray)

#haar_cascade=cv.CascadeClassifier('faces.xml')
haar_cascade=cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")  #https://stackoverflow.com/a/59717885/4926757

#faces_rect=haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbours=3)
faces_rect=haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
print(f"Number of faces found = {len(faces_rect)}")

for (x,y,w,h) in faces_rect:
    cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)

cv.imshow('detected face', img)
cv.waitKey(0)

img (example):
enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65