Not able to find faces(face-detection) in django (using python-opencv) project
Asked Answered
P

1

8

This block of code (in views.py) is triggered by a URL.No problem in importing cv2.(Same thing tried with virtualenvwrapper shows same result (after adding all required libraries) Camera initializes and ....

def caminit(request):  
  cam.open(0)
  img=cam.read()
  cv2.imwrite("snap"+".jpg",img[1])
  cam.release()                                #takes the instant pic

  faceCascade =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

When checked for print type(faceCascade) gives <type 'cv2.CascadeClassifier'>.The object is created.

moving on further in same caminit

image = cv2.imread("snap.jpg")

# when checked with image.dtype it shows correct uint8 also image.shape shows correct data {Eg: (480, 640, 3)}

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

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

now THE crucial part "Finding the no. of faces"

print "Found {0} faces!".format(len(faces))

OUTPUT in TERMINAL:

Found 0 faces!

Why is this happening?

I have tried debugging by printing in terminal.I have mentioned them in comments.Camera being used is My laptop (HP envy) camera which gives snap with resolution 640x480.

I suspect something needs to be tweaked in faceCascade.detectMultiScale(..) block.(The parameters).I tried with scalefactor = 1.000001 and minNeighbors = 3 to no avail.

Physiology answered 8/12, 2015 at 7:31 Comment(10)
You seem to have only included a fraction of your question.. please include the rest of it, hopefully along with what you have tried/researched so far and why that hasn't workedOutrun
I was editing the question.The old Question was posted by mistake.Please do have look at new one.Physiology
Have you checked that the picture is created correctly? Is there a clear distinction in the photo of what should be a face? (lighting in pic etc)Outrun
Pic is opening perfectly with face.I have tried it many times. BUT When I try the same code (inside def caminit) run with simple " python caminit.py " without integrating with Django, NO ERRORS CAME, FACE IS detected.Physiology
This is why I was curious if the picture is actually created correctly (in django) every server I know of doesn't have a camera so I'm not sure how its supposed to have been able to take a picture (it certainly doesn't have access to the users machine). I'd have thought you'd need to take a picture client side and send that to your viewOutrun
Picture gets created in django project base directory and then cv2.imread reads image. (snap.jpg) I think it reads image because when checked with printing out image.dtype it shows correct uint8 in terminal and also image.shape shows correct data {Eg: (480, 640, 3)}.Physiology
Let us continue this discussion in chat.Physiology
hey! I am facing similar problems.Mullinax
Curious to know the answer. Someone do help.Prelature
If cv2.CascadeClassifier cannot find the requested xml file, it will return an empty cv2.CascadeClassifier object. To test if you cascade classifier is correctly loaded, can you try printing faceCascade.empty()?Roband
D
0

In my experience the classifier that predicts the best is this: haarcascade_frontalface_alt2.xml, you can try with it.

This is the code that works for me:

min_face_size=30
max_face_size=100
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
faces = face_cascade.detectMultiScale(img_gray, 1.05,1,0| cv2.cv.CV_HAAR_SCALE_IMAGE,(min_face_size,min_face_size),(max_face_size,max_face_size))

Apart from trying this, you should make sure that you are loading a real image. It might happen that you are loading a black image, then it can return something like what you said (480, 640, 3).

Detailed answered 11/12, 2015 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.