Mac 下用opencv进行人脸识别并保存图片
2016-5-18 Frank Python
安装opencv
-
已安装brew
-
添加opencv所在的homebrew/science
brew tap homebrew/science
-
安装
brew install opencv
-
可以在下面路径找到opencv了
cd /usr/local/Cellar/opencv/2.4.12_2/
设置python
-
找到python包路径
cd /Library/Python/2.7/site-packages/
-
创建软连接
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
-
测试
import cv2
人脸识别并保存图片
-
从官网http://opencv.org/ 下载2.4.11,主要用到data里面的xml。
-
代码
'''
name:face.py
$ python face.py input.jpg
''' import sys import cv2 imagePath = sys.argv[1] cascPath = "../../data/haarcascades/haarcascade_frontalface_alt.xml" facecascade = cv2.CascadeClassifier(cascPath) image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 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)) 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])
发表评论 登录: