-3

I am not able to print the face coordinates and getting errors

The code works fine till that line. I tried fixing it in many ways like using location instead of the name of the picture.

import cv2
trained_face_data= cv2.CascadeClassifier('haarcascade_frontface_default.xml')
# the source pic
img=cv2.imread('D:\\opencv\\CUTE  2.png')
#img=cv2.imread('CUTE  2.png')
#convert to gray
Gray_img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_coordinates = trained_face_data.detectMultiScale(Gray_img)
#showng an image
#cv2.imshow('Clever Programmer Face Detector', Gray_img)
print(face_coordinates)
# wait to excute
cv2.waitKey()

the Erroe message is cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

What is the cause of the error?

R44
  • 9
  • 2
  • Are you sure `Gray_img` is not `None`? BTW, avoid posting error messages as image. Copy-n-paste them here. – Berriel Aug 17 '21 at 12:52
  • when i excute cv2.imshow('Clever Programmer Face Detector', Gray_img), it works with no error – R44 Aug 17 '21 at 12:56
  • 1
    By "it works", you mean the image is shown? Please, update the question with the full traceback. Then, delete the image. – Berriel Aug 17 '21 at 12:58
  • Does this answer your question? [error: (-215) !empty() in function detectMultiScale](https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale) – Berriel Aug 17 '21 at 13:00
  • yes the image is converted to gray and shown – R44 Aug 17 '21 at 13:00
  • i changed the first line to trained_face_data=cv2.CascadeClassifier('D:\\opencv\\haarcascade_frontface_default.xml') to make sure that it take the right path, and still not working – R44 Aug 17 '21 at 13:11

1 Answers1

0

Check the name of trained file, please:
haarcascade_frontface_default or haarcascade_frontalface_default ?

And also where is your xml file? check its path again.

ProjectFolder
-script.py
-xml file
-im.jpg

import sys
import cv2

pth=sys.path[0] # Returns the "ProjectFolder" path
cs= cv2.CascadeClassifier(pth+'/haarcascade_frontalface_default.xml')

im=cv2.imread(pth+'/im.jpg')
gry= cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

data = cs.detectMultiScale(gry)
print(data)

cv2.waitKey()

Its working well in my case:

[[ 91 303 573 573]]

Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36