Opencv人脸识别python
Open cv face recognition python
我开始学习一些物体识别,我对 openCV
很感兴趣。首先,我尝试了识别人脸的教程。我安装了 numPy 和 openCV
。当我尝试 运行 下面的程序时,我收到了这个错误:
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
我试图到处寻找这个问题的答案,但我失败了。我发现很多其他人都有这个问题,但解决方案不起作用。我尝试重新安装 python、安装不同的 openCV
等
我的代码在这里:
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) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
如有任何帮助,我将不胜感激。
P.S.: 我的 xml
程序以 <?xml...?>
开头
我刚试了一下,没有报错。如果我故意弄乱一个或两个文件,我会收到该错误,但不会直接来自 github 项目的文件。这些是我每个文件的前十行:
$ head haarcascade_*.xml
==> haarcascade_eye.xml <==
<?xml version="1.0"?>
<!--
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.
==> haarcascade_frontalface_default.xml <==
<?xml version="1.0"?>
<!--
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.
head haarcascade_*.xml
为您输出什么?如果你的看起来和我的一样,是否有可能一些奇怪的控制字符或其他东西进入了你的文件?尝试 head haarcascade_frontalface_default.xml | hexdump -C
查看文件开头的确切字节。对我来说,它输出:
00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
00000010 2e 30 22 3f 3e 0a 3c 21 2d 2d 0a 20 20 20 20 53 |.0"?>.<!--. 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
问题已通过重新下载 xml 文件解决。就这么简单,我想了半天
我开始学习一些物体识别,我对 openCV
很感兴趣。首先,我尝试了识别人脸的教程。我安装了 numPy 和 openCV
。当我尝试 运行 下面的程序时,我收到了这个错误:
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
我试图到处寻找这个问题的答案,但我失败了。我发现很多其他人都有这个问题,但解决方案不起作用。我尝试重新安装 python、安装不同的 openCV
等
我的代码在这里:
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) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
如有任何帮助,我将不胜感激。
P.S.: 我的 xml
程序以 <?xml...?>
我刚试了一下,没有报错。如果我故意弄乱一个或两个文件,我会收到该错误,但不会直接来自 github 项目的文件。这些是我每个文件的前十行:
$ head haarcascade_*.xml
==> haarcascade_eye.xml <==
<?xml version="1.0"?>
<!--
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.
==> haarcascade_frontalface_default.xml <==
<?xml version="1.0"?>
<!--
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.
head haarcascade_*.xml
为您输出什么?如果你的看起来和我的一样,是否有可能一些奇怪的控制字符或其他东西进入了你的文件?尝试 head haarcascade_frontalface_default.xml | hexdump -C
查看文件开头的确切字节。对我来说,它输出:
00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
00000010 2e 30 22 3f 3e 0a 3c 21 2d 2d 0a 20 20 20 20 53 |.0"?>.<!--. 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
问题已通过重新下载 xml 文件解决。就这么简单,我想了半天