0

I am learning OpenCV through Python v3.8.5 since last few days, got stuck in CascadeClassifier on Facedetection. Following the same code as the tutor : https://www.youtube.com/watch?v=LopYA64KmdE Timestamp: 08:40

I have the image and haarcascade_frontalface_default.xml files in the resources folder

Still, I don't get any output for this below code. I tried changing images files too but still no output. When I tried printing it print till 'foo1'. So I suspect the issue is around detectMultiScale() method.

Here is my code:

import cv2

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

# img = cv2.imread('resources\\lena.jpg')
# img = cv2.imread('D:\photos\house\DSCF2736 copy.jpg')
img = cv2.imread('resources\\messi5.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

print('foo1')
faces = face_cascade.detectMultiScale(gray,1.1,4)
print('foo2')

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

cv2.imshow('out',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I am using Windows7 32 bit PC. Is this due to OS support issue? Please let me know the solution.

Thanks

  • Have you tried changing a picture for testing? –  Jan 11 '21 at 03:43
  • @August, Yes I tried various clear clarity visible frontal face images – Aksay Sriram Jan 11 '21 at 04:28
  • Then it is obvious that there is a problem with your `haarcascade_frontalface_default.xml`, you can download a `haarcascade_frontalface_default.xml` from the Internet to try.[haarcascade_frontalface_default.xml](https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml) –  Jan 11 '21 at 05:29
  • I have the right file in the right place bro. Also, I checked all the other answers before posting this question. Nothing seems relevant to where I am stuck. So I posted here. – Aksay Sriram Jan 11 '21 at 12:56

2 Answers2

0

I guess you should write [x,y,w,h,] or (x,y,w,h) instead of x,y,w,h in the for loop because the given detectMultiScale function generates a list containing multiple [x,y,w,h] elements for every face and lists can be stored only in lists objects or tuples.

  • oh, I corrected it in my code and in the question. Thanks for that.But No output still. Since, Python is an interpreted language, it got stuck 2 lines before. – Aksay Sriram Jan 11 '21 at 02:21
  • Then try by removing the cv2.destroyAllWindows line at the end of code because it closes all open windows in opencv when this line happens to execute. – Mohammad Anas Jan 11 '21 at 12:33
0

It's in the resources folder, but is the current working directory properly set relative to the calling script. There's a common issue when the path to this xml file is not properly set for one or another reason - try with absolute path or copy it in the folder with the script and calling from command line in that folder.

See: OpenCV Facial Detection come ups with this error

Twenkid
  • 825
  • 7
  • 15
  • yes, I added the xml file and image inside the resource folder which is in current working directory only – Aksay Sriram Jan 11 '21 at 02:13
  • face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_default.xml") I tried this way, but still no output – Aksay Sriram Jan 11 '21 at 02:31