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.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Mohit Kumar
  • 500
  • 6
  • 24
  • 1
    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 worked – Sayse Dec 08 '15 at 07:38
  • I was editing the question.The old Question was posted by mistake.Please do have look at new one. – Mohit Kumar Dec 08 '15 at 08:02
  • 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) – Sayse Dec 08 '15 at 08:09
  • 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. – Mohit Kumar Dec 08 '15 at 08:13
  • 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 view – Sayse Dec 08 '15 at 08:15
  • 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)}. – Mohit Kumar Dec 08 '15 at 09:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97259/discussion-between-mohit-kumar-and-sayse). – Mohit Kumar Dec 08 '15 at 09:05
  • hey! I am facing similar problems. – Rohit Roy Dec 08 '15 at 09:42
  • Curious to know the answer. Someone do help. – Killing_Falcon Dec 08 '15 at 09:45
  • 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()`? – Mark Hannel Nov 09 '17 at 18:58

1 Answers1

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).

hoaphumanoid
  • 977
  • 1
  • 9
  • 25