Ask Your Question
0

Error while running code that uses Haar Classifier xml: "Incorrect type of self "

asked 2018-02-28 00:48:58 -0600

Joshitha gravatar image

I created my own Haar Cascade Classifier for detecting cows. The code works perfectly fine for the available OpenCv xml files but shows the following error when I run it using the xml file that I created.

Traceback (most recent call last):
File "C:........\Haar Cascade\cow_detect_image.py", line 11, in < module >

cows = cow_cascade.detectMultiScale(gray, 1.3, 5)

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

Here's the code:

import numpy as np

import cv2

cow_cascade = cv2.CascadeClassifier('cow.xml')

img = cv2.imread('Cow3.bmp')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cows = cow_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in cows:

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)

cv2.waitKey(0)

cv2.destroyAllWindows()

What is wrong and how can I fix this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-02-28 02:39:55 -0600

saideepthik gravatar image

please check the path and name of the xml file which you created after training.

edit flag offensive delete link more

Comments

The xml was in the same folder itself, and even on specifying the path, I got this error.

Joshitha gravatar imageJoshitha ( 2018-02-28 02:43:13 -0600 )edit

i tried with your code it works fine. please check once again.

import numpy as np

import cv2

cow_cascade = cv2.CascadeClassifier('/home/user/Desktop/cascade1.xml')

img = cv2.imread('/home/user/Desktop/34998473.jpeg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cows = cow_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in cows:

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)

cv2.waitKey(0)

cv2.destroyAllWindows()

saideepthik gravatar imagesaideepthik ( 2018-02-28 02:58:52 -0600 )edit

I have figured out where I went wrong, this particular error is thrown if an xml file from dpm cascade model is used

Joshitha gravatar imageJoshitha ( 2018-02-28 03:57:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-28 00:48:58 -0600

Seen: 1,221 times

Last updated: Feb 28 '18