您的位置:首页 > 编程语言 > Python开发

Python 对图片进行人脸识别

2016-01-07 15:29 543 查看
import cv2

def detect(path):
img = cv2.imread(path)
cascade = cv2.CascadeClassifier("/vagrant/detect/haarcascade_frontalface_alt.xml")#xml文件路径一定要注意
rects = cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))

if len(rects) == 0:
return [], img
rects[:, 2:] += rects[:, :2]
return rects, img

def box(rects, img):
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), (127, 255, 0), 2)
cv2.imwrite('/vagrant/img/detected.jpg', img);

rects, img = detect("/vagrant/img/one.jpg")
box(rects, img)


以上是源码,来自:
http://fideloper.com/facial-detection


依赖:
$ sudo apt-get update
$ sudo apt-get install -y vim build-essential python-software-properties    # The Basics
$ sudo apt-get install -y python-opencv python-numpy python-scipy        # OpenCV items

$ wget http://eclecti.cc/files/2008/03/haarcascade_frontalface_alt.xml[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: