Mac 下用opencv进行人脸识别并保存图片

2016-5-18 Frank Python

安装opencv

  1. 已安装brew
  2. 添加opencv所在的homebrew/science
brew tap homebrew/science 
  1. 安装
brew install opencv 
  1. 可以在下面路径找到opencv了
cd /usr/local/Cellar/opencv/2.4.12_2/ 

设置python

  1. 找到python包路径
cd /Library/Python/2.7/site-packages/ 
  1. 创建软连接
ln -s /usr/local/Cellar/opencv/2.4.12_2/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.12_2/lib/python2.7/site-packages/cv2.so cv2.so 
  1. 测试
import cv2 

人脸识别并保存图片

  1. 从官网http://opencv.org/ 下载2.4.11,主要用到data里面的xml。
  2. 代码
#!bin/evn python #-*-coding:utf8-*- '''
name:face.py
$ python face.py input.jpg
''' import sys import cv2 # Get user supplied values imagePath = sys.argv[1] #cascPath = sys.argv[2] cascPath = "../../data/haarcascades/haarcascade_frontalface_alt.xml" # Create the haar 级联 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,
   minNeighbors=5,
   minSize=(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.imwrite('output.jpg',image, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) #显示 #cv2.imshow("Faces found" ,image) #cv2.waitKey(0)  #cv2.destroyAllWindows()

发表评论 登录

Top