用Python进行人脸识别

0 安装环境

 

操作系统: win7 (64bit)
python :2.7.9.4 (32bit
openCV: 2.4.13

 

 

1 安装openCV

 

下载地址 http://opencv.org/ , 选择 https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.13/opencv-2.4.13.exe/download

解压后,得到,选择build\python\2.7\x86\cv2.pyd, 复制到python-2.7.9\Lib\site-packages\中即可。

 

关于C或者JAVA接口的安装,可参考build\doc\opencv_tutorials.pdf, 里面各种平台的详细安装步骤,以及结合IDE编译的说明。

Linux版本的下载地址https://github.com/Itseez/opencv/archive/2.4.13.zip

 

 

2 python 代码下载测试

 

下载地址 https://github.com/shantnu/FaceDetect/archive/master.zip;    github 网址:https://github.com/shantnu/FaceDetect/

解压后

操作说明

 

Run the code like this:

*python face_detect.py abba.png haarcascade_frontalface_default.xml*

If you want to understand how the code works, the details are here:

https://realpython.com/blog/python/face-recognition-with-python/


Update: Now supports OpenCV3. This change has been made by furetosan ( https://github.com/furetosan) and tested on Linux.

To run the OpenCV3 version, run facedetect_cv3.py.

 

 

 

命令行

  

 

 

 

 

           

 

 

3  代码说明

 

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

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

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Faces found", image)
cv2.waitKey(0)

 

 

 

其中,当识别有错误的时候,可修改参数

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1, ##1.1  可根据需要修改
    minNeighbors=5,  ##5     可根据需要修改
    minSize=(30, 30), ##30, 30 可根据需要修改
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

 

4 Linux 下测试

 

安装依赖包

sudo apt-get install libopencv-* 
sudo apt-get install python-opencv

测试命令

python2.x face_detect.py abba.png haarcascade_frontalface_default.xml

结果

 

5 说明

 

以上所有代码和操作都来源于  Shantnu Tiwari github主页 https://github.com/shantnu


 

参考资料

1   Face Recognition With Python, in Under 25 Lines of Code   https://realpython.com/blog/python/face-recognition-with-python/

2   安装文件中的build\doc\opencv_tutorials.pdf

3   python使用opencv进行人脸识别   http://www.cnblogs.com/ma6174/archive/2013/03/31/2991315.html

4   Introduction to OpenCV  http://docs.opencv.org/2.4/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction

posted @ 2016-09-11 10:23  kongmeng  阅读(3133)  评论(0编辑  收藏  举报