Anna Jeanine
Anna Jeanine

Reputation: 4125

OpenCV: Incorrect type of self (must be 'CascadeClassifier' or its derivative)

I'm following a tutorial on OpenCV but I have ran into a problem that I can't seem to fix. The code I currently have:

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('/home/Desktop/family.png')

face_cascade = cv2.CascadeClassifier('/home/Desktop/family.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(face_cascade)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print (faces)
for (x,y,w,h) in faces:
    cv2.rectangle(img, (x,y), (w+h, y+h), (255,0,0),5)
    roi_gray = gray[y:y:h, x:x+w]
    roi_color = img[y:y:h, x:x+w]

plt.imshow(img)
plt.show()

The output of this is:

<CascadeClassifier 0x7f5f22e9eb30>

Traceback (most recent call last):
  File "/home/testtt.py", line 10, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
TypeError: Incorrect type of self (must be 'CascadeClassifier' or its derivative)

Does anyone see what I am doing wrong? Clearly face_cascade is of type CascadeClassifier but yet it later on fails.

Upvotes: 0

Views: 1950

Answers (1)

EdChum
EdChum

Reputation: 394439

Your error is that you're loading your image to setup the classifier, you're supposed to load the xml that sets your classifier up for face detection:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

you may need to update the path to where ever openCV is installed on your machine, you can also follow the openCV tutorial: https://docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.html

Upvotes: 1

Related Questions