python+opencv 做人脸识别

一.准备工作

  对于Linux操作系统

   1.安装Python

   2.安装依赖库,命令如下:

     yum install cmake gcc gcc-c++ gtk2-devel gimp-develgimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devellibpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-develjasper-devel jasper-utils swig python libtool nasm

  3.安装opencv

1、下载  http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download

     并解压opencv2.49 

2、安装依赖的库

sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev

3、安装opencv

cd opencv2.49 
mkdir build 
cd build 
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 
make 
sudo make install

3、拷贝cv2.so  (查找其路径的命令  find / -name cv2.so )

cp /usr/local/lib/python2.7/site-packages/cv* /usr/local/lib/python2.7/site-packages/       #第二个路径是根据你使用的Python的site-packages目录位置决定的,如果你使用的是anaconda ,复制cv2.so 到anaconda下的site-packages下

二.python 程序如下

import cv2

import sys

path=sys.argv[1]

face_patterns = cv2.CascadeClassifier('/usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')


#haarcascade_frontalface_default.xml 文件在你opencv下有,查找出来自己的路径

sample_image = cv2.imread(path)

faces = face_patterns.detectMultiScale(sample_image,scaleFactor=1.1,minNeighbors=5,minSize=(100, 100))


for (x, y, w, h) in faces:
    cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2)


cv2.imwrite('/Users/abel/test.png', sample_image);

保存为face-rg.py

$python face-rg.py /home/maicaijian/test_1.jpg                #命令后面的路径是图片的路径

运行效果如下:


python+opencv 做人脸识别_第1张图片


你可能感兴趣的:(python,机器学习基础)