Ask Your Question
-1

cpp:1698: error: (-215:Assertion failed) Please help

asked 2018-11-18 17:15:12 -0600

btasci67 gravatar image

Hello, we are trying to write a face detection code in pycharm.

"import cv2

img = cv2.imread("angelina.jpg")

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

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

faces = face_cascade.detectMultiScale(gray, 1.1, 4)

for(x,y,w,h)in faces: cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255),1)

cv2.imshow('img', img) cv2.waitKey(0)"

when we try to run this code we get that error: Traceback (most recent call last): File "C:/Users/Barış/Desktop/haarcascade.py", line 11, in <module> faces = face_cascade.detectMultiScale(gray, 1.1, 4) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

please help us to come through that problem

edit retag flag offensive close merge delete

Comments

1

cascade file not loaded ...

berak gravatar imageberak ( 2018-11-19 01:27:37 -0600 )edit

@berak then how can i load it? Sorry i'm newbie to Python.

btasci67 gravatar imagebtasci67 ( 2018-11-19 02:32:38 -0600 )edit
1

change the path to the xml file, so it can be found ;)

you also need to check, if your image was loaded ...

(somehow, python noobs never understand this)

berak gravatar imageberak ( 2018-11-19 02:35:51 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2018-11-18 19:51:26 -0600

supra56 gravatar image

Change this:

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

to

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edit flag offensive delete link more

Comments

Nothing's changed. https://i.hizliresim.com/EmQpPv.png (screenshot)

btasci67 gravatar imagebtasci67 ( 2018-11-19 02:31:35 -0600 )edit

@btasci67. There is nothing wrong with your code. I tested it on Raspberry pi 3B+ using linux. Also nothing wrong with path. Perhap @berak stating that you must put all in one folder such as xml and jpg.

import cv2
img = cv2.imread("lena.jpg")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for(x,y,w,h)in faces:
    cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255),1)
cv2.imshow('img', img)
cv2.waitKey(0)
supra56 gravatar imagesupra56 ( 2018-11-19 07:26:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-18 17:15:12 -0600

Seen: 4,915 times

Last updated: Nov 18 '18