python+opencv面部识别

使用python+opencv面部识别

原文链接

Facial Detection with openCV and Python

环境配置:

1.系统环境:ubuntu14.04 LTS
2.编译环境:Python2.7.4
3.依赖库: cv2
4.其他工具:haarcascade_frontalface_alt.xml

服务安装

在ubuntu14.04 LTS上,我运行这些:

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

下载面部检测训练器

为了可以使程序识别你需要一个XML文件训练数据。可以这样下载:

wget http://eclecti.cc/files/2008/03/haarcascade_frontalface_alt.xml

脚本

这是检测面部的python脚本

#-*- coding:utf-8 -*-
import cv2

def detect(path):
    img = cv2.imread(path)
    cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.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('detected.jpg', img);

rects, img = detect("test.jpg")
box(rects, img)

结果图

原图:

脸部检测图:

回顾与总结

  • 事实上检测结果并不完美,要知道面部检测可不是一件简单且落实锤的研究
  • 另外值得注意的是haarcascade_frontalface_alt.xml文件是用来检测“frontal faces”,而不是profile中的faces。也可用于检测其他特征,例如手。

猜你喜欢

转载自blog.csdn.net/qq_30650153/article/details/79211862