Open cv face recognition python
<p>I started learning some object recognition and I'm interested in <code>openCV</code>. First of all, I tried the tutorial where your face is recognized. I installed numPy and <code>openCV</code>. When I tried running the program below, I received this error: </p>

<blockquote>
  <p>File "path to my program\camera.py", line 4, in 
      face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  cv2.error:
  D:\Build\OpenCV\opencv-3.1.0\modules\core\src\persistence.cpp:2220:
  error: (-212) haarcascade_frontalface_default.xml(1): Valid XML should
  start with '' in function icvXMLParse</p>
</blockquote>

<p>I was trying to find the answer to this question everywhere but I failed. I found a lot of other people had this problem, but the solutions didn't work. I tried re-installing python, installing a different <code>openCV</code> etc.</p>

<p>My code is here: </p>

<pre><code>import numpy as np
import cv2

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

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

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imshow('img',img)
    k = cv2.waitKey(30) &amp; 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()
</code></pre>

<p>I will be grateful for any help.</p>

<p>P.S.: My <code>xml</code> program starts with <code>&lt;?xml...?&gt;</code></p>

<p>I just tried it out and I didn't get the error. If I intentionally mess up one or both of the files I get that error, but not the ones directly from the github project. These are the first ten lines of each file for me:</p>

<pre><code>$ head haarcascade_*.xml
==&gt; haarcascade_eye.xml &lt;==
&lt;?xml version="1.0"?&gt;
&lt;!--
    Stump-based 20x20 frontal eye detector.
    Created by Shameem Hameed (http://umich.edu/~shameem)

////////////////////////////////////////////////////////////////////////////////////////

  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.

  By downloading, copying, installing or using the software you agree to this license.

==&gt; haarcascade_frontalface_default.xml &lt;==
&lt;?xml version="1.0"?&gt;
&lt;!--
    Stump-based 24x24 discrete(?) adaboost frontal face detector.
    Created by Rainer Lienhart.

////////////////////////////////////////////////////////////////////////////////////////

  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.

  By downloading, copying, installing or using the software you agree to this license.
</code></pre>

<p>What does <code>head haarcascade_*.xml</code> output for you? If yours look the same as mine, is there a chance some odd control characters or something got into your file? Try <code>head haarcascade_frontalface_default.xml | hexdump -C</code> to see the exact bytes at the beginning of the file. For me, it outputs:</p>

<pre><code>00000000  3c 3f 78 6d 6c 20 76 65  72 73 69 6f 6e 3d 22 31  |&lt;?xml version="1|
00000010  2e 30 22 3f 3e 0a 3c 21  2d 2d 0a 20 20 20 20 53  |.0"?&gt;.&lt;!--.    S|
00000020  74 75 6d 70 2d 62 61 73  65 64 20 32 34 78 32 34  |tump-based 24x24|
00000030  20 64 69 73 63 72 65 74  65 28 3f 29 20 61 64 61  | discrete(?) ada|
00000040  62 6f 6f 73 74 20 66 72  6f 6e 74 61 6c 20 66 61  |boost frontal fa|
00000050  63 65 20 64 65 74 65 63  74 6f 72 2e 0a 20 20 20  |ce detector..   |
00000060  20 43 72 65 61 74 65 64  20 62 79 20 52 61 69 6e  | Created by Rain|
00000070  65 72 20 4c 69 65 6e 68  61 72 74 2e 0a 0a 2f 2f  |er Lienhart...//|
00000080  2f 2f 2f 2f 2f 2f 2f 2f  2f 2f 2f 2f 2f 2f 2f 2f  |////////////////|
*
000000d0  2f 2f 2f 2f 2f 2f 0a 0a  20 20 49 4d 50 4f 52 54  |//////..  IMPORT|
000000e0  41 4e 54 3a 20 52 45 41  44 20 42 45 46 4f 52 45  |ANT: READ BEFORE|
000000f0  20 44 4f 57 4e 4c 4f 41  44 49 4e 47 2c 20 43 4f  | DOWNLOADING, CO|
00000100  50 59 49 4e 47 2c 20 49  4e 53 54 41 4c 4c 49 4e  |PYING, INSTALLIN|
00000110  47 20 4f 52 20 55 53 49  4e 47 2e 0a 0a 20 20 42  |G OR USING...  B|
00000120  79 20 64 6f 77 6e 6c 6f  61 64 69 6e 67 2c 20 63  |y downloading, c|
00000130  6f 70 79 69 6e 67 2c 20  69 6e 73 74 61 6c 6c 69  |opying, installi|
00000140  6e 67 20 6f 72 20 75 73  69 6e 67 20 74 68 65 20  |ng or using the |
00000150  73 6f 66 74 77 61 72 65  20 79 6f 75 20 61 67 72  |software you agr|
00000160  65 65 20 74 6f 20 74 68  69 73 20 6c 69 63 65 6e  |ee to this licen|
00000170  73 65 2e 0a                                       |se..|
00000174
</code></pre>

python xml opencv face-recognition