Python 对图片进行人脸识别
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